Search This Blog

Labels

adobe (1) android (3) apache (3) aviation (1) bash (2) chrome (2) composer (1) cookery (3) dev (3) dodanperks (1) extensions (1) facebook (2) firefox (1) git (3) grafana (1) guzzle (1) headaches (12) htaccess (1) html5 (2) jquery (2) lamp (1) life hacks (10) linux (29) mysqli (2) native (1) opera (2) php (10) railfanning (1) reactjs (3) reactnative (1) servers (11) sinhala (1) smartphones (2) snap (1) sound (1) tech (22) troubleshoots (5) ubuntu (30) unicode (4) virtualbox (1) wamp (2) web (11) windows (4) wordpress (3) youtube (2) කෑම (3)

Thursday, 9 October 2025

Revert a pushed GIT commit

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