Git Uncommit Your Last Commit |Video upload date:  · Duration: PT3M53S  · Language: EN

Quick guide to undo the last Git commit safely using reset and revert with notes for pushed commits and staged changes

If you made a commit and wish you had not do not panic yet You can uncommit the last commit and choose whether to keep the files staged keep them unstaged or wipe everything like it never happened This quick git tutorial walks through safe ways to uncommit your last commit in version control without angry teammates or ruined branches

Check the repo first

Before wrecking things run these commands to see what is actually going on The log and status will tell you if you are about to act on the right commit or if you are about to erase something precious

git log --oneline -n 5

git status

Look at the short log to confirm the commit id and message and use git status to see staged and unstaged changes

Uncommit but keep changes staged

Want to edit the commit message or add one more file without losing what you had staged Use git reset --soft HEAD~1 This moves HEAD back one commit but leaves the index and working tree intact In plain English the files stay staged and ready for a new commit

git reset --soft HEAD~1

After that run git commit --amend to change the message or git add and git commit again to produce the new commit

Uncommit and keep changes unstaged

If you need to keep your file edits but want a clean index so you can craft a different commit run git reset HEAD~1 This moves HEAD back one commit and unstages the changes while keeping them in your working tree

git reset HEAD~1

Use this when you want to rework files before committing again or when you accidentally included debug junk

Remove the commit and discard local changes

When the commit was a mistake and the work is disposable use git reset --hard HEAD~1 This will remove the commit and wipe working tree changes This is destructive garbage disposal so triple check the log first

git reset --hard HEAD~1

If there is any chance you want the work back create a branch backup first

What to do if the commit was already pushed

If the bad commit is already on a shared remote prefer safe options Revert the commit to preserve public history

git revert HEAD

git push

git revert will create a new commit that undoes the previous commit so history stays intact If you must rewrite history with git reset and force push coordinate with collaborators and be prepared for merge headaches Here is the nuclear option

git reset --soft HEAD~1

git commit -m "fixed"

git push --force-with-lease

Using push --force with lease is slightly less reckless than plain force but still requires team coordination

Safety tip

If you are unsure create a recovery branch and then experiment on HEAD

git branch backup-uncommit

Quick summary

  • Use git reset --soft HEAD~1 to uncommit and keep files staged
  • Use git reset HEAD~1 to uncommit and leave changes unstaged
  • Use git reset --hard HEAD~1 to remove the commit and wipe changes
  • Use git revert HEAD to undo a commit that was already pushed

Pick the command that matches whether you want staged files to remain or whether you want the working tree to survive If nothing else make a backup branch before experimenting and you will still be employed by the end of the day

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.