Read Anthropic’s case study about Graphite Reviewer

How to overwrite a local git local branch with a remote

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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.

Terminal
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.).

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

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:

Terminal
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.

Terminal
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:

Terminal
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:

Terminal
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.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
  • 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.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2