Data report"State of code review 2024" is now liveRead the full report

Managing deleted branches in GitHub

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide covers how to delete branches in GitHub, what happens when they are deleted, and whether they can be recovered.

Deleting a branch in GitHub can be done either locally via your Git command line or directly through the GitHub web UI. Each method suits different scenarios, such as cleaning up after a merge or removing unnecessary or outdated branches.

To delete a branch locally and then remove it from your GitHub repository, follow these steps:

  1. Ensure you are not on the branch you want to delete: Switch to a different branch, typically the main branch:
    Terminal
    git checkout main
  2. Delete the branch locally: Use the git branch -d <branch-name> command for a safe deletion that checks for unmerged changes, or git branch -D <branch-name> to force the deletion:
    Terminal
    git branch -d feature-branch
  3. Push the deletion to GitHub: To remove the branch from your remote GitHub repository, use:
    Terminal
    git push origin --delete feature-branch

You can also delete branches directly on GitHub’s website:

  1. Navigate to your repository on GitHub.
  2. Go to the Branches tab
  3. Find the branch you want to delete and click on the trash bin icon next to it. GitHub delete branch screenshot

When you delete a branch:

  • Locally: The reference to that branch is removed from your local repository. The commits associated with the branch remain in your repository until they are pruned or garbage collected.
  • On GitHub: The reference is removed from the repository, and the branch no longer appears in the list of branches. However, the commits themselves remain accessible via their hashes if they are not part of other deleted histories.

Yes, deleted branches can often be recovered if necessary, as long as the commits were not explicitly purged from the repository.

If you remember the commit hash that was the tip of your deleted branch, you can recover it:

  1. Find the last commit of the deleted branch: If you don’t remember the commit hash, you can find it using:
    Terminal
    git reflog
    This command shows a log of where your HEAD and branch pointers have been recently, including at the time you deleted the branch.
  2. Create a new branch from the old commit: Once you have the commit hash, create a new branch pointing to it:
    Terminal
    git checkout -b new-branch-name <commit-hash>

Deleting branches in GitHub helps keep your repository clean and manageable, especially after features have been merged or ideas abandoned. Although deleting a branch removes its reference, the commits generally remain accessible, allowing for recovery if necessary. This flexibility ensures that you can manage your project effectively without losing important work.

For further reading see the official GitHub documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2