If you need to delete a branch from a local or remote repository, there are a few different ways to go about it. The first way is to use the git branch command. This will list all of the branches in your repository and show you which one you want to delete. To delete the branch, you can use the -d flag. For example, if you wanted to delete the master branch in your local repository, you would type: git branch -d master. The second way is to use the git push command. This will send a deletion request to the remote repository and it will automatically remove the branch from there. To use this method, you first need to know the name of the branch that you want to delete. You can find this information by using the git status command or by looking at the output ofgit branch. Once you have this information, all you need to do is type: git push origin . For example, if you wanted to delete the master branch in your local repository, you would type: git push origin master. ..


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.

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.

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.

Then, you can use git branch with the -d flag to delete a branch:

Because of the way Git handles branches, this command can fail under certain circumstances. Git actually keeps three branches for each “branch”: the local branch, the remote branch, and a remote-tracking branch usually named origin/branchname.

The -d flag 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 -D flag:

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 push with the -d flag, which will cause the branch to be removed if you have access to do so.

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 fetch with the –prune flag, which will clean up old branches:

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: