How to Revert a Git Commit |Video upload date:  · Duration: PT6M52S  · Language: EN

Learn how to undo Git commits safely with revert reset and reflog examples and commands for shared and local branches

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.

Decide if the branch is shared or private

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.

Safe undo with git revert

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.

When to use revert

  • On shared branches where branch management and traceability matter
  • When you need an audit friendly record of the change and its undo
  • When you do not want to risk rewriting commits that others built on

Local cleanup with git reset

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
  • Default reset without flags moves the pointer and unstages changes but keeps them in the working tree
  • --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 that

Use reset for local history edits and cleanup. Do not use reset to hide commits that others may rely on.

Recover lost commits with git reflog

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

  • Run git reflog and find the commit SHA or entry you want
  • Create a branch to inspect the state with git checkout -b recover <sha> or restore directly with git reset --hard <sha>

Pushing and collaboration

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.

Quick cheat sheet of git commands to undo commit

  • git revert <commit-hash> Undo while preserving history
  • git reset --soft HEAD~1 Move head and keep staged changes
  • git reset --hard HEAD~1 Drop the last commit and its changes
  • git reflog Find lost commits after a messy reset
  • git checkout -b recover <sha> Inspect or restore a lost state
  • git push --force-with-lease Safer force push when you must rewrite shared history

Final recap and best practices

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