How can I replace a local branch with a remote branch entirely in Git?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Replacing a local branch with the remote version is a common cleanup step after a bad rebase, local experiments, or accidental divergence. The operation is simple, but it is destructive for local commits and local working-tree changes, so the safe approach is to verify the branch, optionally create a backup, and then reset intentionally.
Confirm What You Want to Replace
Before using reset --hard, confirm the situation:
- the local branch really should match the remote exactly
- any unpushed local commits are disposable or already backed up
- uncommitted work has been stashed or otherwise preserved
If there is any doubt, create a backup branch first.
That one command gives you a quick escape hatch if you realize afterward that local work mattered after all.
Standard Replacement Workflow
The normal sequence is to fetch the remote state, switch to the branch, and reset the branch tip to the remote-tracking branch.
After this:
- the branch pointer matches
origin/my-branch - the index matches that commit
- tracked files in the working tree match that commit too
This is the standard way to say “make my local branch exactly what the remote currently is.”
Clean Untracked Files Only If You Need a Truly Pristine Tree
git reset --hard does not remove untracked files. If you want your checkout to look like a brand-new clone of that branch, clean those files explicitly.
Be careful with -x, which also removes ignored files. That may delete local caches, environment files, or other machine-specific artifacts you did not intend to destroy.
Protect Yourself With a Quick Inspection Step
If you want a slightly safer workflow, inspect the difference before the reset.
This gives you a final chance to notice that the branch contains useful local work before you throw it away.
Recover Mistakes With reflog
If you reset the wrong branch or regret the replacement immediately afterward, reflog usually still knows where the branch pointed before the reset.
That is why quick recovery is possible after many “oops, wrong hard reset” mistakes. The old tip usually is not gone instantly.
Understand the Difference Between Local Reset and Remote Rewrite
Replacing your local branch with the remote branch changes only your clone. It does not modify the remote repository. That is an important distinction.
If the remote branch itself is wrong and needs rewriting, that is a separate operation and usually involves coordination plus a push such as --force-with-lease. Do not blur those two workflows together.
Use This Pattern in CI and Disposable Workspaces
The same idea is common in automation. CI jobs often want a deterministic working copy with no leftover local history or untracked files from previous runs.
That is a good pattern for disposable workspaces because it removes drift and keeps builds repeatable.
Common Pitfalls
The biggest mistake is running the hard reset without a backup when local commits might still matter. Another is assuming reset --hard removes untracked files, which it does not. Developers also confuse “replace my local branch” with “rewrite the remote branch,” which are very different operations with very different risk.
Summary
- Use
git fetchplusgit reset --hard origin/branchto replace a local branch with the remote version. - Create a backup branch first whenever there is uncertainty about local commits.
- Use
git clean -fdonly when you also need untracked files removed. - Recover accidental resets quickly with
git reflog. - Keep local branch replacement separate from any remote history-rewrite workflow.

