git undo commits example |Video upload date:  · Duration: PT6M28S  · Language: EN

Practical guide to undo commits in Git using reset revert and reflog with safety notes and recovery tips for shared branches.

Quick truth about undoing commits

You made a commit and then promptly regretted everything. Happens to the best of us and to many of us repeatedly. This short Git tutorial walks through safe and destructive ways to undo commits and how to rescue work that wandered off a cliff. We cover git reset and git revert plus how to use reflog to find a lost commit. Read on before you rewrite shared history and earn a collection of angry messages.

Inspect history first

Before you perform any heroics look at the commit history so you know which commit needs mercy. Run a compact log to find the hash and confirm the target.

git log --oneline

Keep calm and copy the SHA you need. Mistakes are reversible as long as you know where to look.

Undo last commit but keep your changes staged

If you only need to fix the commit message or add a missing file use a soft reset. HEAD moves back but your index and working tree stay intact.

git reset --soft HEAD~1
# amend or create a fresh commit

This is perfect when the commit was premature but the work itself is fine.

Throw away the last commit and the working tree

When the previous commit is garbage and you do not care about the working tree do a hard reset. This rewrites history and resets files to match the target commit. Warning do not do this on a shared branch unless everyone agreed and bought snacks.

git reset --hard HEAD~1

Undo a commit that has already been pushed

For public history prefer git revert which creates a new commit that reverses the changes. This keeps the timeline honest and keeps teammates alive.

git revert <SHA>
git push

If you are working alone on a branch and willing to accept the diplomatic fallout you can rewrite history locally and force push. Expect questions and possible rage.

git reset --hard <SHA>
git push --force

Recover a lost commit with reflog

Git keeps a trail of where HEAD has been. If you lost a commit due to a reset or a bad merge use reflog to find the missing SHA and then bring it back.

git reflog
# find the SHA
git reset --hard <SHA>
# or cherry pick back onto your branch
git cherry-pick <SHA>

When to prefer revert over reset

  • Shared branches and mainline work use git revert to avoid breaking collaborators
  • Private or feature branches may safely use reset plus force push with care
  • Use reflog to recover commits after you panic and hit a button

Quick cheat sheet

  • Inspect history git log --oneline
  • Keep changes staged git reset --soft HEAD~1
  • Discard changes git reset --hard HEAD~1
  • Reverse a pushed commit git revert <SHA> then git push
  • Recover lost work git reflog then git reset or git cherry-pick

Version control is a safety net and also a place to practice humility. Use git revert on public branches, treat git reset as a scalpel, and consult reflog when you need a tiny miracle. Now go fix that commit message and try not to make the same mistake in the next dramatic commit spree.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.