Open navigation
Back to Articles

July 2025 · 1 min read

How to Reset a Local Git Branch to Match Remote

A quick reference for when your local branch diverges from the remote and you need to discard local changes to return to parity.

Resetting Local Divergence

When my local branch is a mess and I have no local commits worth saving, I use a hard reset to force the branch back to the remote state. This is the fastest way to resolve drift when you want to discard your current history entirely.

First, ensure your local reference to the remote is up to date by fetching. Then, perform the reset to the specific remote branch.

git fetch origin
git reset --hard origin/branch-name

Managing Unsaved Local Work

Hard resets are destructive by definition. If you are working on a branch where you might need your current progress later, never run a reset without first securing your changes.

Before you reset, either stash your current work or create a backup branch to hold your commits. This gives you a safety net if you realize the divergence was actually intended.

git stash
git reset --hard origin/branch-name

Cleaning Untracked Files

A hard reset only affects tracked files. If your repository contains untracked build artifacts, dependencies, or ignored files that have become corrupted, the reset will leave them untouched.

Use the clean command to remove these leftovers. Run the dry-run version first to see exactly what will be deleted from your working directory.

git clean -fdxn
git clean -fdx
gitversion-controlworkflow

Sources

  1. Git - git-push Documentation
  2. I accidentally force pushed to my repo · community · Discussion #23242 · GitHub
  3. Bring your feature branch up to date with master. Deploying from Git branches adds flexibility. Bring your branch up to date with master and deploy it to make sure everything works. If everything looks good the branch can be merged. Otherwise, you can deploy your master branch to return production to its stable state. · GitHub
  4. git - Reset local repository branch to be just like remote repository HEAD - Stack Overflow
  5. How to Reset a Git Branch to a Remote Repository? - GeeksforGeeks
  6. How can I replace a local branch with a remote branch entirely in Git? - Stack Overflow
0
Press ? for keyboard shortcuts