Recover deleted git branch from local
You are the boss of yourself, you deleted it, you recover it.
👋 Say Hi.. to me on Twitter
Here’s your blog content rewritten in a conversational, easy-to-read format:
Recover a Deleted Git Branch Without Diving Too Deep
Oops! Accidentally deleted a branch? No worries. Here’s how you can recover it quickly without breaking a sweat.
Step 1: Find the Commit (SHA) of the Deleted Branch
First, we need to locate the commit history of the branch you deleted. Git keeps a handy log called reflog
, which tracks all the HEAD pointers, even for deleted branches.
Run the following command:
git reflog
This will give you a list of all recent actions in your repository, including the SHA (commit hash) of your deleted branch.
Step 2: Restore the Deleted Branch
Once you have the SHA, restoring the branch is straightforward:
git checkout -b <branch_name> <sha>
And boom, your branch is back! 🎉
Quick Git Concept: What’s HEAD?
Before clapping (yes, you can clap 50 times on Medium!), let me quickly explain something crucial — HEAD.
HEAD is like a pointer that always references the current state of your repository. Think of it as:
- The tip of your current branch.
- The last place where your repository left off.
- The parent of your next commit (essentially, where the next commit will be recorded).
Whenever you switch branches, make commits, or create a new branch, Git updates the HEAD pointer accordingly.
Even if you delete a branch, the HEAD history persists, and this is exactly what allows us to recover deleted branches.
Understanding git reflog
with an Example
Let’s walk through this with a practical example:
Step 1: Check the Reflog on the Master Branch
git branch
* master
git reflog
ce656f5 HEAD@{0}: commit (initial): Initial commit
This shows the initial commit SHA for the master branch.
Step 2: Create a New Branch and Commit Changes
git checkout -b test
Switched to a new branch 'test'
# Make some changes to a file and commit them
git add .
git commit -m "This is test branch commit after change"
[test 9867883] This is test branch commit after change
Step 3: Check the Reflog Again
git reflog
9867883 HEAD@{0}: commit: This is test branch commit after change
ce656f5 HEAD@{1}: checkout: moving from master to test
ce656f5 HEAD@{2}: commit (initial): Initial commit
Notice how a new HEAD (HEAD@{1}) was created when we switched to the test
branch. The SHA of the test branch’s initial state matches the master branch (since no changes were made at first).
Step 4: Delete the Test Branch
git checkout master
git branch -D test
Deleted branch test (was 9867883).
Step 5: Check the Reflog After Deletion
git reflog
ce656f5 HEAD@{0}: checkout: moving from test to master
9867883 HEAD@{1}: commit: This is test branch commit after change
ce656f5 HEAD@{2}: checkout: moving from master to test
ce656f5 HEAD@{3}: commit (initial): Initial commit
Even though the test
branch is deleted, its last commit (SHA 9867883
) still exists in the reflog.
Step 6: Recover the Deleted Branch
Here’s how you bring back your branch:
git checkout -b recovered_test 9867883
Switched to a new branch 'recovered_test'
Check your branches now:
git branch
* master
recovered_test
And just like that, your branch is restored! 🎉
Wrap-Up
If you made it this far, congratulations — you’ve successfully learned how to recover a deleted Git branch like a pro. The key takeaway here is that git reflog
is your best friend when it comes to undoing accidental deletions.
Happy coding! 👩💻👨💻