Learning Notes #61 β Undo a git pull
Today, i came across a blog on undo a git pull
. In this blog, i have reiterated the blog in other words.
Mistakes happen. You run a git pull
and suddenly find your repository in a mess. Maybe conflicts arose, or perhaps the changes merged from the remote branch arenβt what you expected.
Fortunately, Gitβs reflog comes to the rescue, allowing you to undo a git pull
and restore your repository to its previous state. Hereβs how you can do it.
Understanding Reflog
Reflog is a powerful feature in Git that logs every update made to the tips of your branches and references. Even actions like resets or rebases leave traces in the reflog. This makes it an invaluable tool for troubleshooting and recovering from mistakes.
Whenever you perform a git pull
, Git updates the branch pointer, and the reflog records this action. By examining the reflog, you can identify the exact state of your branch before the pull and revert to it if needed.
Step By Step Guide to UNDO a git pull
1. Check Your Current State Ensure youβre aware of the current state of your branch. If you have uncommitted changes, stash or commit them to avoid losing any work.
git stash # or git add . && git commit -m "Save changes before undoing pull"
2. Inspect the Reflog View the recent history of your branch using the reflog,
git reflog
This command will display a list of recent actions, showing commit hashes and descriptions. For example,
0a1b2c3 (HEAD -> main) HEAD@{0}: pull origin main: Fast-forward 4d5e6f7 HEAD@{1}: commit: Add new feature 8g9h0i1 HEAD@{2}: checkout: moving from feature-branch to main
3. Identify the Pre-Pull Commit Locate the commit hash of your branchβs state before the pull. In the above example, itβs 4d5e6f7
, which corresponds to the commit made before the git pull
.
4. Reset to the Previous Commit Use the git reset
command to move your branch back to its earlier state,
git reset <commit-hash>
By default, itβs mixed so changes wont be removed but will be in staging.
The next time a pull operation goes awry, donβt panicβlet the reflog guide you back to safety!