I'm working on a branch where I have changed file A in several commits and now I want to revert all changes from it so that file A's state is same as initial state it had when I had first created the branch. What is the easiest way to achieve this?
1 Answer
git checkout <sha1_of_commit> file/to/restore
It will revert file to state after <sha1_of_commit>
commit.
If you want to revert it to state before this commit use
git checkout <sha1_of_commit>~1 file/to/restore
-
This reverted my file back to the state it had "after" <sha1_of_commit>, but didn't revert the changes applied in that commit itself. Is there any way to revert to state before the first commit, that is to the original state of the file? Commented May 15, 2015 at 9:35
-
Let's say that
sha
of that commit isabcde
, then usegit checkout abcde~1 file/to/restore
.~1
means that you checkout one commit beforeabcde
commit. I'll update answer with this info. Commented May 15, 2015 at 9:58 -
You can get initial state back only if you had commited it to git. If you didn't commited initial state, than you can get back only state after first commit of that file. Commented May 15, 2015 at 10:12
-
The file was already there when I created the branch from master so ~1 should revert it back to its initial state. Commented May 15, 2015 at 10:23