Git helps you manage code in different branches. You can delete a branch when it’s old or not being used. This guide shows how to remove local and remote Git branches correctly.
Table of Content
Delete a Local Branch in Git
Some of the branches are no longer useful. You can delete them to keep your project clean. To delete a branch stored on your local machine, use:
git branch -d branch-name
This command verifies if you have combined the branch into the current branch. Git will block the deletion to prevent data loss if not.
You can make the deletion happen even if it’s not allowed normally if you’re sure you don’t need the branch anymore:
git branch -D branch-name
This skips the merge check and deletes the branch right away.
Use -d
when you want to be safe. Git will warn you if the branch has changes not yet merged.
Use -D
when you are already backed up or do not need the changes. It will delete the branch regardless of any condition.
Delete Remote Branch in Git
Remote branches exist on a shared repository. You can delete them when a feature is complete or no longer useful.
To delete a remote branch, such as when you’ve deleted the test branch locally and want to clean up the server:
git push origin --delete branch-name
This tells Git to remove the branch from the remote server. Other users will no longer see it when they fetch or pull.
Sometimes you delete a branch from the remote but your local copy still lists it. To remove it:
git fetch --prune
This updates your list of remote branches and removes ones that no longer exist.
You’ve now used a common command to delete local and remote branches. The next section explains how local and remote branches differ.
The Difference Between Local and Remote Branch
A local branch lives on your own machine. You can edit it or delete it. Or even merge it.
A remote branch exists on remote repositories, like origin
on GitHub. It helps teams collaborate and track code changes.
Changes in local branches are private until you push them. Remote branches help teams share code and track work.
Handle Error of Branch Not Found
If you try to delete or switch to a branch that does not exist, Git shows:
error: branch 'branch-name' not found.
You can fix this by checking which branches exist. Here is a command shows you the local branches:
git branch
Here is another command that shows you remote branches:
git branch -r
Make sure you spelled the branch name right. If it is a remote branch, fetch updates with:
git fetch --all
Delete Multiple Branches at Once
Sometimes you have many old or merged branches you want to remove. Instead of deleting them one by one, you can remove several at once with a single command.
Here is an example:
git branch -d branch1 branch2 branch3
Replace branch1 branch2 branch3 with your actual branch names.
Use the force option if the branches are not fully merged:
git branch -D branch1 branch2 branch3
You can’t delete remote branches directly with git branch
, but you can do this:
git push origin --delete branch1 branch2 branch3
Or use this alternative syntax:
git push origin :branch1 :branch2 :branch3
Use Git GUI Tools to Delete Branches
You don’t have to use the command line to delete branches. Here’s how to delete branches using popular Git GUI tools:
GitHub Desktop:
- Open the repository in GitHub Desktop.
- Go to Branch > Delete from the top menu.
- Select the branch you want to delete.
- Confirm the deletion.
GitKraken:
- Open your repo in GitKraken.
- In the left panel or graph view, right-click the branch.
- Click Delete branch.
- Confirm when prompted.
Sourcetree:
- Open your repository in Sourcetree.
- Go to the Branches list.
- Right-click the branch name.
- Select Delete [branch name].
- Confirm deletion in the popup.
Visual Studio Code (with GitLens or built-in Git):
- Open the Source Control view.
- Click the branch name in the bottom-left corner.
- Select Manage Branches or open the command palette (
Ctrl+Shift+P
) and search forGit: Delete Branch
. - Choose the branch to delete.
Wrapping Up
In this article, you learn how to delete Git branches on your computer and from a remote server. Here is a quick recap:
- Use
git branch
andgit branch -r
to see which branches exist. - The
git branch -d branch-name
to delete a local branch after you merge it. - Use
git branch -D branch-name
command to delete a local branch. That doesn’t need you to check for a merge. - Use
git push origin --delete branch-name
to delete a remote branch. - The
git fetch --prune
command to update your list and remove deleted remote branches.
Thank you for reading. Click here to see more Git tutorials.
FAQs
Can I recover a deleted branch?
git checkout -b branch-name hash
.Why does Git not delete a branch with -d?
What does --delete do in git push?
Is it safe to use -D?
Do deleted remote branches show up in GitHub?
How do I delete a local Git branch?
git branch -d branch-name
This deletes the branch only if it has been merged. If it hasn’t, Git will block the deletion to prevent data loss.
To force-delete a local branch:
git branch -D branch-name
Similar Reads
You can use Git to show all branches or use branch-related commands to list branches and their status. This helps…
If you've worked on any coding project, you would know this frustrating wall where Git simply refuses to pull the…
If you're diving into coding, your introduction to Git and GitHub will happen in no time. Git's your personal time…
The moment you dive into programming or development, the first thing you are going to bump into is Git. It's…
A Git branch is a lightweight movable pointer to one of your commits. A feature or fix is thought of…
You sometimes need to remove old or unneeded branches locally in Git. You might want to keep your project clean…
One of the tools we need for software development is a Version Control System. It tracks code changes, making a…
The purpose of the Git Push command is to upload local repository changes to a remote repository. Pushing your committed…
Git Pull Rebase works to keep your commit history simple. Git Pull Rebase helps you reduce merge noise in your…
The switch operation in Git will make it easier. Designed to simplify the process for developers, it's safer and more…