How to Delete a Branch in GitHub

Summarize this article with:

Stale branches pile up fast. One merged feature here, an abandoned experiment there, and suddenly your repository looks like a graveyard of outdated code.

Knowing how to delete a branch in GitHub keeps your project organized and your team focused on what matters.

This guide walks you through four deletion methods:

  • Removing local branches via terminal
  • Deleting remote branches with Git Bash commands
  • Using the GitHub web interface
  • Setting up automatic cleanup after merges

Each method takes under three minutes. You will also learn how to recover accidentally deleted branches and troubleshoot common errors.

Let’s clean up your version control workflow.

How to Delete a Branch in GitHub

maxresdefault How to Delete a Branch in GitHub

Deleting a branch in GitHub is the process of removing a Git branch reference from your local machine or remote repository.

You need this when feature branches pile up after merging, when abandoned code clutters your codebase, or when stale branches create confusion during code reviews.

This guide covers 4 methods requiring 1-3 minutes each. Basic familiarity with Git and terminal commands recommended.

Prerequisites

  • Git version 2.0+ installed on your local machine
  • A GitHub account with write or admin repository permissions
  • Terminal access (Git Bash, Command Prompt, or macOS Terminal)
  • Time estimate: 1-3 minutes per deletion method
  • Skill level: Beginner to intermediate

Verify your Git installation by running git --version in your terminal.

Why is GitHub the heart of open source?

Uncover GitHub statistics: developer community growth, repository trends, collaboration patterns, and the platform that powers modern software development.

Explore GitHub Data →

Step One: How Do You Delete a Local Branch in Git?

The git branch -d command removes a branch from your local repository. Run it from your terminal inside the project folder. You get a confirmation message showing the deleted branch name and its last commit hash.

Action

  1. Switch branches first: git checkout main (you cannot delete the branch you are on)
  2. Delete merged branch: git branch -d branch-name
  3. Force delete unmerged: git branch -D branch-name

Expected result: Deleted branch branch-name (was abc1234).

Purpose

Local branch deletion keeps your development environment clean. The -d flag performs a safe delete that checks for unmerged changes. Use -D (capital) to force delete branches with unmerged commits.

Step Two: How Do You Delete a Remote Branch Using Git Commands?

The git push origin --delete command removes a branch from your remote repository. Execute this from your terminal after navigating to the project directory. The branch disappears from GitHub immediately.

Action

  1. Open terminal in your repository folder
  2. Run command: git push origin --delete branch-name
  3. Alternative syntax: git push origin :branch-name

Expected result: To github.com:username/repo.git - [deleted] branch-name

Purpose

Remote branch deletion through CLI gives you precise control. Works well in continuous integration workflows and automated scripts. The origin reference points to your default remote server.

Step Three: How Do You Delete a Branch Through GitHub Web Interface?

GitHub’s browser interface lets you delete remote branches without touching the command line. Navigate to your repository’s branches page and click the trash icon. The deletion happens on the server side only.

Action

  1. Go to: github.com > Your Repository > Code tab
  2. Click: “X branches” link below the repository description
  3. Find your branch: locate it in the list
  4. Click trash icon: the red delete button next to the branch name

Expected result: Branch removed from the branches list with option to restore.

Purpose

The web interface method works when you lack terminal access. Good for quick cleanups and non-technical team members. Remember this only deletes the remote branch, not your local copy.

Step Four: How Do You Enable Automatic Branch Deletion After Merge?

GitHub can automatically delete head branches after pull requests merge. Configure this once in repository settings. Saves manual cleanup across your entire team.

Action

  1. Navigate to: GitHub.com > Repository > Settings
  2. Scroll to: Pull Requests section
  3. Enable: “Automatically delete head branches” checkbox

Expected result: Merged branches auto-delete when pull requests close.

Purpose

Automatic deletion reduces repository clutter in active software development workflows. Works well with GitHub Actions and continuous deployment pipelines.

Verification

Confirm Local Branch Deletion

Run git branch -a to list all branches.

Your deleted branch should not appear in the output.

Confirm Remote Branch Deletion

Run git fetch --prune to sync local references with remote.

Check GitHub.com > Repository > branches page. The branch should be gone from the list.

Troubleshooting

Cannot Delete Branch Checked Out

Error: error: Cannot delete branch 'branch-name' checked out at '/path'

Fix: Switch to a different branch first using git checkout main, then retry deletion.

Remote Ref Does Not Exist

Error: error: unable to delete 'branch-name': remote ref does not exist

Fix: Run git fetch --prune to clean up stale remote-tracking references. Someone may have already deleted the branch.

Branch Has Unmerged Changes

Error: error: The branch 'branch-name' is not fully merged

Fix: Use git branch -D branch-name (capital D) to force delete. Or merge the branch first if you need those changes.

Permission Denied

Error: remote: Permission to repo denied

Fix: Verify write or admin access in GitHub > Settings > Collaborators. Check branch protection rules that may block deletion.

Alternative Methods Comparison

MethodTimeScopeBest ForKey Advantage
Git CLI (Local)10–20 secLocal OnlyQuick daily cleanupDeletes your local copy without affecting the server.
Git CLI (Remote)20–30 secRemote OnlyManual server maintenanceEssential for removing “dead” branches from the cloud.
GitHub Web UI< 1 minRemote OnlyVisual learnersInstant deletion via the “Delete Branch” button after a PR.
Auto-DeletionPermanentRemote OnlyHigh-velocity teamsZero effort; GitHub automatically prunes merged branches.

CLI methods offer scripting capabilities. Web interface provides visual confirmation. Automatic deletion works passively after initial configuration.

Recovering Deleted Branches

Restore Local Branches

Use git reflog to find the commit hash of your deleted branch tip.

Run git checkout -b branch-name commit-hash to recreate the branch.

Restore Remote Branches

GitHub keeps deleted branches tied to closed pull requests.

Go to Pull Requests > Closed > find your PR > click “Restore branch” button. Only works if a pull request existed for that branch.

Related Processes

FAQ on How To Delete A Branch In Github

Can I delete a branch I am currently on?

No. Git prevents you from deleting the branch you have checked out. Switch to another branch first using git checkout main or git switch main, then run the delete command.

What is the difference between git branch -d and git branch -D?

The lowercase -d performs a safe delete that fails if the branch has unmerged changes. The uppercase -D forces deletion regardless of merge status. Use -D carefully since you may lose uncommitted work.

Does deleting a remote branch delete the local branch too?

No. Remote and local branches exist independently. Running git push origin --delete branch-name only removes the branch from GitHub. You must separately run git branch -d branch-name to remove your local copy.

How do I delete multiple branches at once?

List branches separated by spaces: git branch -d branch1 branch2 branch3. For remote branches, repeat the push command or use a loop in your terminal. Some development IDEs offer batch deletion features.

Can I recover a deleted branch?

Yes, usually. Use git reflog to find the last commit hash, then run git checkout -b branch-name hash. On GitHub, branches tied to closed pull requests show a Restore branch button.

Why does my deleted branch still appear in git branch -a?

Your local repository caches remote branch references. Run git fetch --prune or git remote prune origin to sync. This cleans up stale remote tracking references that no longer exist on GitHub.

Can I delete the main or master branch?

GitHub blocks deletion of the default branch. Change the default branch in Settings > Branches first if you need to delete it. Most Git workflows keep main or master as a protected, permanent branch.

What happens to pull requests when I delete a branch?

Open pull requests stay visible but show the source branch as deleted. Merged pull requests remain unaffected in history. GitHub preserves the commit history regardless of branch deletion.

Do I need special permissions to delete branches?

You need write access or higher to delete remote branches. Repository admins can set branch protection rules that restrict deletion. Check Settings > Branches for any protected branch configurations blocking your action.

Should I delete branches after merging?

Yes. Merged branches serve no purpose and clutter your repository. Enable automatic deletion in GitHub settings for hands-off cleanup. Regular branch management improves code review efficiency and keeps your branch list readable.

Conclusion

You now have four reliable methods to delete a branch in GitHub. Terminal commands, web interface, or automatic cleanup after pull requests. Pick what fits your workflow.

Regular branch management prevents repository clutter. It keeps your team aligned and your source control history clean.

Quick recap:

  • Use git branch -d for local branches
  • Use git push origin –delete for remote branches
  • Enable automatic deletion in repository settings for merged branches
  • Run git fetch –prune to sync stale references

Deleted something by mistake? The git reflog` command and GitHub’s restore button have you covered.

Clean repositories lead to smoother DevOps pipelines and faster feature delivery. Start pruning those old branches today.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Delete a Branch in GitHub
Related Posts