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
4 Comments
smohadjer
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?
Domen Blenkuš
Let's say that
sha of that commit is abcde, then use git checkout abcde~1 file/to/restore. ~1 means that you checkout one commit before abcde commit. I'll update answer with this info.Domen Blenkuš
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.
smohadjer
The file was already there when I created the branch from master so ~1 should revert it back to its initial state.