git reset --hard HEAD~
git push -f
git reset --hard HEAD~1
: What it does
This command forcefully resets your current branch to the commit just before the latest one (HEAD~1
), and:
- Moves the HEAD pointer back one commit.
- Updates the index (staging area) to match that previous commit.
- Overwrites your working directory to match that previous commit — meaning any changes in the latest commit and any uncommitted changes are lost.
In short, it erases the last commit and everything in it, as if it never happened.
Difference from git revert
Feature | git reset --hard HEAD~1 |
git revert HEAD |
---|---|---|
Action | Deletes the last commit | Creates a new commit that undoes the last one |
History | Rewrites history (destructive) | Preserves history (non-destructive) |
Safety | Risky — can lose data | Safe — doesn't remove any commits |
Collaboration | Not recommended on shared branches | Safe for shared branches |
git revert
is like saying “Oops, let me undo that” — but it keeps a record of the mistake.git reset --hard
is like saying “Let’s pretend that never happened” — and it wipes it clean.
If you're working solo and want a clean slate, reset --hard
is fine. But if you're collaborating, revert
is the safer choice.
Want to see an example of each in action?
No comments:
Post a Comment