If you pushed something that looks wrong and your teammates are watching you sweat, relax and pick the right tool for the job. This short git tutorial walks through how to undo a commit with the least drama. We cover safe fixes that keep history honest and destructive edits for solo branches when you know what you are doing. Keywords you might want to whisper at the terminal include git revert reset reflog and force push.
First question to ask yourself while pretending you had a plan is whether the branch is public or private. If other people might base work on your commits then do not rewrite history. If it is a throwaway feature branch you control then history surgery is allowed, in moderation.
Use git revert
when you want to undo a commit without lying to future readers of the history. Revert makes a new commit that applies the inverse of the target commit. The original bad commit stays in the timeline and everyone can follow along like nothing spooky happened.
Example command
git revert <commit-hash>
This will open your editor to edit the revert message unless you pass a message on the command line. Be descriptive so future humans understand why the reversal happened.
If the commit only exists on your laptop and you prefer a cleaner history then git reset
is your blunt instrument. There are three useful modes to remember.
--soft
moves the branch pointer but keeps changes staged. Example git reset --soft HEAD~1
--hard
drops work from the working tree and index. Example git reset --hard HEAD~1
This destroys uncommitted changes so make sure you really want thatUse reset for local history edits and cleanup. Do not use reset to hide commits that others may rely on.
If you misapplied a reset and now regret life choices use git reflog
to find the lost commit references. Reflog logs where HEAD pointed across actions so you can find the SHA you need to recover.
Typical recovery steps
git reflog
and find the commit SHA or entry you wantgit checkout -b recover <sha>
or restore directly with git reset --hard <sha>
Shared branches reward caution. Avoid git push --force
unless you and your team agreed to it beforehand. When a history rewrite is unavoidable prefer git push --force-with-lease
to reduce the chance of stomping someone else s work.
If you used git revert
you can push normally with git push
. If you reset local commits that were already pushed you will need a force push and a short apology message in the team chat.
git revert <commit-hash>
Undo while preserving historygit reset --soft HEAD~1
Move head and keep staged changesgit reset --hard HEAD~1
Drop the last commit and its changesgit reflog
Find lost commits after a messy resetgit checkout -b recover <sha>
Inspect or restore a lost stategit push --force-with-lease
Safer force push when you must rewrite shared historyUse git revert on public branches to keep version control honest. Use git reset for local cleanup when you can afford to rewrite history. Keep git reflog in your back pocket for rescues after an unfortunate reset. Coordinate and prefer safer pushes when working with teammates.
And remember to write revert messages that explain why the change was rolled back so future developers do not assume the worst. You may have to explain your mistakes to humans, but at least your git history will be readable and slightly less tragic.
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.