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 reflogto see a history of your actions, including thegit pull.git reflogThe output will look something like this:
abc1234 (HEAD -> master) HEAD@{0}: pull: Fast-forward def5678 HEAD@{1}: commit: Some commit messageHere,
def5678is the commit before the pull.Reset to the Previous Commit
Use
git resetto move the branch pointer back to the commit before the pull.git reset --hard def5678This 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 reflogto 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 messageHere,
abc1234is 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 stashFind the Commit Before the Pull
Use
git reflogto 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 reflogto find the commit hash before the pull.Reset Softly
Use
git resetwith--softor--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 stashto 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.