Question
How do I discard unstaged changes in Git?
How do I discard changes in my working copy that are not in the index?
Question
How do I discard changes in my working copy that are not in the index?
Solution
For all unstaged files in current working directory use:
git restore .
For a specific file use:
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.
If a file has both staged and unstaged changes, only the unstaged changes shown in git diff
are reverted. Changes shown in git diff --staged
stay intact.
Before Git 2.23
For all unstaged files in current working directory:
git checkout -- .
For a specific file:
git checkout -- path/to/file/to/revert
--
here to remove ambiguity (this is known as argument disambiguation).
Solution
Another quicker way is:
git stash save --keep-index --include-untracked
You don't need to include --include-untracked
if you don't want to be thorough about it.
After that, you can drop that stash with a git stash drop
command if you like.
Solution
It seems like the complete solution is:
git clean -df
git checkout -- .
WARNING: while it won't delete ignored files mentioned directly in .gitignore, git clean -df
may delete ignored files residing in folders.
git clean
removes all untracked files and git checkout
clears all unstaged changes.
Solution
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
Solution
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
Solution
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.
git restore
was introduced in July 2019 and released in version 2.23 as part of a split of the git checkout
command into git restore
for files and git switch
for branches.git checkout
still behaves as it used to and the older answers remain perfectly valid.git status
with unstaged changes in the working tree, this is now what Git suggests to use to discard them (instead of git checkout -- <file>
as it used to prior to v2.23).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 additional git clean -df
.Solution
My favorite is
git checkout -p
That lets you selectively revert chunks.
See also:
git add -p
Solution
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.
Solution
If you merely wish to remove changes to existing files, use checkout
(documented here).
git checkout -- .
--
) tells Git that what follows should be taken as its second argument (path), that you skipped specification of a branch..
) indicates all paths. If you want to remove files added since your last commit, use clean
(documented here):
git clean -i
-i
option initiates an interactive clean
, to prevent mistaken deletions. If you wish to move changes to a holding space for later access, use stash
(documented here):
git stash
Solution
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
Solution
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.
Solution
If you aren't interested in keeping the unstaged changes (especially if the staged changes are new files), I found this handy:
git diff | git apply --reverse
Solution
As you type git status, (use "git checkout -- ..." to discard changes in working directory) is shown.
e.g. git checkout -- .
Solution
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
Solution
git checkout -f
man git-checkout
:
-f, --force
When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.
Solution
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}
Solution
Tried all the solutions above but still couldn't get rid of new, unstaged files.
Use git clean -f
to remove those new files - with caution though! Note the force option.
Solution
To do a permanent discard:
git reset --hard
To save changes for later:
git stash
Solution
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 :)
Solution
simply say
git stash
It will remove all your local changes. You also can use later by saying
git stash apply
or git stash pop
Solution
you have a very simple git command git checkout .
Solution
No matter what state your repo is in you can always reset to any previous commit:
git reset --hard <commit hash>
This will discard all changes which were made after that commit.
Solution
This works even in directories that are; outside of normal git permissions.
sudo chmod -R 664 ./* && git checkout -- . && git clean -dfx
Happened to me recently
Solution
cd path_to_project_folder # take you to your project folder/working directory
git checkout . # removes all unstaged changes in working directory
Solution
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.
Solution
Final working solution
git restore .
git clean -f
git clean -df (if you have folders in your local changes)
Solution
git checkout .
This will discard any uncommitted changes to the branch. It won't reset it back if any changes were committed. This is handy when you've done some changes and decide you don't want them for some reason and you have NOT committed those changes. It actually just checks out the branch again and discards any current uncommitted changes.
( must be in the app's root or home dir for this to work )
Solution
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).
Solution
I had a weird situation where a file is always unstaged, this helps me to resolve.
git rm .gitattributes
git add -A
git reset --hard
Solution
If it's almost impossible to rule out modifications of the files, have you considered ignoring them? If this statement is right and you wouldn't touch those files during your development, this command may be useful:
git update-index --assume-unchanged file_to_ignore