Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git re-add only those files that have already been staged

Tags:

git

I often have a situation where I have git added a set of files for a commit, but then have to make a modification to those files so end up with the modifications on those same files in Changed but not updated.

Is there a one liner I can execute to git add only the files that exist in Changed but not updated and Changes to be committed lists?

markdsievers$git status
# On branch master
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   src/com/foo/Bar.java
#   modified:   src/com/foo/Foo.java
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   src/com/foo/Bar.java
#   modified:   src/com/foo/Foo.java
#   modified:   src/com/foobar/FooBar.java

For the above example I only want Foo.java and Bar.java but not FooBar.java added to the Changes to be committed: list.

like image 909
markdsievers Avatar asked Aug 26 '11 03:08

markdsievers


People also ask

How can you remove a file which is staged but not yet committed?

If unwanted files were added to the staging area but not yet committed, then a simple reset will do the job: $ git reset HEAD file # Or everything $ git reset HEAD . To only remove unstaged changes in the current working directory, use: git checkout -- .

How do you add all files not staged for commit?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area.


1 Answers

This is one way:

 git add -u $(git status --porcelain | grep ^MM | cut -d ' ' -f 2)
like image 138
manojlds Avatar answered Sep 28 '22 06:09

manojlds