Managing your GitHub repository involves not just pushing and merging code but also cleaning up unused or outdated branches. Whether you’re working solo or in a team, knowing how to delete a branch in GitHub is essential for keeping your workspace tidy and your repository healthy. This detailed guide walks you through the process of deleting branches—both locally and on GitHub—while addressing what you should consider before you delete anything.
Why Delete a Branch in GitHub?
Unused branches in GitHub can clutter your repository and lead to confusion. Removing branches you no longer need is a good practice for several reasons:
- You’ve already merged the branch into main or master
- The branch is outdated or experimental
- You want to declutter your GitHub UI
- To avoid accidental use or conflicts in the future
Deleting branches is a safe and common part of Git workflow, especially after completing a feature, bug fix, or hotfix.
What to Know Before Deleting a Branch
Before diving into deletion, keep the following in mind:
- Merged branches are usually safe to delete.
- Unmerged branches may contain code not present in other branches.
- Local and remote branches are separate—you must delete them individually if needed.
- Deleting a branch in GitHub does not affect your working directory unless you’re currently on that branch.
Let’s break down how to delete both local and remote branches with examples.
How to Delete a Local Branch in GitHub
A local branch exists only on your system. Here’s how you can delete it:
Using Git Command Line
- Open Terminal or Command Prompt.
- Run the following command:
nginx
CopyEdit
git branch -d branch-name
This command is used when the branch has already been merged. If you haven’t merged and still want to delete it, use:
mathematica
CopyEdit
git branch -D branch-name
-D is a shortcut for –delete –force, meaning it will delete the branch even if it hasn’t been merged.
Example
bash
CopyEdit
git branch -d feature/login-page
If you try to delete an unmerged branch with -d, Git will warn you and block the operation.
How to Delete a Remote Branch in GitHub
To delete a branch from the GitHub server, you’ll need to push a delete request.
Git Command to Delete Remote Branch
perl
CopyEdit
git push origin –delete branch-name
This command removes the branch from the origin (which usually refers to GitHub).
Example
perl
CopyEdit
git push origin –delete feature/login-page
After this, the branch will no longer appear on GitHub under the branch list.
Alternative: GitHub UI
You can also delete a branch from the GitHub web interface.
Steps to Delete a Branch on GitHub Website:
- Go to your repository on GitHub.
- Click the Branches tab.
- Locate the branch you want to delete under the “Active” branches section.
- Click the trash bin icon next to the branch name.
- Confirm the deletion when prompted.
Note: You cannot delete the default branch (usually main or master) from the web interface.
How to Delete a Merged Pull Request Branch
If a pull request has been merged successfully, GitHub will often offer a button to delete the branch right from the pull request page.
Steps:
- Go to the Pull Requests tab.
- Click on the closed (merged) pull request.
- Look for the option: “Delete branch”.
- Click to remove it from the remote.
This is a safe and recommended way to clean up after a successful merge.
How to Confirm If a Branch Has Been Merged
To avoid losing unmerged work, verify if the branch was merged.
Run this command:
css
CopyEdit
git branch –merged
This shows all branches merged into your current branch.
Or use:
nginx
CopyEdit
git branch –no-merged
To list unmerged branches—review these before deletion.
Handling Protected Branches on GitHub
Some branches, like main or release branches, might be protected on GitHub, meaning you can’t delete them without changing settings.
To delete a protected branch:
- Go to the Settings tab of your repo.
- Click Branches under “Code and automation.”
- Find the protection rule and click Delete or Edit to remove protection.
- Then proceed with the deletion via UI or command line.
Be cautious: only remove protections if you’re sure about deleting the branch.
GitHub Desktop: How to Delete a Branch
If you’re using GitHub Desktop, you can delete branches easily from the GUI.
Steps:
- Open GitHub Desktop.
- Go to Branch > Delete in the top menu.
- Select the branch you want to delete.
- Confirm deletion when prompted.
This only deletes the local copy. To delete it on GitHub, push the deletion using the command line or go to the web interface.
Why Deleting Branches Helps Maintain a Clean Workflow
Here’s why developers recommend cleaning up branches:
- Avoid confusion about which branch to use
- Prevent bugs due to outdated code
- Keep CI/CD pipelines clean
- Make your repository easier to navigate
- Reduce load on your Git server and interface
Especially in large teams, branch hygiene plays a big role in productivity.
FAQs About Deleting a Branch in GitHub
How do I delete a branch in GitHub after merging?
Go to the pull request page on GitHub and click “Delete branch” or run:
git push origin –delete branch-name
Can I delete the branch I’m currently on?
No. Git requires you to switch to another branch before deleting the one you’re on. Use git checkout main first.
What’s the difference between -d and -D in git branch delete?
-d deletes merged branches. -D forces deletion even if unmerged. Use with caution.
Can I recover a deleted branch in GitHub?
If recently deleted, GitHub may allow restoration via the pull request page. Locally, you might recover using the branch’s commit SHA.
How do I delete a branch on GitHub without affecting others?
As long as you’re deleting a feature branch that’s already merged or unused, others won’t be affected. Avoid deleting shared development branches without notice.