5

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 1

12
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
  • 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?
    – smohadjer
    Commented May 15, 2015 at 9:35
  • 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. 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.
    – smohadjer
    Commented May 15, 2015 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.