How to revert uncommitted changes including files and folders?

898,269

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:

  1. git stash This will move your changes to the stash, bringing you back to the state of HEAD
  2. git 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).

Share:
898,269
MEM
Author by

MEM

Updated on July 08, 2022

Comments

  • MEM
    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
      vusan almost 8 years
    • John Little
      John Little over 5 years
      Well, 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
    MEM about 13 years
    hhmm... I did that but my files are still there. Should I do something after ?
  • Josnidhin
    Josnidhin about 13 years
    git 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
    MEM about 13 years
    So, 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
    jpw almost 11 years
    good 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
    Mike K almost 10 years
    When I try this I get "error: pathspec '.' did not match any file(s) known to git.
  • Felipe
    Felipe over 9 years
    what is the difference between this and git reset --hard?
  • divideByZero
    divideByZero over 9 years
    'git reset --hard' will undo both staged and unstaged changes, whereas 'git checkout -- .' will undo only unstaged changes
  • Aaron Campbell
    Aaron Campbell almost 9 years
    Save 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
    galath over 8 years
    git clean -i for an interactive mode.
  • Vinicius Monteiro
    Vinicius Monteiro over 8 years
    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
  • Aron Lorincz
    Aron Lorincz over 8 years
    It didn't reset my unstaged files, I had to stage them first.
  • Alex Lomia
    Alex Lomia about 8 years
    What is the difference between this and git reset HEAD?
  • waldyrious
    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 do git checkout -- :/
  • Adi Prasetyo
    Adi Prasetyo about 8 years
    what is -fdx? "force, directory and x is?
  • Fr0sT
    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
    b0xxed1n almost 8 years
    This does not work for uncommitted changes, only committed changes.
  • Paul D. Eden
    Paul D. Eden almost 8 years
    I have used it for uncommitted changes and it works.
  • Krish Srinivasan
    Krish Srinivasan almost 8 years
    git 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
    T J over 7 years
    @b0xxed1n Stashing is all about uncommitted changes, and obviously it does works for them.
  • Rob
    Rob over 7 years
    git stash was made to save the uncommited changes so you could.. save them without committing.
  • IgorGanapolsky
    IgorGanapolsky over 7 years
    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
    IgorGanapolsky over 7 years
    git stash results in the following error: fatal: git-write-tree: error building trees Cannot save the current index state
  • IgorGanapolsky
    IgorGanapolsky over 7 years
    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:
  • htanata
    htanata over 7 years
    @IgorGanapolsky You're probably in the middle of merge conflict. Try running git merge --abort.
  • mcoolive
    mcoolive over 7 years
    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.
  • yogipriyo
    yogipriyo about 7 years
    save me a headache!
  • fuzzygroup
    fuzzygroup about 7 years
    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!
  • Matthias
    Matthias almost 7 years
    This 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
    winnie damayo over 6 years
    can also use path with this git checkout -- path/file.php
  • Sergiu Mindras
    Sergiu Mindras about 5 years
    that's one way to do it :D
  • phuclv
    phuclv over 4 years
    it's git 2.23, not 2.13
  • falsePockets
    falsePockets over 4 years
    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
    falsePockets over 4 years
    The solution for undoing git rm is git checkout master -- filename
  • Polv
    Polv almost 4 years
    It seems that I can git checkout -- '**/*.md' as well. Just as what I need right now.
  • Santi Rodriguez
    Santi Rodriguez almost 3 years
    Those commands are not meant for that but it works perfectly.
  • Peter Mortensen
    Peter Mortensen almost 2 years
    What is the corresponding (release) date for Git 2.23?
  • Peter Mortensen
    Peter Mortensen almost 2 years
    It works (tried with Git 2.17.1). But why is it different from Zarne Dravitzki's answer?