Undo Git Pull, How to Bring Repos to Old State
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.
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 thegit 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.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.
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.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.
Stash Your Changes
If you have uncommitted changes, stash them first:
git stash
Find the Commit Before the Pull
Use
git reflog
to find the commit before the pull, as explained earlier.Reset to the Previous Commit
Reset to the commit before the pull:
git reset --hard <commit-hash>
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.
Find the Commit Before the Pull
Use
git reflog
to find the commit hash before the pull.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:
- Identify the commit before the pull using
git reflog
. - Use
git reset --hard <commit-hash>
to reset your branch to that commit, discarding any changes made by the pull. - Optionally, use
git stash
to save uncommitted changes before resetting and then apply them back withgit stash pop
.
These methods allow you to effectively undo a git pull
and revert your repository to its prior state.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github