Git Revert a Commit |Video upload date:  · Duration: PT1M0S  · Language: EN

Quick guide on using git revert to safely undo a commit and keep project history intact

Why revert and not erase

If you pushed something bad and your teammates are already building on it do the polite thing and revert instead of rewriting public history. git revert creates a new commit that undoes an earlier commit while keeping the project history intact and audit friendly. That means no surprise force pushes and fewer angry Slack messages.

Find the guilty commit

First locate the commit hash you want to undo. A concise log helps you avoid guessing games and blame by committee.

git log --oneline

Copy the short hash for the offending commit. If you prefer a visual history use your GUI client but the CLI is faster when caffeine is low.

Run git revert

To create a new commit that undoes the change run this.

git revert <commit-hash>

If you need to revert multiple commits you can supply a range or revert them one by one.

git revert <start-hash>..<end-hash>

If you want to stage a combined set of undo changes and edit the result before committing use the no commit flag.

git revert --no-commit <start-hash>..<end-hash>

Reverting a merge commit is a bit special. Tell git which parent to keep with the parent number flag.

git revert -m 1 <merge-commit-hash>

Resolve conflicts and finish the job

If conflicts appear treat them like mild paperwork. Open the conflicted files fix the content to the desired state then stage and finish.

git add <file>
git commit   # only needed when you used --no-commit or to finish manual edits

Use git status and git diff to see what is happening. Conflicts are not personal and will not be invited to Thanksgiving.

Push the revert so everyone sees it

Once the revert commit exists push it to the remote so the team gets the correction.

git push

This keeps the branch linear and auditable which is exactly what future you will thank present you for.

Quick workflow rules of thumb

  • Prefer git revert on public branches to avoid rewriting shared history
  • Use git reset for local experiments that have not been pushed
  • Use interactive rebase on private branches when you want a neat commit history
  • Write a clear revert message so future archaeologists know what went wrong

Short pitfalls checklist

  • Reverting a merge requires the -m parent flag
  • git revert does not delete commits it only adds a correcting commit
  • If you revert then force push you will still cause trouble so avoid that

There you go. Revert when you need to correct public history and keep your repo sensible. If you are cleaning up local mistakes use reset or rebase depending on how tidy you want the log to look. Now go fix that bug and try not to break anything else this week.

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.