If you’re working on a project, you can hardly do without a version control system, and git is the very best. In fact, it’s so easy to use that you might be tempted to create various branches to test different features – which is a good thing!
The ability to test your changes in git without any consequences to the main branch is one of git’s most powerful features. In fact, it’s so easy that you might even go overboard and end up with several branches filled with half-built, abandoned, or merged features that are no longer necessary. To clean things up, you should regularly do some housekeeping and delete the local git branches you no longer need.
Here’s how to do it, why it might be necessary, how to check the status of your branches, and how to push through the deletion even with uncommitted changes. Along the way, I’ll demonstrate with you several examples to show you how it’s done on my VPS server.
Understanding Git Branches
A git branch is a separate thread of development away from the main branch. If you’re the sole developer on a project, you can get away with working on a single branch all the time – I know I did. But when you have several people working on their specific area of the project, having branches is mandatory.
Let’s say that you have a separate development team working on adding some business logic, while you work on a UI issue. You can’t both work at the same time, because the code each of you will test will be constantly changing based on the actions of someone else, and you might not only affect each other, but your testing could stall when the other party introduces unexpected bugs. You simply can’t have multiple people working on different features on the same branch at the same time.
The solution is simple – separate branches for each team. This way, each team works on its own local copy of the code. Even when they commit changes, they only commit them to their unique branch. After testing and debugging, if they’re finally ready to introduce their completed features into the main branch, then doing so is easy after they commit their changes.
Why You Might Want to Delete a Local Branch
For projects lasting a long time – years, even – it’s incredibly easy for local branches to clutter up the tree, making it hard to get a clear idea of which parts of development are happening currently, and by whom. We’ve seen before when talking about squashing git commits, that maintaining a git repository properly is important so that you can get a snapshot of the history of the project – something that’s particularly important if people are moving in and out of development teams.
Branches are so easy to create, that you might even forget that you created them! Sometimes, a developer will create a branch and then get busy working on something else, leaving it behind as part of the project. Often, the scope of a new feature will change midway, and once again, a branch can be left hanging. Sometimes when there’s no clarity on whether or not a particular feature will make it into the main project, a branch can be left in limbo for a long time, until everyone has forgotten its existence!
All of the above have implications for efficiency. Especially for large projects, having vast swathes of useless code can bloat the project. And if you have tools that work with all branches simultaneously, like say a GUI that displays all the branches, then they can become unwieldy very quickly.
All this is to say that it’s important to know how to delete local branches in git. And that’s what I’m going to show you in this next section.
Creating and Deleting a New Local Git Branch
In my test example, I first create a new local git branch by typing:
git branch add_new_file
Now I switch to the new branch via:
git checkout add_new_file
Finally, I added a new file to the branch as shown here:
As you can see from the screenshot, the “git checkout” parameter ensures that you’re working in the right branch. If at any point in time, you want to know which branch you’re working on, simply type:
The output will show you the list of branches, with an asterisk (*) next to the one you’re currently in, and also show it with a different color like this:
Now that I’ve created the new file, I can add and commit the changes as shown here:
This last step is important, because if you don’t commit your changes, then any changes you make might “travel” with you to another branch and interfere with uncommitted changes to those files if someone else is working on them. So always make sure that you commit your changes before switching branches.
Merging the Two Branches
If you’re done making changes to the new branch and want to merge this branch into the master branch, you have to first switch to the master branch first like this:
Note that in the “master” branch, the new file I created in the other branch doesn’t exist. Now you can merge the two branches using the command:
git merge add_new_file
The output is as shown below:
After merging the two branches, you can see how the new file is now available in the master branch. All’s well that ends well, right?
But the Old Branch Still Exists!
Based on the above, it’s easy to see how easily one can forget about the existence of the merged branch. After all, you’re not longer even in it anymore, and if you start on other tasks, it’s extremely likely that you’ll forget about its existence.
Now imagine a project with dozens of developers, each of whom creates new branches to either merge or abandon, and you can see why it’s so easy for branches to proliferate. Which brings us to the final point.
How to Delete Local Git Branches
Despite the above, deleting a local git branch is incredibly easy. First, you have to ensure that you’re not in the branch being deleted. In our above example, this is easy. If you’ve already merged a branch into the master, then you’re no longer in it. To delete the branch, simply type:
git branch -d add_new_file
This has the following output:
It’s that simple! The only caveat is that if you have committed changes in the other branch that haven’t been merged, git will prevent you from deleting the branch using the above command. If you’re sure you want to get rid of it, replace “-d” with “-D” instead as shown here:
git branch -D add_new_file
This will force git to delete the other branch even if you haven’t merged the changes. Make sure that this is what you want, first!
Git branches are one of the most powerful features of git. It’s so easy to create a new local branch, that you can forget they even exist and neglect their cleanup. However, projects can get messy over time with undeleted branches, so make sure that you practice some project hygiene and delete the local git branches when they’re no longer useful!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!