Categories
git git-checkout revert

How do I revert all local changes in Git managed project to previous state?

2397

I ran git status which told me everything was up to date and there were no local changes.

Then I made several consecutive changes and realized I wanted to throw everything away and get back to my original state. Will this command do it for me?

git reset --hard HEAD

    4207

    To revert changes made to your working copy, do this:

    git checkout .
    

    Or equivalently, for git version >= 2.23:

    git restore .
    

    To revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:

    git reset
    

    To revert a change that you have committed:

    git revert <commit 1> <commit 2>
    

    To remove untracked files (e.g., new files, generated files):

    git clean -f
    

    Or untracked directories (e.g., new or automatically generated directories):

    git clean -fd
    

    19

    • 157

      fwiw after such a long time, git checkout path/to/file will only revert the local changes to path/to/file

      – Matijs

      Aug 22, 2011 at 14:13

    • 32

      +1 on the answers below also mentioning git clean -f (to remove the untracked changes) and -fd (to also remove untracked directories)

      – ptdev

      Jul 5, 2012 at 14:01

    • 16

      git checkout . and git reset [--hard HEAD] didn’t work, I had to do a git clean -fd to revert my changes.

      Jun 16, 2015 at 0:28

    • 13

      git reset doesn’t reset your changes, git reset --hard does that.

      – Cerin

      Jul 6, 2016 at 16:59

    • 4

      git checkout . in the root of my project only applied to the files in that dir, I had to do git checkout * to revert all sub-directories too.

      – danio

      Apr 27, 2017 at 8:30

    469

    Note: You may also want to run

    git clean -fd
    

    as

    git reset --hard
    

    will not remove untracked files, where as git-clean will remove any files from the tracked root directory that are not under git tracking. WARNING – BE CAREFUL WITH THIS! It is helpful to run a dry-run with git-clean first, to see what it will delete.

    This is also especially useful when you get the error message

    ~"performing this command will cause an un-tracked file to be overwritten"
    

    Which can occur when doing several things, one being updating a working copy when you and your friend have both added a new file of the same name, but he’s committed it into source control first, and you don’t care about deleting your untracked copy.

    In this situation, doing a dry run will also help show you a list of files that would be overwritten.

    4

    • 13

      The file clean command is “git clean -f”. Untracked directories are removed with “git clean -d”

      Apr 3, 2011 at 18:22


    • 35

      git clean -fd (force is required for -d)

      Apr 4, 2011 at 18:29

    • 20

      -n or –dry-run are the flags for dry-run.

      Jul 28, 2014 at 22:09

    • 2

      git clean -ffd if you have another git repository in your git repository. Without double f it would not be removed.

      Oct 7, 2016 at 14:31

    243

    Re-clone

    GIT=$(git rev-parse --show-toplevel)
    cd $GIT/..
    rm -rf $GIT
    git clone ...
    
    • ✅ Deletes local, non-pushed commits
    • ✅ Reverts changes you made to tracked files
    • ✅ Restores tracked files you deleted
    • ✅ Deletes files/dirs listed in .gitignore (like build files)
    • ✅ Deletes files/dirs that are not tracked and not in .gitignore
    • 😀 You won’t forget this approach
    • 😔 Wastes bandwidth

    Following are other commands I forget daily.

    Clean and reset

    git clean --force -d -x
    git reset --hard
    
    • ❌ Deletes local, non-pushed commits
    • ✅ Reverts changes you made to tracked files
    • ✅ Restores tracked files you deleted
    • ✅ Deletes files/dirs listed in .gitignore (like build files)
    • ✅ Deletes files/dirs that are not tracked and not in .gitignore

    Clean

    git clean --force -d -x
    
    • ❌ Deletes local, non-pushed commits
    • ❌ Reverts changes you made to tracked files
    • ❌ Restores tracked files you deleted
    • ✅ Deletes files/dirs listed in .gitignore (like build files)
    • ✅ Deletes files/dirs that are not tracked and not in .gitignore

    Reset

    git reset --hard
    
    • ❌ Deletes local, non-pushed commits
    • ✅ Reverts changes you made to tracked files
    • ✅ Restores tracked files you deleted
    • ❌ Deletes files/dirs listed in .gitignore (like build files)
    • ❌ Deletes files/dirs that are not tracked and not in .gitignore

    Notes

    Test case for confirming all the above (use bash or sh):

    mkdir project
    cd project
    git init
    echo '*.built' > .gitignore
    echo 'CODE' > a.sourceCode
    mkdir b
    echo 'CODE' > b/b.sourceCode
    cp -r b c
    git add .
    git commit -m 'Initial checkin'
    echo 'NEW FEATURE' >> a.sourceCode
    cp a.sourceCode a.built
    rm -rf c
    echo 'CODE' > 'd.sourceCode'
    

    See also

    • git revert to make new commits that undo prior commits
    • git checkout to go back in time to prior commits (may require running above commands first)
    • git stash same as git reset above, but you can undo it

    11

    • 1

      I’m pretty sure, that the first option (Re-clone) actually DOES “delete local, non-pushed commits” 🙂

      – Marandil

      May 7, 2017 at 21:28

    • 4

      @styfle ✅ is something it does, ❌ is something it doesn’t do

      May 25, 2017 at 13:07

    • 3

      @FullDecent It’s kind of confusing to read. “❌ Does NOT delete local, non-pushed commits”. That means it doesn’t NOT delete. The double negative means that it does delete?

      – styfle

      May 25, 2017 at 17:24

    • 2

      About -x flag in git clean -f -d -x: if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.- from GIT docs

      – Alex

      Jul 9, 2019 at 8:35

    • 1

      @calyxofheld it doesn’t say it deletes untracked files. ❌ means “it doesn’t do”, ✅ means it does. In Reset part i see “❌ Deletes files/dirs that are not tracked and not in .gitignore”.

      Oct 14, 2020 at 9:31