How to revert uncommitted changes including files and folders?
Solution 1
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
Solution 2
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 --
Solution 3
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
Solution 4
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.
Solution 5
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).
MEM
Updated on July 08, 2022Comments
-
MEM almost 2 years
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?
-
vusan almost 8 yearsPossible duplicate of How do you discard unstaged changes in Git?
-
John Little over 5 yearsWell, 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.
-
-
MEM about 13 yearshhmm... I did that but my files are still there. Should I do something after ?
-
Josnidhin about 13 yearsgit 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
-
MEM about 13 yearsSo, 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 ?
-
jpw almost 11 yearsgood 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.
-
Mike K almost 10 yearsWhen I try this I get "error: pathspec '.' did not match any file(s) known to git.
-
Felipe over 9 yearswhat is the difference between this and
git reset --hard
? -
divideByZero over 9 years'git reset --hard' will undo both staged and unstaged changes, whereas 'git checkout -- .' will undo only unstaged changes
-
Aaron Campbell almost 9 yearsSave 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)
-
galath over 8 years
git clean -i
for an interactive mode. -
Vinicius Monteiro over 8 yearsBut 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
-
Aron Lorincz over 8 yearsIt didn't reset my unstaged files, I had to stage them first.
-
Alex Lomia about 8 yearsWhat is the difference between this and
git reset HEAD
? -
waldyrious about 8 years
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 dogit checkout -- :/
-
Adi Prasetyo about 8 yearswhat is -fdx? "force, directory and x is?
-
Fr0sT about 8 years@AdiPrasetyo -x flag removes all ignored files as well; it might be an undesired effect so I updated my answer.
-
b0xxed1n almost 8 yearsThis does not work for uncommitted changes, only committed changes.
-
Paul D. Eden almost 8 yearsI have used it for uncommitted changes and it works.
-
Krish Srinivasan almost 8 yearsgit 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 -- .
-
T J over 7 years@b0xxed1n Stashing is all about uncommitted changes, and obviously it does works for them.
-
Rob over 7 yearsgit stash was made to save the uncommited changes so you could.. save them without committing.
-
IgorGanapolsky over 7 yearsThis results in an error when merging: fatal: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you merge.
-
IgorGanapolsky over 7 yearsgit stash results in the following error:
fatal: git-write-tree: error building trees Cannot save the current index state
-
IgorGanapolsky over 7 yearsI 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:
-
htanata over 7 years@IgorGanapolsky You're probably in the middle of merge conflict. Try running
git merge --abort
. -
mcoolive over 7 yearsIn
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. -
yogipriyo about 7 yearssave me a headache!
-
fuzzygroup about 7 yearsI'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!
-
Matthias almost 7 yearsThis 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.
-
winnie damayo over 6 yearscan also use path with this git checkout -- path/file.php
-
Sergiu Mindras about 5 yearsthat's one way to do it :D
-
phuclv over 4 yearsit's git 2.23, not 2.13
-
falsePockets over 4 yearsI'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 over 4 yearsThe solution for undoing
git rm
isgit checkout master -- filename
-
Polv almost 4 yearsIt seems that I can
git checkout -- '**/*.md'
as well. Just as what I need right now. -
Santi Rodriguez almost 3 yearsThose commands are not meant for that but it works perfectly.
-
Peter Mortensen almost 2 yearsWhat is the corresponding (release) date for Git 2.23?
-
Peter Mortensen almost 2 yearsIt works (tried with Git 2.17.1). But why is it different from Zarne Dravitzki's answer?