Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

Answer from Dan Moulding on Stack Overflow
Top answer
1 of 16
10340

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

2 of 16
748

First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:

git reset --hard @{u}

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).

Next, as needed, use git clean to remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull
๐ŸŒ
Git
git-scm.com โ€บ docs โ€บ git-reset
Git - git-reset Documentation
But you decided that the topic branch is not ready for public consumption yet. "pull" or "merge" always leaves the original tip of the current branch in ORIG_HEAD, so resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the branch to that commit.
Discussions

How to know when to reset hard?
I often do this if I've made some local changes that I know are bad/unnecessary and just want to revert to a completely clean state. More on reddit.com
๐ŸŒ r/git
14
2
August 28, 2023
How do I use 'git reset --hard HEAD' to revert to a previous commit? - Stack Overflow
If you do git reset --hard then Git will: Make your current branch (typically master) back to point at . More on stackoverflow.com
๐ŸŒ stackoverflow.com
git reset hard main VS git pull
Git reset hard will jsut reset your branch to the current head of the remote branch. Git pull will retrieve the latest from the remote. (I would use git fetch --all) Rebasing takes your current branch and puts it on top of the branch your rebasing on, so by definition it re-writes hitory a little. Merging is the best way to conserve all of your history. More on reddit.com
๐ŸŒ r/git
8
0
March 3, 2025
[Git] How to 'git reset' a remote branch?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/learnprogramming
13
5
February 12, 2024
๐ŸŒ
jwiegley
jwiegley.github.io โ€บ git-from-the-bottom-up โ€บ 3-Reset โ€บ 4-doing-a-hard-reset.html
Doing a hard reset | Git from the Bottom Up
Now, if you do a hard reset against an earlier commit, itโ€™s the same as first doing a soft reset and then using reset --hard to reset your working tree. Thus, the following commands are equivalent: $ git reset --hard HEAD~3 # Go back in time, throwing away changes $ git reset --soft HEAD~3 # Set HEAD to point to an earlier commit $ git reset --hard # Wipe out differences in the working tree
๐ŸŒ
Graphite
graphite.com โ€บ guides โ€บ git-hard-reset-remote
Git hard reset to remote - Graphite
After a hard reset, Git modifies the files in your working directory to reflect the state of the commit you've reset to. Any changes that were not staged (tracked but not added to the staging area) or committed will be lost and replaced with the files from the reset commit. To perform a hard reset to a remote branch, you'll typically follow these steps in your terminal:
๐ŸŒ
Atlassian
atlassian.com โ€บ git โ€บ tutorials โ€บ undoing changes โ€บ git reset
Git Reset | Atlassian Git Tutorial
In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too. Put another way: this obliterates all uncommitted changes, so make sure you really want to throw away your local developments before using it. ... Move the current branch tip backward to commit, reset the staging area to match, but leave the working directory alone...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ git-checkout-f-reset-hard-commend-musaddek-ali
The "git checkout -f" and "git reset --hard" commend for Git.
May 4, 2023 - The --hard option stands for "hard ... When you run git reset --hard, Git resets the branch pointer to the specified commit, and any changes made to the files since then are permanently lost....
Find elsewhere
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-hard-reset-in-git
How to Hard Reset in Git ยท CoreUI
October 31, 2025 - # Hard reset to the last commit (HEAD) git reset --hard HEAD # Hard reset to a specific commit git reset --hard abc123f # Hard reset to previous commit git reset --hard HEAD~1 # Hard reset to 3 commits ago git reset --hard HEAD~3 # Hard reset ...
๐ŸŒ
Medium
medium.com โ€บ @elton-martins โ€บ to-reset-a-git-branch-to-match-the-master-main-branch-6692c28a36fc
To reset a Git branch to match the master/main branch | by Elton Martins | Medium
March 15, 2024 - 1. Make sure you are on the branch you want to reset: ... This git reset - -hard command will discard all local changes in your branch and set the branch to the same commit as the remote master branch.
๐ŸŒ
Continuously Merging
articles.mergify.com โ€บ git-reset-hard-origin
A Guide to Git Reset Hard Origin
October 2, 2025 - Key Takeaway: The git reset --hard origin/branch-name command is a powerful tool for developers to sync their local Git branch with a remote repository, discarding any local changes that haven't been committed or pushed.
๐ŸŒ
Medium
medium.com โ€บ @vishalbarvaliya โ€บ resetting-your-git-branch-to-a-previous-commit-a-complete-guide-96cc314a172e
Resetting Your Git Branch to a Previous Commit: A Complete Guide | by Vishal Barvaliya | Medium
September 8, 2024 - Your branch has accumulated multiple unnecessary commits and you want to clean it up. Youโ€™re dealing with a merge conflict and need to revert to a clean state. The `git reset --hard` command is the most direct and forceful way to reset your branch.
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ git-reset
Git Reset | The Git Reset Command Explained - Initial Commit
May 3, 2022 - Git reset merely moved the change out of the staging area because it defaults to the --mixed option, in contrast with --hard which will wipe out the changes in the working directory as well. ... As we saw in the previous section, git reset --mixed is the default mode for git reset, and is used when we are ok with moving changes out of the staging area and back into the working directory. However, as with each git reset mode, we also have the ability to reset the current branch tip to any commit of our choice.
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ git commands โ€บ undoing changes โ€บ git reset --hard
Git Reset --hard - Discard All Local Changes | Coddy
3 weeks ago - ... git reset --hard moves your branch to a target commit and forces both the staging area and your working tree to match it exactly - discarding every uncommitted change along the way.
๐ŸŒ
CICube
cicube.io โ€บ blog โ€บ git-reset-hard
How to use Git Reset --hard: A Complete Guide with Examples | CICube
January 22, 2025 - If you don't, you could reset your work from the version in the local cache rather than getting critical updates made by your coworkers. # DO THIS FIRST git fetch origin # Then reset git reset --hard origin/main
๐ŸŒ
Quora
quora.com โ€บ How-do-you-use-git-reset-hard
How to use git reset --hard - Quora
Move the branch pointer to another commit and make working tree/index match that commit. Clean working tree before switching tasks or applying a diff ... git reset --hard resets the current branchโ€™s commit pointer, the index (staging area), and the working tree so they all match a specified ...
Top answer
1 of 2
1523

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"
    
2 of 2
321

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

๐ŸŒ
GitKraken
gitkraken.com โ€บ home โ€บ learn โ€บ git reset
Git Reset | Hard, Soft & Mixed | Learn Git
March 26, 2026 - GitKraken Client makes it easy ... commit, you will be presented with an option: Reset <branch-name> to this commit > where <branch-name> is the branch currently checked out....
๐ŸŒ
devconnected
devconnected.com โ€บ home โ€บ software engineering โ€บ how to git reset to head
How To Git Reset to HEAD โ€“ devconnected
December 16, 2020 - $ git log --oneline --graph * 802a2ab ... In order to hard reset to the commit right before HEAD, use โ€œgit resetโ€ with the โ€œโ€“hardโ€ option and specify HEAD^....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ git-reset-hard-how-to-reset-to-head-in-git
Git Reset Hard โ€“ How to Reset to Head in Git
April 10, 2023 - So, when we are talking about resetting to HEAD, it means resetting the current branch to the most recent commit. Apart from the HEAD, you can also reset to other commits with the git reset --hard <commit-hash> command.