DEV Community

foadlind
foadlind

Posted on • Originally published at practicalgit.com

Recover a deleted local branch

If you have accidentally deleted a branch that was never pushed to a remote, you can easily recover it in Git.
You'll need help from a useful Git utility called reflog. Let's show you how to do it:

Suppose I have a local branch called feature_1, in which I have made a commit:

* ef640e4 (feature_1) Add settings flag for feature_1
* 4015b6f (HEAD -> main) Provide default for product size
* d8dc31c Add db info to settings
Enter fullscreen mode Exit fullscreen mode

Now I, accidentally, delete the feature_1 branch:

$ git branch -D feature_1
Deleted branch feature_1 (was ef640e4).
Enter fullscreen mode Exit fullscreen mode

So now all my work in that branch is gone 😱:

* 4015b6f (HEAD -> main) Provide default for product size.
* d8dc31c Add db info to settings.
Enter fullscreen mode Exit fullscreen mode

Since I had not yet pushed the branch, I don't have a copy of it on the remote, so I have to resort to reflog. Git always keeps a log of your activities in the repository.

By activities, I mean anything that has to do with Git. So if
you have committed anything, made a branch, stashed anything, it will be notes in the reflog. Git documentation defines it like this:

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.Reflogs are useful in various Git commands, to specify the old value of a reference.

So let's use it in our case:

$ git reflog
4015b6f (HEAD -> main) HEAD@{0}: checkout: moving from feature_1 to main
ef640e4 HEAD@{1}: commit: Add settings flag for feature_1
4015b6f (HEAD -> main) HEAD@{2}: checkout: moving from main to feature_1
...
Enter fullscreen mode Exit fullscreen mode

There it is! Our last commit in feature_1 branch: ef640e4 HEAD@{1}: commit: Add settings flag for feature_1. That is
the location in our commit tree that we want to be at. It is worth mentioning that branches are nothing but "labels". They just point to a commit in the tree. So if we can make the branch label point to that place in the commit tree, we have restored our branch as it was before deletion.

To do that, first, we recreate our lost branch:

$ git checkout -b feature_1
Switched to a new branch 'feature_1'
Enter fullscreen mode Exit fullscreen mode

Basically we are recreating the label. But now it is pointing to the wrong commit. At the moment it is pointing to the
same commit as main. In our new branch, we use the reset command with the --hard flag to set the branch tip to ef640e4 HEAD@{1}: commit: Add settings flag for feature_1.

(feature_1) $ git reset --hard ef640e4
HEAD is now at ef640e4 Add settings flag for feature_1
Enter fullscreen mode Exit fullscreen mode

So if we look at the Git log, we can see that our branch is restored:

* ef640e4 (HEAD -> feature_1) Add settings flag for feature_1
* 4015b6f (main) Provide default for product size
* d8dc31c Add db info to settings
Enter fullscreen mode Exit fullscreen mode

In this example we only had one commit in the branch, so we pointed to that. If you had more commits in your lost branch,
the process is exactly the same. Just point to the last commit in the lost branch, and you are good to go.


signup

Top comments (0)