shantanu
shantanu

Reputation: 2008

Git : revert to old version for particular file with checkout command and commit

I want to revert to previous version for a particular file.
I am using following command.

git checkcout <commit hash> <file-name>

Now I want to commit this file.
I am doing

git commit -a.

It is giving me following messages

"nothing to commit, working directory clean"

EDIT : All these commit have been pushed to remote repos.

Upvotes: 2

Views: 2110

Answers (1)

merlin2011
merlin2011

Reputation: 75575

Your command below moves the HEAD pointer to the old commit.

 git checkout <commit hash> <file-name>

You probably intended to do the following command, which will "revert to previous version for a particular file", without moving the current HEAD.

 git checkout <commit hash> -- <file-name>

After the command above, git status will show the file as being modified. You can then do git commit -a to commit the file on the top of the current commit.

Update: If you had previously moved your HEAD pointer, you most likely want to move it back to your working branch before running the command above and committing. So the full sequence of commands should be:

 git checkout master
 git checkout <commit hash> -- <file-name>
 git commit -a 

Upvotes: 5

Related Questions