Reverting part of a commit with git

skiphoppy picture skiphoppy · Jan 25, 2011 · Viewed 36.1k times · Source

I want to revert a particular commit in git. Unfortunately, our organization still uses CVS as a standard, so when I commit back to CVS multiple git commits are rolled into one. In this case I would love to single out the original git commit, but that is impossible.

Is there an approach similar to git add --patch that would allow me to selectively edit diffs to decide which parts of a commit to revert?

Answer

mipadi picture mipadi · Jan 25, 2011

Use the --no-commit (-n) option to git revert, then unstage the changes, then use git add --patch:

$ git revert -n $bad_commit    # Revert the commit, but don't commit the changes
$ git reset HEAD .             # Unstage the changes
$ git add --patch .            # Add whatever changes you want
$ git commit                   # Commit those changes

Note: The files you add using git add --patch are the files you want to revert, not the files you want to keep.