How to Delete a Branch in GitHub: Local, Remote, and Everything In Between
Branches are one of GitHub's most useful features — they let you experiment, build features, and fix bugs without touching your main codebase. But once a branch has served its purpose, leaving it around creates clutter. Knowing how to delete branches cleanly, and understanding which deletion method applies to your situation, is a core part of working with Git effectively.
What Happens When You Delete a Branch
Deleting a branch in Git doesn't delete the commits that branch contains — as long as those commits were merged into another branch before deletion. What you're removing is the pointer (the branch reference) that tracked that line of work.
This distinction matters. A merged branch can be safely deleted without losing any history. An unmerged branch, however, contains commits that exist nowhere else. Deleting it means losing that work unless you explicitly force the deletion knowing what you're doing.
There are also two separate locations a branch can live:
- Local — on your own machine, in your Git repository
- Remote — on GitHub's servers, visible to your whole team
These are independent. Deleting one does not automatically delete the other.
Deleting a Branch on GitHub (Remote) via the Web Interface 🖥️
The simplest method requires no command line at all.
- Go to your repository on github.com
- Click the "branches" link near the top of the file list (it shows the branch count)
- Find the branch you want to remove
- Click the trash icon next to it
GitHub also prompts you to delete a branch immediately after a pull request is merged. That prompt appears right on the pull request page and is the most common way teams clean up branches during normal workflow.
If you delete a branch by accident through the web interface, GitHub offers a "Restore branch" button for a short period after deletion — useful safety net if you move too fast.
Deleting a Remote Branch via the Command Line
If you prefer the terminal or are working in an automated script, the command is:
git push origin --delete branch-name Replace branch-name with the actual name of the branch. This tells Git to push a deletion instruction to the origin remote (which is typically your GitHub repository).
An older syntax that still works in most Git versions:
git push origin :branch-name The colon before the branch name is Git's way of saying "push nothing to this branch" — effectively deleting it.
Deleting a Local Branch
Deleting the remote branch leaves your local copy intact. To remove the local branch:
git branch -d branch-name The -d flag is the safe delete — Git will refuse to delete the branch if it contains unmerged commits. This protects you from accidental data loss.
If you're certain you want to discard an unmerged branch:
git branch -D branch-name The uppercase -Dforces the deletion regardless of merge status. Use this only when you're sure the work on that branch is no longer needed.
Checking Before You Delete
To list all local branches and see which are already merged into your current branch:
git branch --merged Any branch appearing in that list is safe to delete with -d. Branches not in the merged list will require -D if you want to remove them.
Cleaning Up Stale Remote-Tracking References
Even after a remote branch is deleted, your local Git repository may still hold a stale tracking reference — a leftover pointer that shows up in certain branch listings. To remove these:
git fetch --prune This syncs your local list of remote branches with what actually exists on GitHub, removing any references to branches that no longer exist remotely. Some teams configure Git to prune automatically on every fetch:
git config --global fetch.prune true Factors That Affect How You Should Approach Branch Deletion
| Factor | What It Changes |
|---|---|
| Merge status | Unmerged branches need -D; merged branches use -d safely |
| Team workflow | Shared branches may need coordination before deletion |
| Protected branches | GitHub branch protection rules can block remote deletion |
| CI/CD pipelines | Some pipelines trigger on branch deletion events |
| Access permissions | Repository role determines whether you can delete remote branches |
Branch protection rules deserve particular attention. If a repository admin has protected a branch (like main or production), GitHub will block deletion attempts through both the UI and the command line until protection is lifted. This is intentional — it prevents accidental removal of critical branches.
The Difference Between Deleting and Archiving 🗂️
Some teams prefer not to delete branches at all, instead using tags to mark significant points in history, or simply leaving merged branches in place as a record. GitHub doesn't have a native "archive branch" feature, but the pattern exists — especially in open-source projects where historical context matters.
For most day-to-day development work, deleting merged feature branches keeps repositories clean and navigation manageable. For long-running release branches or branches tied to specific deployments, some teams choose to leave them intact deliberately.
What Determines the Right Approach for Your Situation
The mechanics of branch deletion are straightforward — the command line options are consistent across platforms, and the GitHub UI is direct. But which branches to delete, when to delete them, and whether to prune local references depends heavily on how your team structures its workflow, what branch protection rules are in place, and whether you're working solo or across a larger codebase. The commands behave the same way; the judgment around when to use them is where your specific setup comes into play.