Is there a Git command to revert all uncommitted changes in a working tree and index and to also remove newly created files and folders?

- 30,738
- 21
- 105
- 131

- 30,529
- 42
- 121
- 191
-
2Possible duplicate of [How do you discard unstaged changes in Git?](http://stackoverflow.com/questions/52704/how-do-you-discard-unstaged-changes-in-git) – vusan May 13 '16 at 10:28
-
2Well, I have read all of the varied and difficult to remember answers below, with their caveats and edge cases and "didnt work if you have xxx", and have stuck with deleting the entire repo, cloning it to remove all edited and added files. Is also only two commands. rm -r projectdir; git clone xxx. For me this is a frequent operation - check out a repo play around with it, then want to get back to a clean checkout so I can start modifying it. Not great, but works 100%. Hoping one day they will add a simple command for this. – John Little Aug 16 '18 at 22:07
14 Answers
You can run these two commands:
# Revert changes to modified files.
git reset --hard
# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd
-
164good idea to run 'git clean -nd' to preview the changes before running git clean to ensure you dont have untracked files or directories that you care about that will be removed. – jpw Jul 14 '13 at 05:13
-
87Save someone a trip to the docs: -f is force, -d is remove directories, -n is dry run (also --dry-run; show output without doing anything yet) – Aaron Campbell Jun 04 '15 at 21:07
-
17
-
-
This results in an error when merging: **fatal: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you merge.** – IgorGanapolsky Oct 12 '16 at 17:06
-
5@IgorGanapolsky You're probably in the middle of merge conflict. Try running `git merge --abort`. – htanata Oct 12 '16 at 20:46
If you want to revert the changes only in the current working directory, use
git checkout -- .
And before that, you can list the files that will be reverted without actually making any action, just to check what will happen, with:
git checkout --

- 30,738
- 21
- 105
- 131

- 7,166
- 2
- 18
- 14
-
1When I try this I get "error: pathspec '.' did not match any file(s) known to git. – Mike K May 16 '14 at 17:12
-
40
-
119'git reset --hard' will undo both staged and unstaged changes, whereas 'git checkout -- .' will undo only unstaged changes – divideByZero Oct 30 '14 at 10:36
-
But if you use checkout and you have modified files, the cmd will return that I need do the merge, even when I just need revert this changes – Vinicius Monteiro Dec 16 '15 at 11:47
-
-
9git checkout -- will simply list the files that will be reverted (no action, just list). this is useful if you want to see what files will be affected before doing git checkout -- . – Krish Srinivasan Aug 03 '16 at 15:27
-
-
I'm working with a pretty complex source code model where I'm pulling in functionality from other git repos via shell scripts. This morning I got it wrong and a bunch of code disappeared. git checkout -- . cleaned up my problem perfectly. Thanks! – fuzzygroup Apr 20 '17 at 09:34
-
-
Use "git checkout -- ..." to discard changes in working directory
git checkout -- app/views/posts/index.html.erb
or
git checkout -- *
removes all changes made to unstaged files in git status eg
modified: app/controllers/posts.rb
modified: app/views/posts/index.html.erb

- 1,648
- 1
- 16
- 15
-
8`git checkout -- *` doesn't work for me unless I'm in the directory where the changed files are located. To checkout all files across the whole repository, you must do `git checkout -- :/` – waldyrious Apr 04 '16 at 19:02
-
In `git checkout -- *`, the star is replaced by the Shell, with all files and directories in the current directory. So it should go in subdirectories. It works for me. But thanks to highlight the syntax ":/" that seams cleaner in my opinion. – mcoolive Nov 30 '16 at 12:40
-
It seems that I can `git checkout -- '**/*.md'` as well. Just as what I need right now. – Polv Jun 19 '20 at 02:15
One non-trivial way is to run these two commands:
git stash
This will move your changes to the stash, bringing you back to the state of HEADgit stash drop
This will delete the latest stash created in the last command.
-
3
-
4
-
10@b0xxed1n Stashing is all about uncommitted changes, and obviously it does works for them. – T J Aug 25 '16 at 06:09
-
1git stash was made to save the uncommited changes so you could.. save them without committing. – Rob Sep 07 '16 at 12:01
-
**git stash** results in the following error: `fatal: git-write-tree: error building trees Cannot save the current index state` – IgorGanapolsky Oct 12 '16 at 17:07
-
-
-
this option worked like a charm for my use-case. I had not done any 'git add' nor 'git commit' on the modified code. I discovered I went down the wrong rabbit hole and just needed to go back to end state of last commit. Easy peasy. – Energetic Pixels Apr 09 '23 at 21:07
Git 2.23 introduced the git restore
command to restore working tree files.
To restore all files in the current directory:
git restore .
If you want to restore all C source files to match the version in the index, you can do
git restore '*.c'

- 30,738
- 21
- 105
- 131

- 20,103
- 19
- 89
- 125
-
1it's [git 2.23](https://www.infoq.com/news/2019/08/git-2-23-switch-restore/), not 2.13 – phuclv Sep 03 '19 at 04:22
-
-
To restore a specific file then include the file name as replacement for . e.g `git restore index.js` – Oluwatobi Samuel Omisakin Dec 29 '22 at 11:01
-
For some reason, this solution with `git restore .` worked for me where-as those with `git checkout` didn't. – NoseKnowsAll Mar 02 '23 at 21:02
git clean -fd
didn't help, and new files remained. I totally deleted all the working tree and then
git reset --hard
See "https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420" for advice to add the -x
option to clean:
git clean -fdx
Note -x
flag will remove all files ignored by Git, so be careful (see the discussion in the answer I refer to).

- 30,738
- 21
- 105
- 131

- 2,959
- 2
- 25
- 18
-
-
@AdiPrasetyo -x flag removes all ignored files as well; it might be an undesired effect so I updated my answer. – Fr0sT May 04 '16 at 06:41
-
I still cannot rebase. I get the error: **error: Failed to merge in the changes. Patch failed at 0003 Create calling back to The copy of the patch that failed is found in:** – IgorGanapolsky Oct 12 '16 at 17:08
If you have an uncommitted change (it’s only in your working copy) that you wish to revert to the copy in your latest commit, do the following:
git checkout filename

- 30,738
- 21
- 105
- 131

- 5,507
- 5
- 39
- 69
-
I'm trying this after an uncommitted `git rm filename`, and it's not working. `error: pathspec 'filename' did not match any file(s) known to git.` – falsePockets Dec 09 '19 at 22:18
-
The solution for undoing `git rm` is `git checkout master -- filename` – falsePockets Dec 09 '19 at 22:19
-
It works (tried with Git 2.17.1). But why is it different from [Zarne Dravitzki's answer](https://stackoverflow.com/questions/5807137/how-can-i-revert-uncommitted-changes-including-files-and-folders/15375309#15375309)? – Peter Mortensen May 14 '22 at 22:17
I think you can use the following command: git reset --hard
-
hhmm... I did that but my files are still there. Should I do something after ? – MEM Apr 27 '11 at 16:22
-
2git reset only reverts the uncommited changes in the working tree. It will not remove the new files and folders. I am not sure how to do that with git – Josnidhin Apr 27 '11 at 16:38
-
1So, if we change a system directory by adding new files and folders, and then we want to revert that directory to a previous state (w/out those files and folders), we cannot do that with git ? So the best we can is to revert file states ? But once we create a file, we can't remove that file unless we do it manually ? – MEM Apr 27 '11 at 17:37
Please note that there might still be files that won't seem to disappear - they might be unedited, but Git might have marked them as being edited because of CRLF / LF changes. See if you've made some changes in .gitattributes
recently.
In my case, I've added CRLF settings into the .gitattributes
file and all the files remained in the "modified files" list because of this. Changing the .gitattributes settings made them disappear.

- 30,738
- 21
- 105
- 131

- 4,927
- 4
- 26
- 41
-
me too. Following an attempt to renormalize my line endings, I had an error in my gitattributes, which made "git reset --hard" only unstage and not undo the modifications – timB33 Feb 09 '23 at 11:35
You can just use following Git command which can revert back all the uncommitted changes made in your repository:
git checkout .
Example:
ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
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: application/controllers/Drivers.php
modified: application/views/drivers/add.php
modified: application/views/drivers/load_driver_info.php
modified: uploads/drivers/drivers.xlsx
no changes added to commit (use "git add" and/or "git commit -a")
ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .
ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

- 30,738
- 21
- 105
- 131

- 5,818
- 48
- 50
From Git help:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)

- 83
- 2
- 8
I usually use this way that works well:
mv fold/file /tmp
git checkout fold/file

- 923
- 13
- 25
-
1This is exactly the same what the guy with 357 "likes" proposed. Only that you even do create a backup of the newly checked out file. – Matthias May 11 '17 at 07:29
A safe and long way:
git branch todelete
git checkout todelete
git add .
git commit -m "I did a bad thing, sorry"
git checkout develop
git branch -D todelete

- 158,662
- 42
- 215
- 303

- 51
- 5
Use:
git reset HEAD filepath
For example:
git reset HEAD om211/src/META-INF/persistence.xml

- 158,662
- 42
- 215
- 303

- 173
- 12