How to overwrite a local git local branch with a remote

Greg Foster
Greg Foster
Graphite software engineer

Below is a step-by-step guide on how to overwrite your local branch with the remote branch, ensuring that your local copy matches the remote repository exactly.

Before you begin overwriting your local branch, it's crucial to fetch the latest updates from the remote repository. This ensures you have access to the most current data and references.

git fetch origin

This command retrieves updates from the origin but does not merge any changes into your local branches. It updates your remote-tracking branches (origin/main, origin/feature-branch, etc.).

Ensure you are on the branch that you intend to reset to the state of the remote branch. If not, first check out the desired branch with:

git checkout your-branch-name

The git reset command is the most direct way to overwrite your local branch. Use the --hard option to reset the index and working tree. Any changes to tracked files in the working tree since the commit are discarded.

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

This command resets the current branch's head to the specified commit (in this case, the latest commit of the branch on the remote) and possibly updates the index (resetting it to the tree of the specified commit) and the working directory to match.

After a hard reset, there may still be untracked files left in the working directory, which weren’t tracked by Git before the reset (such as build outputs or log files). It's a good practice to clean these out with the git clean command:

git clean -fd

This command removes untracked files (-f for force) and directories (-d) from the working tree.

After resetting and cleaning, verify that your branch is now in sync with the remote:

git status

This command should indicate that your branch is up to date with origin/your-branch-name and that there are no changes to commit.

  • Use caution with git reset --hard: This command can lead to permanent loss of local changes. Always make sure you do not need the local changes anymore before running this command.
  • Backup important changes: Before performing a hard reset, consider stashing or backing up significant changes that you might want to retain.

For further reading see the official Git documentation on the git reset command.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2