Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I revert a specific commit such that it leaves what it would delete as unstaged in the file?

Tags:

git

Here is the scenario:

-I made a lot of small and big modifications to my files and didn't commit them

-I decided to use git commit --interactive and use the patch command to stage parts of commits

-I tried to leave all commits relevant when I did it

-I discovered after multiple commits that I accidentally put in a change I was supposed to leave out in the first place 6 commits ago, so I reverted that commit.

-The revert deleted the text relevant in the file, which is expected behavior.

The last step is the one I want to modify. What I want to do is revert a commit, but leave the text in the file so I can re-stage the commit. The way I went about it was diffing the HEAD to the commit before the revert commit, and then I copied and pasted the text and modified it so it wasn't in diff format so I could use it in the file, and then committed it properly.

I do realize I need to learn the habit of committing early and often. I haven't gotten the hang of it, yet, since it likes to interrupt my mindset when I'm just coding away.

The way I did fixed it was tedious. Is there a way to just leave the text in a file that would be removed in a revert as if it is an unstaged change (kind of like reset does)? Any suggestions beyond that?

like image 484
minozake Avatar asked Mar 05 '11 06:03

minozake


2 Answers

You can run the revert twice, and the last time apply --no-commit which will leave the changes in the index ready for committing:

git revert <the commit you want back in the working tree>
git revert HEAD --no-commit

The changes are now staged and ready for a new commit. To unstage run:

git reset HEAD
like image 66
asgerhallas Avatar answered Nov 15 '22 09:11

asgerhallas


After you've run the git revert BADCOMMIT you can restore a particular file to its state before that commit with:

git checkout HEAD^ -- filename

... which should update your working copy of filename to its state before the revert commit. (The old version will also be staged, so if you want to unstage it, do git reset HEAD -- filename afterwards.) I don't think there's an easier way of doing that.

like image 33
Mark Longair Avatar answered Nov 15 '22 07:11

Mark Longair