NeuroAgent

Complete Guide: Restore Deleted Git Branch

Learn how to recover deleted Git branches using reflog, GitHub restore feature, and other methods. Complete guide with step-by-step instructions to restore accidentally deleted branches.

Question

How can I recover a Git branch after accidentally deleting it with git branch -d XYZ? What are the methods to restore a deleted branch in Git, and how can I undo the branch deletion command as if it never happened?

NeuroAgent

Yes, you can recover a deleted Git branch using several methods. The most reliable approach is using git reflog to locate the commit where the branch was deleted, then recreating the branch with git checkout -b branch-name HEAD@{1}. If you’re using GitHub, you can also restore the branch through the GitHub interface, or use git fsck to find unreachable commits if other methods fail.

Contents


Understanding Git Branch Deletion

When you delete a branch with git branch -d XYZ, Git doesn’t immediately remove the commits associated with that branch. Instead, it removes the branch reference, making the commits potentially unreachable if they’re not referenced by any other branch or tag. This is why recovery is often possible - the actual commits typically remain in the repository until garbage collection removes them.

According to The Full Stack Shepherd, “When you delete the branch you’re not deleting the commits that made up your branch, you are getting rid of an easy way to get to reference a relevant commit. If you removed the only reference to a commit you have made it ‘unreachable’.”


Method 1: Using Git Reflog

The git reflog (reference log) command is the most reliable method for recovering deleted branches. It maintains a log of all changes made to the repository’s HEAD, including branch deletions.

Step-by-Step Process:

  1. Check the reflog to find where your branch was deleted:

    bash
    git reflog
    
  2. Look for the deletion entry in the output. You’ll see entries like:

    HEAD@{1}: checkout: moving from feature_1 to master
    HEAD@{2}: branch: deleted feature_1
    
  3. Recreate the branch using the commit hash from before deletion:

    bash
    git checkout -b your-branch-name HEAD@{1}
    

As Better Stack Community explains, “if your branch was deleted at HEAD@{1}, you can recreate it as follows: git checkout -b your-branch-name HEAD@{1}”.

You can also use git reflog show branch to see the reflog for a specific branch before it was deleted.


Method 2: Using GitHub Restore Feature

If you accidentally deleted a branch in a GitHub repository, you can restore it through the GitHub web interface:

  1. Navigate to your GitHub repository
  2. Go to the “Branches” section
  3. Find the deleted branch in the list (GitHub keeps a history of deleted branches)
  4. Click on the “Restore branch” button

According to GitHub Community, “Click on the ‘Restore branch’ button. GitHub will recreate the deleted branch with all its commit history.”

This method works well for branches that were recently deleted and are still tracked by GitHub’s system.


Method 3: Using Git FSCK for Unreachable Commits

If git reflog doesn’t work (perhaps because you’ve run git prune or git gc), you can use git fsck to find unreachable commits:

  1. Find unreachable objects:

    bash
    git fsck --full --no-reflogs --unreachable --lost-found > lost
    
  2. Extract commit hashes:

    bash
    cat lost | cut -d' ' -f3 > commits
    
  3. View commit details:

    bash
    cat commits | xargs -n 1 git log -n 1 --pretty=oneline
    
  4. Recreate the branch using the commit hash:

    bash
    git checkout -b restored-branch abc1234
    

This method is more complex but can recover commits that might have been cleaned up by Git’s garbage collection.


Method 4: Using Git Log with Grep

If you remember part of a commit message from your deleted branch, you can search for it:

bash
git log --all --grep='commit message fragment'

As Graphite.dev explains, “You can also try `git log --all --grep=‘’ if you remember part of a commit message, to locate the commit.”

Once you find the commit hash, you can recreate the branch:

bash
git checkout -b restored-branch abc1234

Preventive Measures

To avoid future branch deletion issues:

  1. Always use git branch -D cautiously - This force-deletes branches even if they haven’t been merged
  2. Create backups of important branches using tags:
    bash
    git tag backup-xyz-branch xyz-branch
    
  3. Use Git hooks to prevent accidental deletions
  4. Regularly push important branches to remote repositories
  5. Use descriptive branch names to avoid confusion

Conclusion

Recovering a deleted Git branch is usually possible if you act quickly. The git reflog method is your best bet for most scenarios, while GitHub’s restore feature provides a convenient alternative for web-based repositories. If all else fails, git fsck can help locate unreachable commits. Remember that these recovery methods work best when used before Git’s garbage collection removes the unreachable commits. Always consider creating backups of important work and using clear branch naming conventions to prevent future issues.

Sources

  1. How to Restore a Deleted Branch or Commit with Git Reflog - Rewind
  2. Can I recover a branch after its deletion in Git? - Stack Overflow
  3. How to restore a deleted branch | GitHub Community
  4. Can I Recover a Branch after Its Deletion in Git? - Better Stack Community
  5. Recovering deleted git branches - Medium
  6. How to Restore a Deleted Branch or Commit with Git - GeeksforGeeks
  7. Recovering lost commits with git reflog - Graphite.dev
  8. Use “git reflog” and “cherry-pick” to restore lost commits - OCPsoft