git undo all uncommitted or unsaved changes

967,072

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

Link to git stash documentation

Share:
967,072
Antarr Byrd
Author by

Antarr Byrd

Building awesome things using Ruby on Rails.

Updated on July 15, 2022

Comments

  • Antarr Byrd
    Antarr Byrd almost 2 years

    I'm trying to undo all changes since my last commit. I tried git reset --hard and git 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?

  • Mark Schultheiss
    Mark Schultheiss almost 8 years
    I 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
    mvp about 6 years
    I don't see how this relates to question asked. This is just series of strange recipes, without any real substance.
  • EresDev
    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
    EresDev over 5 years
    A good solution indeed but you need to stage changes using git add . before git stash because it was showing me uncommited changes even after git stash
  • mvp
    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
    Dani over 4 years
    git reset tells me I have changes I need to save
  • Madrugada
    Madrugada over 4 years
    this is the best
  • fires3as0n
    fires3as0n over 4 years
    running git clean -fdx deleted my node_modules and .env, great
  • Rishav
    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
    Dinu Nicolae about 4 years
    Never undo uncommitted changes with reset. It will ruin your other branches in case you rebase.
  • mvp
    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
    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
    Dinu Nicolae about 4 years
    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.
  • Kamil Czerski
    Kamil Czerski about 4 years
    for all those who are googling why "git checkout ." does not work, thank you for remaining that it "should be executed in repo root".
  • Mamun
    Mamun about 4 years
    This is a life-saver. Thank you
  • esteuart
    esteuart about 3 years
    You 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
    M. Justin about 3 years
    The 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
    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
    luis.espinal about 3 years
    Don'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
    luis.espinal about 3 years
    This should be the answer.
  • P S Solanki
    P S Solanki almost 3 years
    @Mamun did you mean file saver :D ?
  • Aditya Jain
    Aditya Jain over 2 years
    Typo git add .
  • avdotion
    avdotion over 2 years
    Can I achieve this behaviour without stashing (its slow, excessive)? The answer is really great. git clean -fxd, git reset, git checkout . do not remove untracked files. What this solution does. Moreover git clean -fxd removed all my configs and installed modules :(
  • avdotion
    avdotion over 2 years
    I'm using git add -A, git reset --hard HEAD.
  • M. Justin
    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
    Nishant Dwivedi over 2 years
    why are you add git clean -fdx
  • Des
    Des over 2 years
    By far the best option that I've found so far.
  • Luxalpa
    Luxalpa over 2 years
    The 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 of git clean makes no sense for that purpose.
  • Luxalpa
    Luxalpa over 2 years
    git stash -u is better for this purpose. And never run git clean -fdx, it will delete files that are gitignored.
  • zgabi
    zgabi about 2 years
    This does not removes the untracked files