The more you contribute, the more branches you'll create. It never seems like it when you create your first branch, but eventually you're going to have a lengthy list of branches. You it's simple to avoid this by deleting your branches as you go.

You must be certain you're ready to delete your branch before deleting it. Deleting your branch does not delete your work AS LONG AS YOUR WORK HAS BEEN MERGED. If you have unmerged work on your branch, DO NOT DELETE IT.

Do not delete your branch if you have changes that have not been merged. YOU WILL LOSE THOSE CHANGES.

Keep in mind that you have two locations from which branches require deletion: remote and local. Your remote branches are located on GitHub once you push them. Your local branches are located on your computer when you create them.

Deleting Your Remote Branch

After your PR is merged, there is typically a notification at the bottom that tells you it's now safe to delete your branch, with a button to delete it. If you click this button, it will delete the branch from GitHub, but not from your local copy.

As you can see, when you use the link to delete it, it gives you the option to restore it.

If you're not around when your branch is merged, you can still delete your remote branch from GitHub. Navigate to the main page of your repo, and locate the # branches tab, where # is the number of branches you currently have on your repo. In my case, it's 7. Click the link to the tab.

Here you'll find multiple lists of branches. The first is Your branches, which contains every branch you have on your repo. The second is Active branches, which contains branches that were recently updated. The third, if you haven't kept up with deleting branches as you go, is Stale branches, which contains a list of branches that haven't been updated in a while.

You can delete from any of these lists by clicking the trash bin icon delete button located on the right side on the same line as the branch name. I would like to delete my most recent branch, so I'm going to delete it from Your branches by clicking the trash bin icon.

Once you delete the branch, you'll have an opportunity to restore it by clicking the restore button that appears. This line will disappear from your page once you refresh, so be certain you meant to delete that branch before leaving the page.

Now you've deleted your remote branch, but it's still in your local repo. Next, we'll go over how to delete a branch from your local repo.

Deleting Your Local Branch

Open your terminal program and navigate to your repo folder. You'll want to make sure you're on the master branch, so first run the following checkout command:

git checkout master

If you'd like to see a list of your current local branches, you can run git branch.

Next, you'll want to delete your branch. You can use tab-completion to complete your branch name. To do this, start typing the beginning of the name at the end of the command, and then hit tab.

To delete your branch, run the branch delete command, replacing your-branch-name with the name of the branch you're deleting:

git branch -d your-branch-name

Note that in some circumstances, this command will not delete your branch. One situation where it will fail is if you have changes that haven't been merged. Consider the command provided as a way to be sure you don't delete any unmerged changes.

If you are CERTAIN that you do not want to keep the changes in a given branch, you can run the same command with -D, and it will force deletion of the branch. However, sticking with -d ensures that you do not accidentally delete unmerged changes.

That's it! You're all set. You've deleted your branch locally and remotely. You're ready to create a new branch and get started on your next contribution!

This guide was first published on Nov 25, 2020. It was last updated on 2020-11-25 13:48:06 -0500.

This page (Keeping Branches Trimmed) was last updated on Nov 25, 2020.