Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-revert

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

like image 861
Mahmoud Al-Qudsi Avatar asked Jul 23 '17 16:07

Mahmoud Al-Qudsi


2 Answers

git revert --no-commit
git reset --patch     # or `git checkout --patch` if you're sure
like image 140
jthill Avatar answered Nov 09 '22 11:11

jthill


I can only think of something like

git revert commit --no-commit
git reset # now the changes are unstaged
git add -p
...
git commit

But I do not know if it is more practicable than your solution, though.

like image 9
SVSchmidt Avatar answered Nov 09 '22 11:11

SVSchmidt