1283

I know that Git tracks changes I make to my application, and holds on to them until I commit the changes.

To revert to a previous commit, I used:

$ git reset --hard HEAD

HEAD is now at 820f417 micro

How do I then revert the files on my hard drive back to that previous commit?

My next steps were:

git add .
git commit -m "revert"

But none of the files have changed on my hard drive...

3
  • 2
    What do you mean by reverting the files on my hard drive back to that previous commit? If 820f417 is your desired commit, the files should now have the exact content in that commit.
    – kennytm
    Commented Mar 2, 2012 at 6:40
  • 6
    If you want to undo all changes, after git reset --hard, you should git checkout <branch>.
    – jweyrich
    Commented Mar 2, 2012 at 7:20
  • 14
    I really don't get the idea of [duplicate] then ask a new question, when the answers are not satisfactory. It's a recipe for disaster in terms of more duplicates....
    – user2066805
    Commented Apr 13, 2016 at 21:37

2 Answers 2

1477

First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it.

Initially you say the following:

So I know that Git tracks changes I make to my application, and it holds on to them until I commit the changes, but here's where I'm hung up:

That's incorrect. Git only records the state of the files when you stage them (with git add) or when you create a commit. Once you've created a commit which has your project files in a particular state, they're very safe, but until then Git's not really "tracking changes" to your files. (for example, even if you do git add to stage a new version of the file, that overwrites the previously staged version of that file in the staging area.)

In your question you then go on to ask the following:

When I want to revert to a previous commit I use: git reset --hard HEAD And git returns: HEAD is now at 820f417 micro

How do I then revert the files on my hard drive back to that previous commit?

If you do git reset --hard <SOME-COMMIT> then Git will:

  • Make your current branch (typically master) back to point at <SOME-COMMIT>.
  • Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT>.

HEAD points to your current branch (or current commit), so all that git reset --hard HEAD will do is to throw away any uncommitted changes you have.

So, suppose the good commit that you want to go back to is f414f31. (You can find that via git log or any history browser.) You then have a few different options depending on exactly what you want to do:

  • Change your current branch to point to the older commit instead. You could do that with git reset --hard f414f31. However, this is rewriting the history of your branch, so you should avoid it if you've shared this branch with anyone. Also, the commits you did after f414f31 will no longer be in the history of your master branch.
  • Create a new commit that represents exactly the same state of the project as f414f31, but just adds that on to the history, so you don't lose any history. You can do that using the steps suggested in this answer - something like:

    git reset --hard f414f31
    git reset --soft HEAD@{1}
    git commit -m "Reverting to the state of the project at f414f31"
    
15
  • 1
    git reset --soft HEAD@{1} really messed up my local repository. It thinks all the files are now. What do I do now? Commented Oct 26, 2012 at 13:24
  • 2
    @Mark Longair: the answer you point to at the end does not user --hard, but instead simply git reset.
    – MiniQuark
    Commented Mar 28, 2013 at 14:18
  • 3
    @MiniQuark: I'm using the equivalent and slightly shorter variant that I suggested in the comments on the linked answer. Commented Jul 22, 2013 at 6:59
  • 7
    Thanks for the tip about git reset --soft HEAD@{1}. I haven't thought of that way of reverting commits, even though I'm experienced in git. Commented Jul 20, 2014 at 10:23
  • 6
    One can also consult git reflog in the event that git log does not immediately show the relevant commit you wish to revert to. Commented Aug 16, 2016 at 16:36
309

WARNING: git clean -f will remove untracked files, meaning they're gone for good since they aren't stored in the repository. Make sure you really want to remove all untracked files before doing this.


Try this and see git clean -f.

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.

Alternatively, you can do the following (beware though - that removes all ignored files too)

  • git clean -df
  • git clean -xdf CAUTION! This will also delete ignored files

Explanation of Flags:

-d deletes all files in directories recursively

-f

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.

-x Don't use standard ignore rules but ones specified by -e. This can be used to start a clean build.

Source: Man pages

15
  • 32
    This is the first git command I've come across so far that's irreversible... added a warning so others don't get too trigger happy... Commented Apr 1, 2014 at 21:41
  • 32
    Use n instead of f first, to see a list of what will be removed, e.g: git clean -xdn
    – Zantier
    Commented Jun 19, 2014 at 11:27
  • 6
    remember to be careful with clean because git clean -f will remove any environment/gui project folders (e.g. .idea (phpstorm) or .vagrant (vagrant))
    – timhc22
    Commented Nov 21, 2014 at 12:42
  • 169
    Goddamnit, git clean -xdf even removes all ignored files!!!
    – leymannx
    Commented May 30, 2017 at 8:41
  • 16
    git clean -df is enough. The -x flag ignores the .gitignore file.
    – xji
    Commented Jul 8, 2018 at 19:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.