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
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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.
The file was already there when I created the branch from master so ~1 should revert it back to its initial state.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.