Data report"State of code review 2024" is now liveRead the full report

How to undo a pull in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


Sometimes you might need to undo a Git pull due to errors, conflicts, or unintended updates. This guide will walk you through the steps to safely undo a pull and revert your repository back to its previous state.

A "pull" in Git is essentially a fetch followed by a merge. This means that when you pull changes from a remote repository, Git tries to merge these changes into your current branch. If the pulled changes cause issues or weren't what you intended, you might want to revert your repository back to its state before the pull.

Here are the methods you can use to undo a pull:

The most straightforward way to undo a pull is by using the git reset command. This command resets your branch to a previous state:

  1. Find the commit before the pull: Use git log to view the commit history and identify the commit hash that represents the state of your branch before the pull. You can use the command:

    Terminal
    git log --pretty=oneline

    Look for the commit right before the merge commit or the first commit from the pull.

  2. Reset to the previous commit: Once you have identified the correct commit hash, use the following command to reset:

    Terminal
    git reset --hard <commit-hash>

    Replace <commit-hash> with the hash of the commit before the pull. This command resets your working directory and index to that commit, discarding any changes that were merged during the pull.

If the pull resulted in a merge commit and you want to preserve the history of what happened, you can use git revert to undo the effects of the pull:

  1. Identify the merge commit: Again, use git log to find the merge commit created by the pull.

  2. Revert the merge commit: Use the following command to revert the merge commit:

    Terminal
    git revert -m 1 <merge-commit-hash>

    Replace <merge-commit-hash> with the hash of the merge commit. The -m 1 option tells Git to keep the parent side of the mainline, which essentially undoes the merge.

For more information see these guides on the git reset command and git revert command.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
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