Recover deleted git branch from local
You are the boss of yourself, you deleted it, you recover it.
👋 Say Hi.. to me on Twitter
Recover without diving into deep
Find the SHA
for the commit at the tip of your deleted branch using: Use git reflog
to do so:
git reflog
To restore the branch, use:
git checkout -b <branch> <sha>
Show your love by clapping. Ah! you can clap 50 times if you are not aware.
Explain me things first ( will clap later…)
This very important to understand that Git maintains a reference variable called HEAD. And we call this variable a pointer because its purpose is to reference, or point to, a specific commit in the repository. HEAD by default points to the tip of the current branch in our repository.
Another way to think of it is the last state of our repository or what was last checked out, and because it’s where the repository left off or the last state, you can also say that the HEAD points to the parent of the next commit or it’s where commit writing is going to take place.
Having said that it should be clear that when you create a new branch say test, a HEAD is created, when you do some changes and commit the changes, again a new HEAD is created. Now even if you leave this test
branch and delete it, HEAD reference still persists which provides a door to recover the deleted branch.
git reflog
gives all you need e.i. HEADs of all branches
Understanding git reflog by example
1: Let's check the reflog
while I am at master
branch.
~/w/git_demo ❯❯❯ git branch
* master
~/w/git_demo ❯❯❯ git reflog
ce656f5 HEAD@{0}: commit (initial): Initial commit
2: Let's create a new branch test
do some changes and commit it.
~/w/git_demo ❯❯❯ git checkout -b test
Switched to a new branch 'test'
~/w/git_demo ❯❯❯ git branch
master
* test====> Did some changes in file while I'm on test branch <====~/w/git_demo ❯❯❯ git add .
~/w/git_demo ❯❯❯ git commit -m "This is test branch commit after change"
[test 9867883] This is test branch commit after change
1 file changed, 1 insertion(+)
3: Now let's check the reflog
again.
~/w/git_demo ❯❯❯ 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
Important
- A new HEAD →
HEAD@{1}
is created when we checked out totest
branch. It refers to the top commit oftest
the branch which should be exactly the same asmaster
branch’s commit since we did not change anything. - Yes, it is. Note the SHA →
ce656f5
is the same formaster
andtest
branch. - Since we changed and committed on
test
branch our HEAD is moved up which isHEAD@{0}
reflecting the topmost commit of the currenttest
branch.
4: Its time to delete test
branch.
~/w/git_demo ❯❯❯ git checkout master
Switched to branch 'master'
~/w/git_demo ❯❯❯ git branch
* master
test
~/w/git_demo ❯❯❯ git branch -D test
Deleted branch test (was 9867883).
~/w/git_demo ❯❯❯ git branch
* master
5: What happens to reflog
?
~/w/git_demo ❯❯❯ 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
Nothing, though test
branch it deleted its top or latest commit still persists. This is life-saving. We got the door to enter. Now we have a reference to the deleted test
branch. YAAHOOOOO!!
Getting it back…
~/w/git_demo ❯❯❯ git branch
* master
~/w/git_demo ❯❯❯ git checkout -b recovered_test 9867883
Switched to a new branch 'recovered_test'
~/w/git_demo ❯❯❯ git branch
master
* recovered_test
Confused with 9867883? it's SHA
of the deleted test. (refer step 5 snippet)
DONE!!
If you reach here, you nailed it!
Special thanks to Paul. :)