Live streamJoin us for "State of code review 2024" on July 18thRegister today

Git force pull

Greg Foster
Greg Foster
Graphite software engineer


Note

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


While git doesn’t actually have a git pull --force command, it’s possible to overwrite your local branch to pull upstream changes by jumping to the latest commit on origin/main.

The first step in “force pulling” your local branch is to fetch the latest changes from the remote:

Terminal
git fetch --all

The fetch command fetches all of the latest upstream changes but does not merge them with your local branches.

While this step is not necessary, it’s highly recommended to mitigate any unintentional data loss.

Create a copy of the branch you intend on “force pulling”:

Terminal
git branch <backup_branch_name>

This will preserve a copy of the current state of the local branch just in case you delete anything unintentionally.

Next use the git reset command with the --hard flag to reset your local index and working tree, and point them to the latest commit of your remote branch.

Another warning: this is a destructive operation. Per the documentation:

Any changes to tracked files in the working tree since <commit> are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.

Once you are ready to overwrite your existing local branch, run:

Terminal
git reset --hard origin/<branch_name>

This command resets your current branch to exactly match the remote branch, discarding any local changes or commits.

In cases where you want to keep your local changes while overwriting the rest of the branch with the latest changes you can use git stash. This command saves all your local changes and reverts the working directory to match the HEAD commit.

In order to save your local changes first run:

Terminal
git stash

Then, once you’ve completed your “force pull” according to the previous steps, run:

Terminal
git stash pop

This command re-applies your local changes on top of the current working tree state. This may result in merge conflicts between the stashed changes and the current local state.

Once you have resolved these conflicts, you're all set. You've force pulled remote changes into your local branch while preserving your local changes.

For further reading on these operations please see the official documentation on fetch, reset, branch, and stash.

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