Git – How to restore deleted branch?
Most developers have accidentally deleted the wrong branch at least once. Fatigue, distraction, or just a bad moment – it happens. Once you realize what just occurred, the first thought is usually: “Oh no, what have I done?” (well, the real first thought contains considerably more swear words, but I’ll skip those).
In a moment of panic, you start searching for a solution – and luckily, you find one: git reflog.
Git reflog
Git keeps a record of all actions you perform in something called the reflog. It’s a reference log that tracks where HEAD was pointing over time, essentially where your repository was at different moments.
You can think of it as a kind of activity journal that records your actions, such as creating a branch, switching to it, making commits, switching to another branch, or merging branches. Git remembers commit hashes for each branch, even if that branch has already been deleted.
Thanks to this, you can go back to a specific point in history, to a moment when your branch still existed and recover the lost code.
With git reflog, you can:
- find a lost commit
- recreate a branch from it
- recover work you thought was gone
Restoring a deleted branch
Let’s get to the point: how do you restore a locally deleted branch?
git reflog
You’ll see a list of performed actions along with their associated commit hashes, for example:
5edd4ac (HEAD -> feature/product-filters) HEAD@{0}: checkout: moving from 5edd4acd423ee7ceb3caed203f264a927e17386b to feature/product-filters
5edd4ac (HEAD -> feature/product-filters) HEAD@{1}: checkout: moving from feature/pagination to 5edd4ac
60cf7e1 (feature/pagination) HEAD@{2}: commit: change styles for products pagination - again
771d662 HEAD@{3}: checkout: moving from staging to feature/pagination
<-- Delete feature/product-filters here -->
06fb2b3 (staging, master) HEAD@{4}: checkout: moving from feature/pagination to staging
771d662 HEAD@{5}: commit: change styles for products pagination
5edd4ac (HEAD -> feature/product-filters) HEAD@{6}: checkout: moving from feature/product-filters to feature/pagination
5edd4ac (HEAD -> feature/product-filters) HEAD@{7}: commit: change styles for product filters
b0f7d9b HEAD@{8}: commit: change file name from fileX to fileY
06fb2b3 (staging, master) HEAD@{9}: checkout: moving from staging to feature/product-filters
Find the hash of the last commit that existed on your deleted branch. It’s usually the entry just before switching to another branch or the one related to the deletion itself. You can now create a new branch (even with the same name) that points to this commit:
git checkout -b name-of-deleted-branch 5edd4ac
And that’s it – your branch is available locally again.
If you also deleted the branch remotely (for example on GitHub, GitLab, or Bitbucket), you’ll need to push it back to the remote after restoring it locally.
git push -u origin name-of-deleted-branch
Keep in mind that the reflog exists only on your local machine. It is not shared across the team, which is why regular pushes are recommended, especially when working on larger changes, so you don’t have to rely solely on the reflog for recovery.