git undo all uncommitted or unsaved changes
Solution 1
-
This will unstage all files you might have staged with
git add
:git reset
-
This will revert all local uncommitted changes (should be executed in repo root):
git checkout .
You can also revert uncommitted changes only to particular file or directory:
git checkout [some_dir|file.txt]
Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
git reset --hard HEAD
-
This will remove all local untracked files, so only git tracked files remain:
git clean -fdx
WARNING:
-x
will also remove all ignored files, including ones specified by.gitignore
! You may want to use-n
for preview of files to be deleted.
To sum it up: executing commands below is basically equivalent to fresh git clone
from original source (but it does not re-download anything, so is much faster):
git reset
git checkout .
git clean -fdx
Typical usage for this would be in build scripts, when you must make sure that your tree is absolutely clean - does not have any modifications or locally created object files or build artefacts, and you want to make it work very fast and to not re-clone whole repository every single time.
Solution 2
If you wish to "undo" all uncommitted changes simply run:
git stash
git stash drop
If you have any untracked files (check by running git status
), these may be removed by running:
git clean -fdx
git stash
creates a new stash which will become stash@{0}. If you wish to check first you can run git stash list
to see a list of your stashes. It will look something like:
stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes
Each stash is named after the previous commit messsage.
Solution 3
What I do is
git add . (adding everything)
git stash
git stash drop
One liner: git add . && git stash && git stash drop
A shorter version as pointed out by M. Justin
git stash -u && git stash drop
Solution 4
there is also git stash
- which "stashes" your local changes and can be reapplied at a later time or dropped if is no longer required
more info on stashing
Solution 5
Adding this answer because the previous answers permanently delete your changes
The Safe way
git stash -u
Explanation: Stash local changes including untracked changes (-u
flag). The command saves your local modifications away and reverts the working directory to match the HEAD commit.
Want to recover the changes later?
git stash pop
Explanation: The command will reapply the changes to the top of the current working tree state.
Want to permanently remove the changes?
git stash drop
Explanation: The command will permanently remove the stashed entry
Comments
-
Antarr Byrd almost 2 years
I'm trying to undo all changes since my last commit. I tried
git reset --hard
andgit reset --hard HEAD
after viewing this post. I responds with head is now at 18c3773... but when I look at my local source all the files are still there. What am I missing?-
jasonleonhard over 7 yearsThis is a great resource straight from Github: How to undo (almost) anything with Git
-
WelcomeTo over 4 yearsEasy to understand and simple to read: git-scm.com/book/en/v2/Git-Basics-Undoing-Things
-
-
Mark Schultheiss almost 8 yearsI did not down vote however your answer is less than clear on your intent; it would help if you were to reword the first sentence in the answer.
-
mvp about 6 yearsI don't see how this relates to question asked. This is just series of strange recipes, without any real substance.
-
EresDev over 5 years@turibe that is right. I had to start my project several times because it was removing all downloaded dependencies and my .idea folder. Need a better solution.
-
EresDev over 5 yearsA good solution indeed but you need to stage changes using
git add .
beforegit stash
because it was showing me uncommited changes even aftergit stash
-
mvp over 5 years@EresDev, you don't have to execute
git clean
, it will undo uncommitted changes but keep all untracked or added files. But, it may affect build outcome because some of untracked files may be interfering. E.g. what if your .idea directory was corrupted? -
Dani over 4 years
git reset
tells me I have changes I need to save -
Madrugada over 4 yearsthis is the best
-
fires3as0n over 4 yearsrunning
git clean -fdx
deleted mynode_modules
and.env
, great -
Rishav over 4 years@fires3as0n What else did you expect? This was bound to happen. There is a warning in the answer too.
-
Dinu Nicolae about 4 yearsNever undo uncommitted changes with reset. It will ruin your other branches in case you rebase.
-
mvp about 4 years@DinuNicolae: it will not ruin other branches. But yes, don't let kids play with knife, they can get hurt. That said, we can't get around without using knife.
-
Dinu Nicolae about 4 years@mvp if you reset and then change branch and rebase on the reseted branch this will hurt. Reset is useful, but should not be used to drop uncommitted changes. In the case of dropping uncommitted changes just stash and drop the stash.
-
Dinu Nicolae about 4 yearsIf you reset and then change branch and rebase on the reseted branch this will hurt. Reset is useful, but should not be used to drop uncommitted changes. In the case of dropping uncommitted changes just stash and drop the stash.
-
Kamil Czerski about 4 yearsfor all those who are googling why "git checkout ." does not work, thank you for remaining that it "should be executed in repo root".
-
Mamun about 4 yearsThis is a life-saver. Thank you
-
esteuart about 3 yearsYou can also do
git stash --include-untracked
to get all files then there's no need to do the clean which gets rid of ignored files. -
M. Justin about 3 yearsThe separate
add
step can be removed if the stash is created to include untracked changes using-u
or--include-untracked
:git stash -u && git stash drop
. -
luis.espinal about 3 years@DinuNicolae - I've never seen this problem. Not that I don't trust your judgement, but I need to see this happening.
-
luis.espinal about 3 yearsDon't rely on UI tools. Learn to use the CLI because when things break (and UI front ends always do), or you are forced to work through a remote SSH connection, the CLI is what will save ya ;)
-
luis.espinal about 3 yearsThis should be the answer.
-
P S Solanki almost 3 years@Mamun did you mean file saver :D ?
-
Aditya Jain over 2 yearsTypo
git add .
-
avdotion over 2 yearsCan I achieve this behaviour without stashing (its slow, excessive)? The answer is really great.
git clean -fxd
,git reset
,git checkout .
do not removeuntracked files
. What this solution does. Moreovergit clean -fxd
removed all my configs and installed modules :( -
avdotion over 2 yearsI'm using
git add -A
,git reset --hard HEAD
. -
M. Justin over 2 years@AdityaJain Stack Overflow is a community curated site, so feel free to edit such obvious errors as you see them. That being said, I've edited this post with the change you suggested.
-
Nishant Dwivedi over 2 yearswhy are you add git clean -fdx
-
Des over 2 yearsBy far the best option that I've found so far.
-
Luxalpa over 2 yearsThe comment about git clean is misleading. Yes, I should have read the warning, yes I am still mad that I lost my .env and some other stuff. But most importantly, I think it's completely missing the point. I want to remove the files that show as unstaged or untracked in
git status
. So this variant ofgit clean
makes no sense for that purpose. -
Luxalpa over 2 years
git stash -u
is better for this purpose. And never rungit clean -fdx
, it will delete files that are gitignored. -
zgabi about 2 yearsThis does not removes the untracked files