Read Anthropic’s case study about Graphite Reviewer

How to overwrite local changes when executing a git pull command

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


The git pull command is used to synchronize your local repository with changes from a remote repository by fetching and merging changes from the remote repository to your local branch. However, it's important to understand how to handle local changes during this process, especially when you need to overwrite them or ensure they are not overwritten unintentionally.

The git pull command is essentially a combination of git fetch followed by git merge. It fetches changes from the remote repository and then attempts to merge them into your current branch. The behavior of git pull regarding local changes depends on whether these local changes conflict with the changes being pulled.

By default, git pull does not overwrite unstaged local changes unless there is a conflict between your local changes and the changes coming from the remote repository. If such a conflict exists, Git will not proceed with the merge and will ask you to resolve the conflicts manually.

If you need to discard all local changes (both staged and unstaged) and fully synchronize your local branch with the remote branch, you can reset your branch to the state of the remote branch before pulling:

  1. Fetch the latest changes from the remote:

    Terminal
    git fetch origin
  2. Reset your local branch to match the remote branch:

    For a branch named main, you would use:

    Terminal
    git reset --hard origin/main

    This command resets the current branch's history to match the origin/main, and all local changes will be discarded.

  3. Pull the changes (optional):

    Since you already synchronized your branch with git reset --hard, a git pull is typically not necessary immediately after this unless new changes have been made to the remote in the meantime.

If you wish to fetch and merge remote changes without risking your local changes, you can take the following approach:

  1. Stash your local changes:

    Terminal
    git stash push -m "Saving local changes"
  2. Pull the remote changes:

    Terminal
    git pull origin main
  3. Reapply your stashed changes:

    Terminal
    git stash pop

    After popping the stash, you may need to resolve conflicts between your local changes and the newly pulled changes.

  • If you want to ensure you don’t accidentally overwrite local changes: Always run git status before a pull to check for uncommitted changes and decide whether to stash or commit them before pulling.
  • When unsure about pulling changes: Use git fetch and git diff origin/main to review changes before integrating them into your local branch with git merge.

For further reading, see this guide on the git pull command.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

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