Git Reset: Match Local Branch to Remote Exactly
Reset your local Git branch to exactly match the remote repository using git fetch origin, git reset --hard origin/branch, and git clean -fdx. Discard all local changes safely with step-by-step guide and backups.
How do I reset my local Git branch to match the remote repository exactly?
I tried using git reset --hard HEAD to reset my local branch, but when I run git status, it still shows modified files:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: java/com/mycompany/TestContacts.java
modified: java/com/mycompany/TestParser.java
What is the correct command to make my local branch identical to the remote branch, discarding all local changes?
To make your local branch identical to the remote, fetch the remote refs and hard-reset your branch to the remote ref, then remove untracked/ignored files: git fetch origin → git reset --hard origin/<branch> → git clean -fdx. git reset --hard HEAD only resets to your current local commit (HEAD), so it won’t sync to the remote; back up or stash any work first because this destroys local changes.
Contents
- Quick answer — Reset local branch to match remote (git reset --hard origin/
) - Step-by-step: git fetch, git reset --hard origin/
, git clean -fdx - Handling untracked and ignored files (git clean options)
- Safer alternatives and backups (stash, temp branch, patch)
- Verify & troubleshooting (git status, git log, reflog)
- When not to force-reset (collaboration risks)
- Sources
- Conclusion
Quick answer — Reset local branch to match remote (git reset --hard origin/)
Run these commands (replace <branch> with your branch name, e.g. master or main):
git fetch origin
git reset --hard origin/<branch> # sets HEAD, index and working tree to the remote commit
git clean -fdx # removes untracked files, dirs and ignored files (dangerous)
Why? git fetch updates the remote tracking refs (so origin/<branch> is current). git reset --hard HEAD only resets to the local HEAD commit — it does not move your branch to match the remote. The pattern above is the canonical sequence shown in the StackOverflow answer that people use to make the local branch identical to the remote: https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head.
If your branch already tracks an upstream, you can skip naming origin explicitly and use:
git fetch origin
git reset --hard @{u} # reset to the branch's upstream (short for origin/<branch> if set)
git clean -fdx
Step-by-step: git fetch, git reset --hard origin/, git clean -fdx
- Confirm the branch you want to reset:
git rev-parse --abbrev-ref HEAD # shows current branch
git branch -vv # shows tracking info
- Update remote refs:
git fetch origin
# or, to prune deleted remote branches:
git fetch --prune origin
- Hard-reset the branch to the remote commit:
git reset --hard origin/<branch>
This moves your branch pointer and force-updates index and working tree to match the remote commit, discarding staged and unstaged tracked changes.
4. Remove untracked/ignored files if you want a truly identical working tree:
git clean -nfdx # preview (dry run)
git clean -fdx # actually remove files/directories/ignored files
- Verify:
git status # should show: "nothing to commit, working tree clean"
git rev-parse HEAD
git rev-parse origin/<branch>
Note: if you ran git reset --hard HEAD earlier, that just reset to the local commit; it didn’t pull the remote state down. Fetch first, then reset to origin/<branch].
Handling untracked and ignored files (git clean options)
- Dry-run first:
git clean -nfdxshows what would be removed. Always do this before the real clean. - Common options:
git clean -fd— remove untracked files and directories, but keep ignored files.git clean -fdx— also remove files ignored by .gitignore (use with extreme caution).- If you only want to restore tracked files (no file deletions), use
git restorefor modern Git: git restore .restores tracked files to HEADgit restore --staged <file>unstages a file
The Git Tower guide has a clear summary ofgit restoreandgit cleanbehavior: https://www.git-tower.com/learn/git/faq/git-discard-changes/.
Safer alternatives and backups (git stash, temporary branch, patch)
Don’t want to lose work? A few quick backups:
- Stash everything (including untracked):
git stash push --include-untracked -m "WIP before hard reset"
You can recover later with git stash list and git stash pop or git stash apply.
- Commit to a temporary branch:
git branch backup-$(date +%Y%m%d%H%M)
git add -A
git commit -m "WIP: backup before reset"
- Export a patch:
git diff > /tmp/my-changes.patch
Pick the method that fits how you want to recover — stash is fast for temporary work, a branch commit is safest for preserving history.
Verify & troubleshooting (git status, git log, git reflog)
- Compare hashes:
git rev-parse HEAD git rev-parse origin/<branch>
They should match after the reset.
- See what commits differ:
- Commits on remote not in local:
git log --oneline HEAD..origin/<branch> - Commits on local not in remote:
git log --oneline origin/<branch>..HEAD - A combined view:
git log --oneline --left-right --graph HEAD...origin/<branch> - If you accidentally wipe a commit you later need, check the reflog:
git reflog # find the old HEAD hash
git checkout -b recover <hash>
Reflog is your safety net until garbage collection runs. For other ways to discard or recover changes see this broader StackOverflow discussion: https://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-git-managed-project-to-previous-state.
If the reset doesn’t seem to change files, confirm:
- You fetched (remote ref must be current).
- You reset to the right remote branch (
origin/mainvsorigin/master). - You’re not dealing with submodules, smudge/clean filters, or line-ending differences.
When not to force-reset (collaboration risks)
If the branch is shared and you’ve already pushed commits, don’t rewrite public history without coordinating. Resetting your local branch to match the remote is fine locally, but pushing a forced rewrite (git push --force) can clobber teammates’ work. If you need to undo shared commits, prefer git revert or communicate and use git push --force-with-lease to reduce the risk of overwriting others.
Sources
- Reset local repository branch to be just like remote repository HEAD — StackOverflow
- How to Discard Changes in Git — Git Tower
- How to Replace Local Branch With Remote Branch Entirely — Better Stack Community
- Git hard reset to remote — Graphite guide
- Reset local repository branch to be just like remote repository HEAD — DesignGurus mirror
- Revert all local changes — StackOverflow
- How to discard local changes in Git — TheServerSide article
- How to discard local changes in Git — Graphite guide (alternate)
Conclusion
Short version: fetch the remote and hard-reset to the remote ref, then clean untracked files: git fetch origin && git reset --hard origin/<branch> && git clean -fdx. git reset --hard HEAD only returns you to your local HEAD and won’t sync with the remote. Save or stash any work you care about first — otherwise it’s gone (recovery may be possible via git reflog, but don’t count on it forever).