If you work with Git long enough, mistakes are inevitable. You delete the wrong branch. You commit sensitive data. You rewrite history too far. And suddenly, your terminal looks less like a friendly command line and more like a trapdoor.
Good news: Git is more forgiving than it seems. Most changes are reversible if you know what to look for — and how to act quickly and calmly. This guide is for developers who want to recover from common Git mistakes without spiralling into panic mode.
1. You Committed to the Wrong Branch
It’s a classic mistake: you meant to be on your feature branch, but you were still on main or develop, and now your changes are in the wrong place. The first thing to do is stay calm and check your latest commits:
- git log –oneline
If the commit hasn’t been pushed to the remote, you’re in a great position to fix it. First, switch to the correct branch:
- git checkout your-feature-branch
Then cherry-pick the commit from the wrong branch to bring it into the right one:
- git cherry-pick <commit-hash>
Now, return to main and clean it up:
- git checkout main
- git reset –hard HEAD~1
If the change was already pushed, instead of rewriting history, revert the commit cleanly to preserve your history:
- git revert <commit-hash>
2. You Deleted a Local Branch with Work
You deleted a branch thinking it was no longer needed, only to realise later that it had important commits. Don’t panic — Git tracks more than you think. Use reflog to search your command history:
- git reflog
Scroll through the list to find the commit where that branch last existed. Once located, you can restore the branch using:
- git checkout -b branch-name <commit-hash>
This brings your work back from the dead. You can now continue working as if the branch was never deleted.
3. You Lost Changes After a Hard Reset
git reset –hard is a powerful but dangerous command. It discards uncommitted changes and can erase commits if not used carefully. But not all is lost. Git’s reflog holds a record of branch pointers, even ones you just reset:
- git reflog
Find the commit hash that represents the state before your reset. Once you’ve found it, restore your branch to that point:
- git reset –hard <commit-hash>
If you just want to grab specific files from that snapshot:
- git checkout <commit-hash> — path/to/file
This lets you recover selected changes without affecting the current HEAD.
4. You Committed Secrets or API Keys
Pushing sensitive data like passwords or API keys is serious — but it’s also fixable. First, stop and reset the last commit before pushing:
- git reset –soft HEAD~1
Edit the file to remove the sensitive content, then commit again. Add the secret file to your .gitignore to prevent future mistakes.
If the secret was pushed, remove the key from your system or provider immediately. Then rewrite your Git history to scrub it from every commit:
Using BFG:
- bfg –delete-files .env
Or with filter-branch:
- git filter-branch –force –index-filter \
- “git rm –cached –ignore-unmatch path/to/secret” \
- –prune-empty –tag-name-filter cat — –all
Finally, force-push the cleaned repository:
- git push origin –force –all
Notify your team and rotate any exposed keys.
5. You Force-Pushed and Overwrote Good Work
Force-pushing can be helpful but dangerous. If you accidentally overwrite remote commits, check whether the overwritten work still exists in your reflog:
- git reflog
Look for the state of your branch just before the force-push. Once identified, you can create a new branch to restore the lost changes:
- git checkout -b recovery-branch <commit-hash>
If the lost commits belonged to a teammate, ask them to run git log or reflog on their machine. They might still have the state locally. Merging their branch back in can fix the loss without conflict.
6. You Merged Too Soon or Merged the Wrong Branch
Merging too early or choosing the wrong branch to merge happens more often than teams admit — especially when working across multiple features, hotfixes, or long-lived branches. If you catch the mistake before pushing to the remote, use the reset command to undo the merge cleanly:
- git reset –hard HEAD~1
This removes the last merge commit and puts your working directory back to its previous state. If you’ve already pushed the incorrect merge, you’ll need to reverse it in a way that keeps your history clean and understandable. That’s where a revert comes in handy:
- git revert -m 1 <merge-commit-hash>
The -m 1 flag tells Git which parent to keep when reverting a merge. After that, you can re-merge correctly from the right branch, or hold off until the timing is better. Always double-check the source and target branches before confirming a merge, especially when using pull requests.
7. You Made a Mess of Interactive Rebase
Interactive rebase (git rebase -i) is powerful for cleaning up commit history, but it’s easy to lose track of what you’re doing in the middle of it — especially with multiple commits or conflicts. If you realise something has gone wrong during the rebase process and want to stop immediately, run:
- git rebase –abort
This cancels the entire rebase session and takes you back to your previous branch state. If the rebase has already completed but produced a bad result (e.g. loss of commits, duplicated changes, broken history), use reflog to roll back to the state before the rebase started:
- git reflog
Look for the point labelled something like rebase started or checkout: moving from X to Y, and reset your branch back to that state:
- git reset –hard <commit-hash>
For large rebases, it’s helpful to work on a temporary branch. Before starting, do:
- git checkout -b rebase-backup
That way, if the rebase doesn’t go as planned, you have a reference point to return to without risking data loss.
Final Thought: Git Is Safer Than You Think
Git seems scary when things go wrong, but it rarely deletes anything permanently unless you tell it to. Behind every action is a log, a reference, or a breadcrumb you can follow back. When panic hits, don’t rush — check reflog, read the output, and slow down.
Most importantly, learn from the error. Set up pre-commit hooks, make backups, or script your workflow. Mistakes happen — but recovery gets easier with practice.