Urgent.News

What's breaking now, across thousands of outlets.

Tech

The Git Recovery Guide: How to Undo Anything (Without Panic)

There's a specific feeling every developer knows. You run a git command, hit Enter, and half a second later your stomach drops because you realize what you just did. Three hours of work, gone from git log . The wrong branch, obliterated. A rebase that turned your history into soup. Take a breath, because here's the truth that this entire guide rests on: Git almost never actually deletes your…

Developers often experience a sinking feeling when they accidentally run a git command and realize they've made a mistake. They lose three hours of work or find their branch obliterated. But, here's the truth: Git almost never deletes your work. Instead, it makes the work "orphaned," meaning nothing points to it anymore. The commit is still in Git's database, and its hash is recorded in a log of everything you've done. Recovery is almost always possible if you know which command can bring it back.

This guide is organized by the action you took, so find your situation, copy the fix, and breathe. Bookmark it for future emergencies. Before diving into the recipes, let's understand the theory. Git rarely destroys anything; it moves references like branches and HEAD, which point to the current state. When you reset, delete a branch, or mess up a rebase, you're usually just moving a pointer, not deleting commits.

The commits stay in Git's object database until garbage collection clears the unreferenced ones. Git keeps two safety nets: the reflog, a log of every move HEAD and branches have made, and git fsck, which finds unreachable objects.

The guide focuses on reversing actions, starting with undoing uncommitted changes. To discard changes to one file, use git restore filename. To unstage a file but keep the changes, use git restore --staged filename. To discard all uncommitted changes, use git restore..

For fixing commits, amend the last commit message with git commit --amend. If you forgot to include a file in the last commit, add it with git add filename and amend again with git commit --amend. To undo the last commit but keep the changes, use git reset --soft HEAD~1. To undo the last commit and discard the changes, use git reset --hard HEAD~1. To undo a commit that's already pushed, use git revert commit-hash, which creates a new commit that reverses the old one. This is safe for shared branches.

The heart of the guide is the reflog, the safety net for almost every panic. When you run git reset --hard and lose commits, use git reflog to find the entry before the reset and reset to it. If you deleted a branch with commits on it, find the last commit on the branch using git reflog and recreate the branch with git branch. If you botched a rebase, find the commit before the rebase and reset to it with git reset --hard HEAD@{5}. If your commits were in a detached HEAD state, give them a new branch with git branch.

When the reflog isn't enough, use git fsck --unreachable | grep commit to inspect unreachable objects and find the one you want. Then, recover it with git branch and git show.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Tuesday 22 September →