Search code examples
gitgit-revert

git selective revert (equivalent of `git revert --patch`)


Is there a preferred method of reverting part of a previous commit, along the lines of partially reverting unstaged changes (git checkout -p) and partially adding unstaged changes (git add -p)?

i.e. I have a commit several (or even, many) commits back that contains both wanted and unwanted changes and I want to selectively revert some of those changes while keeping others.

My current workflow is not fun:

git diff commit commit^ > selective.diff
cp selective.diff selective2.diff
nvim selective2.diff 
# change unwanted - to ' ' and remove unwanted +, then save
rediff selective.diff selective2.diff | rewrite selective2.diff
git apply selective2.diff

and pray the patch takes


Solution

  • git revert --no-commit
    git reset --patch     # or `git checkout --patch` if you're sure