Git Reset Hard |Video upload date:  · Duration: PT1M0S  · Language: EN

Learn how to use git reset --hard safely to discard changes move HEAD and recover using reflog

If you like living on the edge and you use version control then git reset --hard is the thrill ride that also deletes your picnic basket. This guide will show the safe, sarcastic and technically correct way to move HEAD and force the working tree to match a commit while leaving you a path back when you mess up.

Check the repo and save your work

Before you do anything heroic or regretful run git status and see what is about to be nuked. If you want the changes later either commit them or stash them. No drama is the point here.

git status
# to keep changes
git add .
git commit -m "WIP save before reset"
# or to stash and hide the evidence
git stash push -m "WIP before reset"

Pick the target commit

Find the commit you actually want to end up at. Use git log --oneline for a compact view. Look at the message and diff to make sure it is not a mirage.

git log --oneline --decorate --graph
# inspect a commit if you need to be extra sure
git show COMMIT_HASH

Run the hard reset

When you are certain run the reset command. This moves HEAD and forces the index and working tree to match that commit. If you have uncommitted work it will be discarded without mercy.

git reset --hard COMMIT_HASH

Yes it is brutal. No it will not send you a warning message. That is why you saved your work earlier.

Update the remote safely

If the branch you rewrote was already pushed to a shared remote you need to update the remote too. Use force with lease to reduce the chance of trashing other peoples work. It is not a perfect safety net but it helps.

git push --force-with-lease origin your-branch-name

If you are the kind of person who likes to break history in public warn your team first and coordinate a tiny funeral for the old commits.

Recover lost commits with reflog

If something went wrong and you need to roll back the reset the reflog is your emergency parachute. It records where HEAD has been for a limited time.

git reflog
# find the HEAD entry you want then
git reset --hard HEAD@{n}

Reflog entries expire so do not rely on it as a permanent backup. Still it is a lifesaver when you realize five minutes too late that you reset the wrong branch.

Quick safety checklist

  • Create a temporary branch before a hard reset with git branch backup if you want a manual undo point
  • Prefer --force-with-lease over raw force when pushing to shared branches
  • Use git stash or commits to preserve WIP
  • Tell your teammates when you rewrite history so they do not debug ghost commits

Summary in plain words Keep calm and save your work first Inspect the target commit Reset with respect for the damage you can do Push with lease to avoid stomping others and use reflog for recovery when courage fails. If nothing else remember that backup branches are cheap and pride can be rebuilt.

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.