[git] How do I use 'git reset --hard HEAD' to revert to a previous commit?

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:

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?

My next steps were:

git add .
git commit -m "revert"

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

What am I doing right/wrong?

This question is related to git head git-reset git-revert

The answer is


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, as @Paul Betts said, you can do this (beware though - that removes all ignored files too)

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

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? Pandas - Get first row value of a given column Get first element of Series without knowing the index Head and tail in one line How do I use 'git reset --hard HEAD' to revert to a previous commit? How to read first N lines of a file? Is header('Content-Type:text/plain'); necessary at all?

Examples related to git-reset

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'? Git, How to reset origin/master to a commit? Can I delete a git commit but keep the changes? What is the meaning of git reset --hard origin/master? How to git reset --hard a subdirectory? git undo all uncommitted or unsaved changes Unstaged changes left after git reset --hard How do I use 'git reset --hard HEAD' to revert to a previous commit?

Examples related to git-revert

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits How to undo local changes to a specific file How do I use 'git reset --hard HEAD' to revert to a previous commit? What's the difference between Git Revert, Checkout and Reset? Why does git revert complain about a missing -m option? How to revert uncommitted changes including files and folders? Reset all changes after last commit in git How do I revert a Git repository to a previous commit? Undo a particular commit in Git that's been pushed to remote repos