Reverting part of a commit with git

GitGit Cvs

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

I have used the following successfully.

First revert the full commit (puts it in index) but don't commit.

git revert -n <sha1>  # -n is short for --no-commit

Then interactively remove the reverted GOOD changes from the index

git reset -p          # -p is short for --patch  

Then commit reverse diff of the bad changes

git commit -m "Partially revert <sha1>..."

Finally the reverted GOOD changes (which have been unstaged by the reset command) are still in the working tree. They need to be cleaned up. If no other uncommitted changes are left in the working tree, this can be done by

git reset --hard

Solution 3 - Git

Personally, I prefer this version, which reuses the auto-generated commit message and gives the user the opportunity to edit and stick the word "Partially" in before finally committing.

# generate a revert commit
# note the hash printed to console on success
git revert --no-edit <hash to revert>

# undo that commit, but not its changes to the working tree
# (reset index to commit-before-last; that is, one graph entry up from HEAD)
git reset HEAD~1

# interactively add reversions
git add -p

# commit with pre-filled message
git commit -c <hash from revert commit, printed to console after first command>

# reset the rest of the current directory's working tree to match git
# this will reapply the excluded parts of the reversion to the working tree
# you may need to change the paths to be checked out
# be careful not to accidentally overwrite unsaved work
git checkout -- .

Solution 4 - Git

Solution:

git revert --no-commit <commit hash>
git reset -p        # every time choose 'y' if you want keep the change, otherwise choose 'n'
git commit -m "Revert ..."
git checkout -- .   # Don't forget to use it.

Solution 5 - Git

Another alternative (if your current version of a file isn't too far from the version you're trying to revert) is to get the hash of the commit immediately before the one you wish to partially revert (from git log). Then your command becomes:

$ git checkout -p <hash_preceding_commit_to_revert> -- file/you/want/to/fix.ext

This does change the files in your working tree but creates no commits, so if you really stuff up you can just start again with git reset --hard -- file/you/want/to/fix.ext.

Solution 6 - Git

You can use git-revert -n, and then use add --patch to select hunks.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskiphoppyView Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - Gituser1338062View Answer on Stackoverflow
Solution 3 - Gitjeffcook2150View Answer on Stackoverflow
Solution 4 - GitKrzysztof KaczmarskiView Answer on Stackoverflow
Solution 5 - GitWalfView Answer on Stackoverflow
Solution 6 - GitWilliam PursellView Answer on Stackoverflow