RGB logo

RGB Studios.org

A web development company

April 24, 2025

How to Recover a Deleted Git Branch - Deleted Locally, Remotely or Both

Justin Golden

git
Photo credit @seogalaxy on Unsplash

How to Recover a Deleted Git Branch

Accidentally deleted a Git branch? Don’t worry — Git is more forgiving than it seems. Here’s how to recover a deleted branch depending on whether it was deleted locally, remotely, or both.


Option 1: Recover a Locally Deleted Branch

If you deleted the branch only locally (e.g., via git branch -d branch-name), but it still exists on the remote:

✅ Solution

git fetch origin
git checkout -b branch-name origin/branch-name

This recreates the local branch from the remote one.

Option 2: Recover a Remotely Deleted Branch

If you deleted the branch on GitHub (or another remote), but you still have it locally, you can simply push it back:

Solution

git checkout branch-name  # (if you're not already on it)
git push -u origin branch-name

This will restore the branch on the remote.

Option 3: Recover a Branch Deleted Locally and Remotely

If the branch is deleted both locally and remotely, hope is not lost — Git’s reflog can still help you retrieve it if you had it checked out recently.

Step-by-step Recovery with git reflog

  1. View recent Git activity:
git reflog
  1. Look for a line like this:
abc1234 HEAD@{5}: checkout: moving from master to branch-name

Or something like:

abc1234 HEAD@{7}: commit: Work on feature
  1. Create the branch again from that commit:
git checkout -b branch-name abc1234
  1. Optionally push it back to the remote:
git push -u origin branch-name

Tips

Search by Commit Message

If you remember the commit message but not the hash, try searching for some of the commit message text:

git log --all --grep="fix imports"

For example, if the full message was “clean and fix imports in utils” this would find it.

Or, if you’re trying to find a branch reference:

git reflog | grep "branch-name"

To find it

Recover From Someone Else’s Clone

If you’re on a team and the branch is gone from both your local and GitHub, someone else might still have a copy.

If your teammate had the branch locally, they can push it back to the remote:

git push origin branch-name

If You Never Pushed and Never Checked Out

If someone creates a branch, makes a commit, then deletes it without pushing or switching branches, those commits may still exist but are “dangling.” Could be recovered with:

git fsck --lost-found

FSCK stands for File System Consistency Check and is used to verify the repository’s internal structure, ensuring that all objects (commits, blobs, trees, and tags) are valid and reachable. See if you find any dangling commits.

Closing

Git remembers more than you think. Always try googling, stack overflow, and chatbots for git issues before giving up. There’s a lot more tools at your disposal and with the internet and AI, you don’t have to remember everything.


More Blog Articles
  Share   Tweet   Pin   Share   Post   Post   Share   Email