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?
Thanks, that worked. Is there a way do that for all files in the repository instead of having to specify them individually?
Readonly
2008-09-09 19:39:55
git checkout -- .
Charles Bailey
2009-06-20 10:14:53
+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
2008-09-09 19:39:24
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
2008-09-09 19:52:21
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
2009-06-20 10:22:48
+2
A:
Wouldn’t a simple “git checkout HEAD -- $(git ls-files -m)
” work?
Aristotle Pagaltzis
2008-09-17 22:58:50
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
2008-09-17 23:02:17
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
2009-06-20 10:21:56
the same result can be achieved via ``git checkout `git ls-files -m` ``
asymmetric
2009-12-11 21:02:54
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
2009-12-11 21:04:49
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
2009-12-16 11:08:24
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
2010-07-28 20:06:05
+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
2009-06-20 10:28:04