How Do You Recover a Deleted Branch in Git?

Accidentally deleting a branch in Git can be concerning, but fortunately, Git makes it possible to recover deleted branches in most cases. Whether the branch was deleted locally or remotely, there are ways to restore it, allowing you to continue your work without losing any progress.

Recovering a Locally Deleted Branch

If you’ve deleted a branch locally but haven’t yet deleted it from the remote repository, you can easily recover it using the branch’s last known commit hash.

Step 1: Find the Commit Hash

First, you’ll need to find the commit hash of the branch’s last commit. You can do this by using the git reflog command, which shows a log of all the actions you’ve performed in your repository, including branch checkouts and deletions.

git reflog

Look through the output for an entry related to the deleted branch, and note the commit hash.

Step 2: Create a New Branch from the Commit Hash

Once you’ve identified the commit hash, you can create a new branch that points to the same commit as the deleted branch:

git checkout -b <branch-name> <commit-hash>

Replace <branch-name> with the name of the deleted branch, and <commit-hash> with the hash you found in the previous step.

Example:

git checkout -b feature-branch 1234abcd

This command recreates the branch feature-branch at commit 1234abcd.

Recovering a Remotely Deleted Branch

If the branch was deleted from both your local and remote repositories, recovery is still possible, but it requires a different approach.

Step 1: Check the Remote Repositories

First, ensure that the branch was indeed deleted from the remote repository. You can check this by running:

git fetch --all

This command updates your local copy of all remote branches. After fetching, use:

git branch -r

This lists all remote branches. If the branch is still listed here, you can simply check it out:

git checkout -b <branch-name> origin/<branch-name>

Step 2: Recover from Another Clone

If the branch was deleted from the remote repository, but you have another clone of the repository that still contains the branch, you can push it back to the remote:

git push origin <branch-name>

Step 3: Use the Stash (If Applicable)

If the branch had uncommitted changes before deletion and you used git stash, you can recover those changes by applying the stash:

git stash list
git stash apply

This command restores the stashed changes, which you can then commit to a new branch.

Preventing Accidental Branch Deletion

To minimize the risk of accidentally deleting branches in the future, consider the following tips:

  • Use git branch -d for Safe Deletion: The -d option prevents deletion if the branch contains unmerged changes. Use git branch -D with caution, as it forces deletion.
  • Regular Backups: Regularly push your branches to a remote repository or create backups to avoid data loss.
  • Use Git Hooks: Consider setting up Git hooks to warn you before deleting certain branches, especially important or protected ones.

Conclusion

Recovering a deleted branch in Git is often possible thanks to Git’s robust history tracking features. Whether the branch was deleted locally or remotely, following the steps outlined above can help you restore your work and continue development without interruption.