How to Recover Lost Files in Git After Committing to the Wrong Branch
If you’ve ever accidentally committed changes to the wrong Git branch, you know how stressful it can be — especially if you stash your work or switch branches afterward. Fortunately, Git keeps a detailed history that makes it possible to recover your files. Here’s a step-by-step guide to getting your work back and cleaning up afterward.
Step 1: Assess the Situation
Before making any changes, check the status of your repository:
git status
This will show any untracked or modified files. Make a note of what’s missing or uncommitted.
Step 2: Check Your Stash
If you stashed your changes, check if your stash still exists:
git stash list
You might see something like:
stash@{0}: WIP on wrong-branch: abc1234 Commit message
If the stash exists, your changes are safely stored there.
Step 3: Inspect Recent Commits
Even if you committed to the wrong branch and then stashed, your commit likely still exists. Use Git’s reflog to see recent actions:
git reflog
Look for entries like your recent commit or stash. Each entry will have a commit hash that you can reference later.
Step 4: Recover Your Commit
If your commit isn’t on any branch, you can create a new branch from it:
git checkout -b recovery <commit-hash>
This creates a new branch with all the files from that commit. You can then merge, cherry-pick, or copy files to the correct branch.
Step 5: Recover from a Stash
If your changes are in a stash, you can apply them safely:
git stash apply stash@{0}
Or create a new branch from your stash for a safer approach:
git stash branch recovery-stash stash@{0}
This puts all your stashed changes on a new branch without affecting your current working tree.
Step 6: Clean Up the Wrong Branch
Once your files are safely recovered, you may want to delete the branch you accidentally committed to.
Delete the local branch:
git checkout main # switch to a safe branch first
git branch -D wrong-branch-name
Delete the remote branch (if it was pushed):
git push origin –delete wrong-branch-name
Optionally, prune stale remote references:
git fetch –prune
Key Takeaways
- Git keeps a detailed history via commits, reflog, and stashes — you rarely truly “lose” work.
- Always check git status, git stash list, and git reflog when files seem lost.
- Use a recovery branch or stash branch to safely retrieve work before deleting anything.
- Only delete branches once you are confident nothing important remains on them.
Recovering from a mis-commit can feel scary, but with these steps, your files are safe, and your repo stays clean and organized.
Retiring Lawson, PeopleSoft, or Oracle? APIX archives the entire application — every table, every year, attachments and security included — into your own AWS account in about 30 days, so you can decommission the legacy system and keep full access to the history.


