Intro Paragraph
If you’ve ever deleted a branch that you accidentally committed by mistake or simply wish for it back, this tutorial will guide you through the process of recovering your lost Git branch. Whether due to a simple typo in your command history or an accidental commit, losing a branch can be frustrating, but with the right tools and commands, you can retrieve it.
Quick Answer / TL;DR
The steps below walk through using git reflog and git checkout to find the deleted branch. This process is effective even if you have already committed or pushed changes to that branch.
Step 1: Identify the Deleted Branch
$ git reflog
56dff7d HEAD@{0}: commit (initial): feat: initial scaffold
This command will show you all history-related commands starting from the point when your current state was last saved (i.e., HEAD). Look for a line similar to something like:
e56c40a HEAD@{1}: checkout: moving from main -> branch_name
f78d9be HEAD@{2}: commit: Branch deleted by mistake!
Step 2: Determine the Commit Hash of the Deleted Branch
From the command output, note down the hash code immediately after mentioning HEAD. This is crucial as it will be used to find your old branch.
Step 3: Fetch and Checkout the Original Branch from Git Reflog
$ git checkout <hash_code>
Replace with the actual commit hash you noted in step 2. The exact code should look something like e56c40a.
Optional: Reset Your Repository to a Specific Point (Optional)
If your branch has diverged significantly from the main repository, or if you want to ensure that everything is up-to-date before continuing:
$ git reset --hard <hash_code>
This command will force-checkout the original commit, effectively discarding any changes made since.
Troubleshooting / Common Mistakes
1. Missing Commit Hash
If you misspelled a hash or misremembered part of it due to typos, ensure that every character is correct and try again:
$ git checkout e56c40a
You may need to manually input the complete hash to make sure there are no mistakes.
2. Incorrect Branch Name
Ensure you have the exact same branch name as before (in this case, branch_name).
$ git checkout main
(no output)
If your branch was renamed during deletion, try searching for all instances of branches and ensure you haven’t missed any:
$ git ls-remote --heads origin | grep 'main'
3. Branch Not Deleted Yet
Ensure the deleted branch hasn't been committed or pushed yet by checking if it still appears in your repository:
$ git rev-list e56c40a..HEAD
This command should return an empty list, indicating that your branch hasn’t been committed or pushed.
4. Multiple References to the Same Branch
If you’ve had multiple commits referencing the same branch before it was deleted:
- Use
git logwith a specific refspec (e.g.,HEAD) to find all references:
$ git log <hash_code> --oneline
- Then, use
git show-reforgit tag -lto list all tags related to the branch.
5. Branch Not Found in Reflog
If you’re certain a branch was deleted but can’t find it through reflog:
- Try searching for references using
git ls-tree.
$ git ls-tree -r <hash_code>
If this doesn’t reveal your branch, consider checking the .git/refs directory manually to see if any remnants of the deleted branch are present.
6. Branch Is Not a Feature Branch
Ensure you’re not trying to recover something that was meant for a feature branch or another type of Git branching model:
- Check your commit message history:
$ git log --graph --abbrev-commit --pretty=format:'%an: %ad on %h - %s' HEAD..HEAD~1
This command traces the commits back to find any potential conflicts or non-feature branch-related changes.
Conclusion
Recovering a deleted Git branch can be frustrating but is often possible with the right commands and knowledge of where it’s located. This article has shown you how to use git reflog effectively to locate your lost branch, recover it, and ensure everything in your repository remains clean. Remember that this process involves careful steps to avoid losing any important changes or commits.
References
- Official Git Documentation:
- Stack Overflow: Searching for "recover deleted git branch"
- GitHub Blog: Tips on Recovering a Deleted Branch