How do I discard changes in my working copy that are not in the index?
30 Answers
For all unstaged files in current working directory use:
git checkout -- .
For a specific file use:
git checkout -- path/to/file/to/revert
--
here to remove ambiguity (this is known as argument disambiguation).
For Git 2.23 onwards, one may want to use the more specific
git restore .
resp.
git restore path/to/file/to/revert
that together with git switch
replaces the overloaded git checkout
(see here), and thus removes the argument disambiguation.
It seems like the complete solution is:
git clean -df
git checkout -- .
git clean
removes all untracked files (warning: while it won't delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders) and git checkout
clears all unstaged changes.
git clean -df
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
-d
: Remove untracked directories in addition to untracked files
-f
: Force (might be not necessary depending on clean.requireForce
setting)
Run git help clean
to see the manual
2019 update
You can now discard unstaged changes in one tracked file with:
git restore <file>
and in all tracked files in the current directory (recursively) with:
git restore .
If you run the latter from the root of the repository, it will discard unstaged changes in all tracked files in the project.
Notes
git restore
was introduced in July 2019 and released in version 2.23 as part of a split of thegit checkout
command intogit restore
for files andgit switch
for branches.git checkout
still behaves as it used to and the older answers remain perfectly valid.- When running
git status
with unstaged changes in the working tree, this is now what Git suggests to use to discard them (instead ofgit checkout -- <file>
as it used to prior to v2.23). - As with
git checkout -- .
, this only discards changes in tracked files. So Mariusz Nowak's answer still applies and if you want to discard all unstaged changes, including untracked files, you could run, as he suggests, an additionalgit clean -df
.
Since no answer suggests the exact option combination that I use, here it is:
git clean -dxn . # dry-run to inspect the list of files-to-be-removed
git clean -dxf . # REMOVE ignored/untracked files (in the current directory)
git checkout -- . # ERASE changes in tracked files (in the current directory)
This is the online help text for the used git clean
options:
-d
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f
option twice if you really want to remove such a directory.
-x
Don’t use the standard ignore rules read from .gitignore
(per directory) and $GIT_DIR/info/exclude
, but do still use the ignore rules given with -e
options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset
) to create a pristine working directory to test a clean build.
-n
Don’t actually remove anything, just show what would be done.
-f
If the Git configuration variable clean.requireForce
is not set to false
, Git clean will refuse to delete files or directories unless given -f
, -n
, or -i
. Git will refuse to delete directories within the .git
subdirectory or file, unless a second -f
is given.
If you merely wish to remove changes to existing files, use checkout
(documented here).
git checkout -- .
- No branch is specified, so it checks out the current branch.
- The double-hyphen (
--
) tells Git that what follows should be taken as its second argument (path), that you skipped specification of a branch. - The period (
.
) indicates all paths.
If you want to remove files added since your last commit, use clean
(documented here):
git clean -i
- The
-i
option initiates an interactiveclean
, to prevent mistaken deletions. - A handful of other options are available for a quicker execution; see the documentation.
If you wish to move changes to a holding space for later access, use stash
(documented here):
git stash
- All changes will be moved to Git's Stash, for possible later access.
- A handful of options are available for more nuanced stashing; see the documentation.
I really found this article helpful for explaining when to use what command: http://www.szakmeister.net/blog/2011/oct/12/reverting-changes-git/
There are a couple different cases:
If you haven't staged the file, then you use
git checkout
. Checkout "updates files in the working tree to match the version in the index". If the files have not been staged (aka added to the index)... this command will essentially revert the files to what your last commit was.git checkout -- foo.txt
If you have staged the file, then use git reset. Reset changes the index to match a commit.
git reset -- foo.txt
I suspect that using git stash
is a popular choice since it's a little less dangerous. You can always go back to it if you accidently blow too much away when using git reset. Reset is recursive by default.
Take a look at the article above for further advice.
The easiest way to do this is by using this command:
This command is used to discard changes in working directory -
git checkout -- .
https://git-scm.com/docs/git-checkout
In git command, stashing of untracked files is achieved by using:
git stash -u
You can use git stash - if something goes wrong, you can still revert from the stash. Similar to some other answer here, but this one also removes all unstaged files and also all unstaged deletes:
git add .
git stash
if you check that everything is OK, throw the stash away:
git stash drop
The answer from Bilal Maqsood with git clean
also worked for me, but with the stash I have more control - if I do sth accidentally, I can still get my changes back
UPDATE
I think there is 1 more change (don't know why this worked for me before):
git add . -A
instead of git add .
without the -A
the removed files will not be staged
Instead of discarding changes, I reset my remote to the origin. Note - this method is to completely restore your folder to that of the repo.
So I do this to make sure they don't sit there when I git reset (later - excludes gitignores on the Origin/branchname)
NOTE: If you want to keep files not yet tracked, but not in GITIGNORE you may wish to skip this step, as it will Wipe these untracked files not found on your remote repository (thanks @XtrmJosh).
git add --all
Then I
git fetch --all
Then I reset to origin
git reset --hard origin/branchname
That will put it back to square one. Just like RE-Cloning the branch, WHILE keeping all my gitignored files locally and in place.
Updated per user comment below: Variation to reset the to whatever current branch the user is on.
git reset --hard @{u}
Just use:
git stash -u
Done. Easy.
If you really care about your stash stack then you can follow with git stash drop
. But at that point you're better off using (from Mariusz Nowak):
git checkout -- .
git clean -df
Nonetheless, I like git stash -u
the best because it "discards" all tracked and untracked changes in just one command. Yet git checkout -- .
only discards tracked changes,
and git clean -df
only discards untracked changes... and typing both commands is far too much work :)
In my opinion,
git clean -df
should do the trick. As per Git documentation on git clean
git-clean - Remove untracked files from the working tree
Description
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
If any optional ... arguments are given, only those paths are affected.
Options
-d Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.
-f --force If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.
Another way to get rid of new files that is more specific than git clean -df (it will allow you to get rid of some files not necessarily all), is to add the new files to the index first, then stash, then drop the stash.
This technique is useful when, for some reason, you can't easily delete all of the untracked files by some ordinary mechanism (like rm).
What follows is really only a solution if you are working with a fork of a repository where you regularly synchronize (e.g. pull request) with another repo. Short answer: delete fork and refork, but read the warnings on github.
I had a similar problem, perhaps not identical, and I'm sad to say my solution is not ideal, but it is ultimately effective.
I would often have git status messages like this (involving at least 2/4 files):
$ git status
# Not currently on any branch.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: doc/PROJECT/MEDIUM/ATS-constraint/constraint_s2var.dats
# modified: doc/PROJECT/MEDIUM/ATS-constraint/parsing/parsing_s2var.dats
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: doc/PROJECT/MEDIUM/ATS-constraint/constraint_s2Var.dats
# modified: doc/PROJECT/MEDIUM/ATS-constraint/parsing/parsing_s2Var.dats
A keen eye will note that these files have dopplegangers that are a single letter in case off. Somehow, and I have no idea what led me down this path to start with (as I was not working with these files myself from the upstream repo), I had switched these files. Try the many solutions listed on this page (and other pages) did not seem to help.
I was able to fix the problem by deleting my forked repository and all local repositories, and reforking. This alone was not enough; upstream had to rename the files in question to new filenames. As long as you don't have any uncommited work, no wikis, and no issues that diverge from the upstream repository, you should be just fine. Upstream may not be very happy with you, to say the least. As for my problem, it is undoubtedly a user error as I'm not that proficient with git, but the fact that it is far from easy to fix points to an issue with git as well.
When you want to transfer a stash to someone else:
# add files
git add .
# diff all the changes to a file
git diff --staged > ~/mijn-fix.diff
# remove local changes
git reset && git checkout .
# (later you can re-apply the diff:)
git apply ~/mijn-fix.diff
[edit] as commented, it ís possible to name stashes. Well, use this if you want to share your stash ;)
You could create your own alias which describes how to do it in a descriptive way.
I use the next alias to discard changes.
Discard changes in a (list of) file(s) in working tree
discard = checkout --
Then you can use it as next to discard all changes:
discard .
Or just a file:
discard filename
Otherwise, if you want to discard all changes and also the untracked files, I use a mix of checkout and clean:
Clean and discard changes and untracked files in working tree
cleanout = !git clean -df && git checkout -- .
So the use is simple as next:
cleanout
Now is available in the next Github repo which contains a lot of aliases:
git-clean
only removes untracked files from the working tree git-scm.com/docs/git-clean – Yegagit-clean -df
can be dangerous. It will delete local untracked files (e.g. covered by a .gitignore) Read all below carefully and consider git checkout . instead – jacanterburygit status
gives a suggestion on how to do that!git checkout -- .
– Paulogit status
gives the suggestion:git restore
.git restore
is a new command exactly for this purpose. See my 2019 update. – prosoitos