How to Delete Git Branches On Local and Remote Repositories (2024)

Quick Links

  • Why Delete Branches?

  • Delete Local Branch

  • Delete Remote Branch

  • Automatically Deleting Github Pull Request Branches

Branches are a core part of Git workflows, being used to keep unfinished code out of the master codebase. However, if you work with them a lot, you may have a bunch of unused branch references in your branch listing that need to be deleted.

Why Delete Branches?

It's important to understand how branches work before you go about deleting them. First off, you're not really deleting any code---you're deleting the branch reference, which can still lead to data loss in some cases, but for most workflows is entirely harmless.

You can think of Git commits like a chain pointing backward, constructing your Git history all the way back to the beginning of the repo (it's technically stored as complicated blobs and trees internally, but this analogy works in most cases).

Feature branches are forks in this chain, creating two diverging histories. When you create a commit on a new branch, a branch reference is created, and set to point towards that commit. Merge commits bring them back together, and also move the

feature 

label to be in line with

master 

.

How to Delete Git Branches On Local and Remote Repositories (1)

At this point,

feature 

and

master 

are both doing the same thing. You can delete the

feature 

reference, and no code is lost. If you don't intend to reuse this branch, it's best practice to just delete it.

However, this can lead to data loss if the branch reference points to commits which have not been merged into the

master 

timeline (or another branch). Git will recognize that these commits are no longer needed, since there isn't an active branch reference that includes them, and clean them up when it does garbage collection.

You can also opt to not delete branches. However, the branch reference will not move with

master 

, which will continue chugging ahead. If you make a new commit in the feature branch, it will create a "new branch" in the history, which can be merged, but may be severely out of date.

How to Delete Git Branches On Local and Remote Repositories (2)

For this reason, unless you're constantly merging two branches, it's best to delete feature branches after they're merged to clear up your history.

Delete Local Branch

Git won't let you delete the branch that the HEAD is on, so you will need to switch your working branch back to

master 

. Unless, of course, you want to delete

master 

for some reason.

git checkout master

Then, you can use git branchwith the -dflag to delete a branch:

git branch -d branch_name

Because of the way Git handles branches, this command can fail under certain circ*mstances. Git actually keepsthree branches for each "branch": the local branch, the remote branch, and a remote-tracking branch usually named origin/branchname.

The -dflag will only delete branches that have been pushed and merged into the remote tracking branch. This is usually what you'd want for normal operations, but if you want to forcibly delete it, you can run the same command again with a capital -Dflag:

git branch -D branch_name

Keep in mind that this will lead to data loss if the commits are unmerged.

Delete Remote Branch

Deleting branches on the remote is easy as well.To delete remote branches, run git pushwith the -dflag, which will cause the branch to be removed if you have access to do so.

git push origin -d branch_name

Depending on your Git provider, like Github or Bitbucket, you may have to enable branch deletion in the online settings, and there may be tools there to handle the deletion instead.

This won't send the changes to client machines however, until they run git fetchwith the --pruneflag, which will clean up old branches:

git fetch --all --prune

Automatically Deleting Github Pull Request Branches

Github actually has a nice feature for repositories to automatically clean up branches created from pull requests. You can turn it on in the settings, under Options > Merge Button:

How to Delete Git Branches On Local and Remote Repositories (3)
  • Programming

Your changes have been saved

Email Is sent

Please verify your email address.

You’ve reached your account maximum for followed topics.

Manage Your List

Follow

Followed

Follow with Notifications

Follow

Unfollow

Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

How to Delete Git Branches On Local and Remote Repositories (2024)
Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5332

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.