How to Delete Git Branches On Local and Remote Repositories

Join 5,000 subscribers and get a periodic digest of news, articles, and more.
By submitting your email, you agree to the Terms of Use and Privacy Policy.
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon’s AWS platform. He’s written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read more…

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.
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.
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.
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:
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:

The above article may contain affiliate links, which help support CloudSavvy IT.
Facebook
Twitter
LinkedIn
RSS Feed
Cloud wisdom in your inbox
By submitting your email, you agree to the Terms of Use and Privacy Policy.

source

Digital Strategist Chris Hood

Leave a Reply

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

© 2022 SHAQ HAX - Proudly powered by theme Octo