After the last commit, I modified a bunch of files in my working copy, but I want to undo the changes to one of those files, as in reset it to the same state as the most recent commit.
However, I only want to undo the working copy changes of just that one file alone, nothing else with it.
How do I do that?
If your file is already staged (happens when you do a git add etc after the file is edited) to unstage your changes.
Use
Then
If not already staged, just use
You can use
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:
If you want to just undo the previous commit's changes to that one file, you can try this:
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.I have Done through git bash:
(use "git checkout -- <file>..." to discard changes in working directory)
I always get confused with this, so here is a reminder test case; let's say we have this
bash
script to testgit
:At this point, the change is not staged in the cache, so
git status
is:If from this point, we do
git checkout
, the result is this:If instead we do
git reset
, the result is:So, in this case - if the changes are not staged,
git reset
makes no difference, whilegit checkout
overwrites the changes.Now, let's say that the last change from the script above is staged/cached, that is to say we also did
git add b.txt
at the end.In this case,
git status
at this point is:If from this point, we do
git checkout
, the result is this:If instead we do
git reset
, the result is:So, in this case - if the changes are staged,
git reset
will basically make staged changes into unstaged changes - whilegit checkout
will overwrite the changes completely.For me only this one worked