#How to Delete Git Branches On Local and Remote Repositories – CloudSavvy IT

Table of Contents
“#How to Delete Git Branches On Local and Remote Repositories – CloudSavvy IT”

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.
git checkout master
Then, you can use git branch
with the -d
flag to delete a branch:
git branch -d branch_name
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:
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 push
with the -d
flag, 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 fetch
with the --prune
flag, 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:
If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.
For forums sites go to Forum.BuradaBiliyorum.Com
If you want to read more like this article, you can visit our Technology category.