Today I learned how to revert a Git commit after pulling it on a server. Whether you've made a mistake or just need to backtrack, Git offers several ways to undo commits depending on your situation. Here's a quick guide on how to handle it.
If You Haven't Pushed the Commit Yet
If the commit hasn't been pushed to the remote repository, you can use git reset
to undo it.
Soft Reset (keeps changes staged)
git reset --soft HEAD~1
This moves the HEAD back by one commit but keeps your changes staged.
Mixed Reset (keeps changes unstaged):
git reset --mixed HEAD~1
This moves the HEAD back by one commit and unstages your changes.
Hard Reset (discards changes):
git reset --hard HEAD~1
This completely removes the last commit and discards any changes.
Tip: Replace
HEAD~1
withHEAD~2
to go back two commits, and so on.
If You've Already Pushed the Commit
Revert the Commit (Safe for Shared Repositories):
If you've already pushed the commit and want to undo it without rewriting history, use git revert
. This creates a new commit that undoes the changes.
git revert <commit_hash>
Find the commit hash using:
git log --oneline
Force Reset (If You're Okay Rewriting History):
If you don't mind rewriting history (and no one else is working on the branch), you can force reset.
git reset --hard <commit_hash>
git push origin HEAD --force
⚠️Warning
This is not recommended if others are working on the same branch as it can cause conflicts.
If You Just Pulled and Want to Undo That
If you just pulled and want to undo the merge, you can reset to the previous state using ORIG_HEAD
.
Undo a Merge Pull:
git reset --hard ORIG_HEAD
This will bring your repository back to the state before the last pull.
Final Thoughts
Reverting commits can seem daunting at first, but once you understand the tools Git offers, it's pretty straightforward. Always double-check before using --hard
or --force
to avoid accidental data loss!
Hope this helps you get back on track!