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.
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"
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
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.
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.
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.
git branch backup
if you want a manual undo point--force-with-lease
over raw force when pushing to shared branchesgit stash
or commits to preserve WIPSummary 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.