tags:

views:

10610

answers:

6

If some changes are added to the index and there are some changes that are not added to the index, how do I discard the changes in my working copy that are not added to the index yet?

+24  A: 
git checkout path/to/file/to/revert
Tobi
Thanks, that worked. Is there a way do that for all files in the repository instead of having to specify them individually?
Readonly
git checkout -- .
Charles Bailey
+17  A: 

Another quicker way is:

git stash save --keep-index

After that, you can drop that stash with a git stash drop command if you like.

Greg Hewgill
A: 
for f in `git ls-files -m`; do git checkout HEAD -- $f; done

Will get you something similar to stash save, but without cluttering your stash (or requiring an extra step to drop). Of course, any number of steps are scriptable. Either one will work.

Ben Collins
If a file is modified in the index and again in the working tree this will discard the staged changes as well, though.
Charles Bailey
+2  A: 

@Ben Collins:

Wouldn’t a simple “git checkout HEAD -- $(git ls-files -m)” work?

Aristotle Pagaltzis
yup. that'd work too. using the for loop was just the first thing that came to mind because I don't normally pass multiple paths to checkout.
Ben Collins
If a file is modified in the index and again in the working tree this will discard the staged changes as well, though.
Charles Bailey
the same result can be achieved via ``git checkout `git ls-files -m` ``
asymmetric
note: the backticks in the previous comment are misplaced, there seems to be a different syntax for comments that ignores double backticks (a bug maybe?)
asymmetric
That’s the same as what I wrote, other than that it doesn’t defend against the possibility that the first file listed is named the same as one of your refs, in which case the command line you showed would do the wrong thing.
Aristotle Pagaltzis
+1  A: 

git reset --hard

This will throw away changes added to the index as well.
Charles Bailey
@Charles: What do you mean?
Josh K
I believe he means that anything you added to the stage with git add would also get discarded. The question is asking for "unstaged".
ChrisH
+17  A: 

This checks out the current index for the current directory, throwing away all changes in files from the current directory downwards.

git checkout .

or this which checks out all files from the index, overwriting working tree files.

git checkout-index -a -f
Charles Bailey