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 ..
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.
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.
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
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
git checkout filename
This works. If changes needed to be kept please create a backup before you run this command.