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?
-
git checkout path/to/file/to/revertReadonly : Thanks, that worked. Is there a way do that for all files in the repository instead of having to specify them individually?Charles Bailey : git checkout -- . -
Another quicker way is:
git stash save --keep-indexAfter that, you can drop that stash with a
git stash dropcommand if you like. -
for f in `git ls-files -m`; do git checkout HEAD -- $f; doneWill 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.
Charles Bailey : If a file is modified in the index and again in the working tree this will discard the staged changes as well, though. -
Wouldn’t a simple “
git checkout HEAD -- $(git ls-files -m)” work?Ben Collins : 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.Charles Bailey : If a file is modified in the index and again in the working tree this will discard the staged changes as well, though.asymmetric : 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?)Aristotle Pagaltzis : 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. -
git reset --hard
Charles Bailey : This will throw away changes added to the index as well.Josh K : @Charles: What do you mean?ChrisH : I believe he means that anything you added to the stage with git add would also get discarded. The question is asking for "unstaged". -
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
0 comments:
Post a Comment