Undo Git Pull, How to Bring Repos to Old State

Better Stack Team
Updated on August 12, 2024

If you have performed a git pull and want to undo it, bringing your repository back to its previous state, you can use one of several approaches depending on the exact situation. Here are the methods to undo a git pull:

Undoing a git pull with git reset

Case 1: Simple Undo (No Merge Conflicts)

If the git pull resulted in a fast-forward merge or a clean merge without conflicts, you can reset your branch to the previous state.

  1. Find the Commit Before the Pull

    First, find the commit hash before the pull. You can use git reflog to see a history of your actions, including the git pull.

     
    git reflog
    

    The output will look something like this:

     
    abc1234 (HEAD -> master) HEAD@{0}: pull: Fast-forward
    def5678 HEAD@{1}: commit: Some commit message
    

    Here, def5678 is the commit before the pull.

  2. Reset to the Previous Commit

    Use git reset to move the branch pointer back to the commit before the pull.

     
    git reset --hard def5678
    

    This command will discard all changes made by the pull and bring your repository back to the state of commit def5678.

Case 2: Pull with Merge Commit

If the pull resulted in a merge commit (not a fast-forward), you can identify the merge commit and reset to the commit before it.

  1. Find the Merge Commit

    Again, use git reflog to identify the merge commit. For example:

     
    abc1234 (HEAD -> master) HEAD@{0}: merge origin/master: Merge made by the 'recursive' strategy.
    def5678 HEAD@{1}: commit: Some commit message
    

    Here, abc1234 is the merge commit created by the pull.

  2. Reset to the Previous Commit

    Reset to the commit before the merge commit:

     
    git reset --hard def5678
    

Undoing a git pull with Uncommitted Changes

If there were uncommitted changes before the pull, you may want to preserve these changes.

  1. Stash Your Changes

    If you have uncommitted changes, stash them first:

     
    git stash
    
  2. Find the Commit Before the Pull

    Use git reflog to find the commit before the pull, as explained earlier.

  3. Reset to the Previous Commit

    Reset to the commit before the pull:

     
    git reset --hard <commit-hash>
    
  4. Apply the Stashed Changes

    Finally, apply the stashed changes:

     
    git stash pop
    

Undoing a Pull in a Non-Hard Reset Way

If you want to keep your working directory changes and just want to undo the commits brought by the pull, you can use git reset without the --hard option.

  1. Find the Commit Before the Pull

    Use git reflog to find the commit hash before the pull.

  2. Reset Softly

    Use git reset with --soft or --mixed:

     
    git reset --mixed <commit-hash>
    
 
- `-soft`: Keeps the changes in the staging area.
- `-mixed`: Keeps the changes in the working directory (default behavior).

Summary

To undo a git pull and return your repository to its previous state:

  1. Identify the commit before the pull using git reflog.
  2. Use git reset --hard <commit-hash> to reset your branch to that commit, discarding any changes made by the pull.
  3. Optionally, use git stash to save uncommitted changes before resetting and then apply them back with git stash pop.

These methods allow you to effectively undo a git pull and revert your repository to its prior state.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github