Catalogue
Reviving a Deleted Git Branch

Reviving a Deleted Git Branch

🌐 日本語で読む

Overview

What to do when you force-deleted a branch with the command below, only to realize afterward that you actually needed it.

1
$ git branch -D <branch_name>

How to Recover

  1. Check the HEAD change history
  2. Create a branch from the HEAD log number
1
2
$ git reflog
$ git branch <branch_name> HEAD@{num}

Example:

1
2
3
4
5
6
$ git reflog

c95c7e9 HEAD@{0}: merge release: Merge made by the 'recursive' strategy.
ad5bed0 HEAD@{1}: checkout: moving from release to master
ffe45df HEAD@{2}: merge develop: Merge made by the 'recursive' strategy.
6aa536b HEAD@{3}: checkout: moving from develop to release

Once you figure out that HEAD@{3} above is the commit for the branch you deleted, run:

1
$ git branch hogehoge HEAD@{3}

After running the command above, running git branch shows that the branch hogehoge has been created.

So it’s a good idea to commit frequently.

Author

Kenzo Tanaka

Posted on

2015-12-09

Licensed under