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.
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.
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.
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
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
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>
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.