Restoring a Deleted Git Branch

  • Last updated Apr 25, 2024

This solution works only if too much time hasn't been passed since the branch was deleted.

Follow these steps to restore a deleted branch in Git:

  1. Use 'git reflog' command to display the reference logs, which provide a history of all references such as branches, commits, and more that were updated in your Git repository. It shows a detailed log of changes to your repository's references, even if they were moved, deleted, or altered in other ways:

  2. git reflog

    Next, you should see something like this in your terminal:

    Deleted branch my_branch_name (was 5756a8c06).

  3. Identify the sha commit hash of the deleted branch from the local records of sha.
  4. After you have identified the commit hash, you can follow the steps mentioned earlier to create a new branch at that commit and restore the deleted branch using the following command:

  5. git checkout -b <branch-name> <sha>

    Example: git checkout -b my_branch_name 5756a8c06