So you made a commit and now regret it. Welcome to version control, where every mistake is forever unless you are clever or lucky. This guide shows how to undo the last Git commit in the safest way for your repo and your coworkers. We cover the usual suspects git reset and git revert and when to use each one so you can survive another day in devops without causing a meltdown.
git reset --soft HEAD~1
Keep changes staged and edit the message or tweak files before recommittinggit reset HEAD~1
Keep file changes but unstage them so you can rework what you needgit reset --hard HEAD~1
Discard the commit and the working tree changes if you really mean itgit revert HEAD
Undo a commit that has already been shared by adding a new commit that reverses itLet us break it down without the corporate euphemisms. Git separates the world into three places the HEAD the index and the working tree. These commands move or change HEAD and the index in different ways. Pick the one that matches how dramatic you are willing to be.
Use git reset --soft HEAD~1
when you just want to fix the commit message or squash the change with another commit. This moves HEAD back one commit while leaving the index intact so everything is staged and ready to go. It is the polite undo for when you are mostly happy with the content but not the prose.
Use git reset HEAD~1
when you want to edit files before re staging. This resets the index but keeps your working files as modified. It is the reasonable middle ground for when you need to tweak things before committing again.
Use git reset --hard HEAD~1
only if you are sure the change is worthless and you do not need to recover it. This moves HEAD back and makes your working tree match the previous commit. There is no undo button after this unless reflog performs a miracle, and even then you should not count on it.
If the commit has already been pushed to a remote prefer git revert HEAD
. This creates a new commit that reverses the effect of the bad commit while preserving history. It keeps your teammates and CI pipelines from having a small nervous breakdown. Rewriting history on a shared branch is rude unless you coordinate with everyone.
If you do a reset on a branch that others might have pulled you will need to force push with git push --force
. That is the nuclear option. It will rewrite history and may require your teammates to re base or recover their branches. If you must force push use a private cleanup branch first and announce what you are about to do like a responsible human.
git revert
on shared branches to avoid merge conflicts and angry messagesgit reflog
if you think you erased something by mistake and need a miracleThis mini tutorial gives you the choices and trade offs when you need to undo the last commit. Use the command that matches the level of safety your repository and your teammates deserve. And remember if your commit message is the only problem a soft reset is less dramatic and far kinder to human relationships.
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.