views:

6083

answers:

5

I have a git repository, After the last commit, I modified a bunch of file. but I want to undo the changes to one of these file, as in reset it to the same version of itself that's in the repository, but I want to undo the change to that file alone! nothing else with it. How do I do that? assuming it's possible of course ..

+3  A: 

If you want to just undo the previous commit's changes to that one file, you can try this:

$> git checkout branchname^ filename

This will checkout the file as it was before the last commit. If you want to go a few more commits back, use the branchname~n notation.

sykora
+2  A: 

Just use

git checkout filename

This will replace filename with the latest version from the current branch. WARNING: your changes will be discarded -- no backup is kept.

nimrodm
+26  A: 
git checkout -- filename

You can do it without the -- (as suggested by nimrodm), but if the filename looks like a branch or tag (or other revision identifier), it may get confused, so using -- is best.

You can also check out a particular version of a file:

git checkout v1.2.3 -- filename         # tag v1.2.3
git checkout stable -- filename         # stable branch
git checkout origin/master -- filename  # upstream master
git checkout HEAD -- filename           # the version from the most recent commit
git checkout HEAD^ -- filename          # the version before the most recent commit
Brian Campbell
what's the difference between HEAD and HEAD^?
hasen j
HEAD is the most recent commit on the current branch, and HEAD^ is the commit before that on the current branch. For the situation you describe, you could use git checkout HEAD -- filename.
Paul
In short "git checkout sha-reference -- filename" where the sha-reference is a reference to the sha of a commit, in any form (branch, tag, parent, etc.)
Lakshman Prasad
HEAD^ , sorry but i am out of touch here little bit. What is last char in HEAD^? Can't find it in windows keyboard
mamu
@mamu It's a caret; on a US keyboard, you usually get it by typing Shift-6.
Brian Campbell
+2  A: 
git checkout <commit> <filename>

I used this today because I realized that my favicon had been overwritten a few commits ago when I upgrated to drupal 6.10, so I had to get it back. Here is what I did:

git checkout 088ecd favicon.ico
neoneye
+1  A: 

git checkout filename

This works. If changes needed to be kept please create a backup before you run this command.

Prashant Padaganur