Git Delete Branch: Remove Local and Remote Git Branches

Git Delete Branch

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.

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 for Git: 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 and git 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?

Yes, if you know the commit hash. Use
git checkout -b branch-name hash
.

Why does Git not delete a branch with -d?

Because it is not merged. Git protects your work by blocking the delete.

What does --delete do in git push?

It tells the remote to delete a branch.

Is it safe to use -D?

Use it only if you are sure you no longer need the branch.

Do deleted remote branches show up in GitHub?

No. GitHub removes the branch once it is deleted with git push origin --delete.

How do I delete a local Git branch?

Use this command:
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

How to Delete a Branch Locally in Git

You sometimes need to remove old or unneeded branches locally in Git. You might want to keep your project clean…

What is Version Control System? Software List Explained

One of the tools we need for software development is a Version Control System. It tracks code changes, making a…

Git Pull Force: Safely Overwrite Local Changes

If you've worked on any coding project, you would know this frustrating wall where Git simply refuses to pull the…

Git List Branches: How to Show All Local & Remote Branches

You can use Git to show all branches or use branch-related commands to list branches and their status. This helps…

Git commit Command: How it Works with Examples

The commit command in Git records changes in a local repository. It stores a snapshot of files. It also adds…

Git Help Command: A Complete Tutorial for Beginners

The git help command shows built-in guides for every Git command so you can check options with its usage and…

Git config Command: A Complete Tutorial to Configure Git Settings

You can use the Git config command to set user data and control Git behavior. It saves preferences for projects…

How to Delete a Remote Branch in Git

When you work with teams, you often need to remove old remote branches. To delete a remote branch in Git,…

Git Pull: How to Keep Your Code in Sync

When you work on a project with friends, one writes the intro, one adds pictures, and others handle different parts.…

Git Pull Rebase: How It Works, and When to Use It

Git Pull Rebase works to keep your commit history simple. Git Pull Rebase helps you reduce merge noise in your…

Previous Article

JavaScript Hoisting: How It Works with Examples

Next Article

JavaScript Functions: How to Create Functions in JavaScript

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.