keepcalculator

06 Numbers and Powers

Git Overwrite Local Branch with Remote: How-To

Card No. 06·37885 · Updated · Standard formula, shown below

To overwrite your local branch with the remote version, run git fetch origin followed by git reset --hard origin/<branch>. This discards all local commits and changes on that branch and makes it identical to the remote branch.

Why You Might Need This

You usually need to overwrite a local branch when it has diverged from the remote and you no longer care about your local changes. Common situations include:

  • You committed to the wrong branch and want to discard those commits.
  • You experimented with code that broke things and want a clean slate.
  • You accidentally merged or rebased and want to undo the result.
  • You want to sync a local branch to match a teammate’s force-pushed remote branch.

This operation is destructive: it permanently deletes uncommitted changes and local commits that are not on the remote. Before running it, make sure you have a backup or a stash if there is anything you might need.

How It Works: Fetch and Reset

The command uses two Git operations in sequence:

  • git fetch origin downloads the latest commits and branch references from the remote repository into your local repository, but it does not modify your working directory or current branch.
  • git reset --hard origin/<branch> moves the current branch pointer to the commit that origin/<branch> points to, and then updates the working directory and staging area to match that commit exactly.

For example, if you are on branch main and want to overwrite it with the remote version:

git fetch origin
git reset --hard origin/main

After this, your local main will point to the same commit as origin/main, and all local changes (tracked files) that were not on the remote will be gone.

How to Choose the Right Command

There are several ways to sync a local branch with a remote, and the best one depends on what you want to keep:

  • Discard everything local: Use git reset --hard origin/<branch> after fetching. This is the fastest and most thorough method.
  • Keep uncommitted changes but discard commits: Use git reset --soft origin/<branch> to move the branch pointer without touching your working directory. Your changes will appear as staged changes.
  • Keep commits but apply them on top of remote: Use git rebase origin/<branch> to replay your local commits onto the remote branch. This is useful if you want to preserve your work.
  • Merge remote into local: Use git merge origin/<branch> to combine the histories. This is the safest option when you want to keep both sets of changes.

For most cases where you want to “overwrite” completely, reset --hard is the correct choice.

Common Mistakes and Pitfalls

Here are frequent errors people make when trying to overwrite a local branch:

  • Forgetting to fetch: If you run git reset --hard origin/main without fetching, you will reset to the last known state of the remote branch from your local cache, which may be outdated.
  • Being on the wrong branch: The reset command affects the currently checked-out branch. If you are on feature and run git reset --hard origin/main, you will overwrite feature with the remote main branch, not update main itself.
  • Losing untracked files: git reset --hard only affects tracked files. Untracked files (new files not added to Git) are left alone. To remove those too, you need git clean -fd.
  • Assuming it updates the remote: This command only changes your local branch. To update the remote with your new local state, you would need to force-push, but that is a separate action.

What About Force Pushing?

Sometimes you want the opposite: overwrite the remote branch with your local branch. That is done with git push --force (or --force-with-lease). The --force-with-lease option is safer because it checks that the remote branch has not changed since you last fetched, preventing you from clobbering someone else’s work.

Force pushing is often necessary after a rebase or when you have rewritten history. However, it can cause problems for teammates who have based their work on the old commits. Always coordinate with your team before force pushing to a shared branch.

How to Recover If You Made a Mistake

If you accidentally overwrote your local branch and need to recover, there is still hope if you know the commit hash of the lost state. You can use git reflog to see a history of where your branch pointer has been. For example:

git reflog

Find the commit hash that corresponds to your previous local state, then create a new branch pointing to it:

git branch recovery <commit-hash>

You can then inspect that branch and cherry-pick or merge as needed. Reflog entries expire after a few months (default 90 days), so recovery is not guaranteed forever.

Alternatives to Overwriting

If you want to avoid the destructive nature of a hard reset, consider these alternatives:

  • Stash your changes: Use git stash to temporarily set aside uncommitted changes, then pull or reset, and later apply the stash.
  • Create a backup branch: Before resetting, run git branch backup to save your current local state. This gives you a safety net.
  • Use a fresh clone: If the local repository is heavily corrupted or you want a completely clean copy, cloning the remote into a new directory is the simplest option.

Frequently Asked Questions

Will this delete my uncommitted changes?

Yes, git reset --hard discards all uncommitted changes to tracked files. Untracked files are not touched, but they can be removed with git clean.

What if I want to overwrite multiple branches at once?

You must do them one at a time. Checkout each branch and run the fetch and reset commands for that branch.

Does this affect the remote repository?

No, the commands only change your local repository. The remote stays exactly as it is.

Can I undo a hard reset?

If you know the commit hash of your previous state, you can recover using git reflog and git branch. If you don’t know the hash, recovery may be impossible.

Is there a difference between git reset --hard and git pull?

Yes. git pull fetches and merges (or rebases) the remote branch into your current branch, preserving your local commits. git reset --hard discards local commits and makes your branch exactly match the remote.

What is the safest way to overwrite a local branch?

The safest way is to first create a backup branch, then fetch and reset. This way you can always return to your previous state if needed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →