When you start working on a project with multiple people who are all making multiple branches it can get to the point where you have a humongous list of branches, sometimes to the point where it's totally unmanageable. If your project is on github, you will see a huge list of possible branches in the dropdown box. This is something that really bothers me. Luckily, there's a pretty way to get rid of the unused ones, and in this post I'll show you show to do just that.
If you would like to follow along with me and branch deleting a branch, you can! Let's just create a new test branch so that we can mess round with it. You can create a new branch named "testes-branch" like this:
With the above code you've just created a new branch locally, but you haven't let your repo know about it yet. To do that you can push your new branch like so:
Note that you don't need to make any commits or changes to the code itself in order to push your new branch here. After you've pushed the branch, you should be able to see it in the dropdown box on Github:
Ok, you've created the new branch and pushed it up to the repo. Now, you can't delete a branch that you are currently on so let's switch back to master like so:
This is how you delete a branch remotely:
Simply run the git push command and prefix the name of the branch with a colon. This instructs git to delete the branch remotely. After running that you should no longer see the branch listed in the dropdown on Github. Booyah!
If you only delete locally, even if you push your changes back up, it won't actually delete the branch for good (and it won't be removed from the dropdown box on github). The key to really cleaning up after yourself is then to remotely delete your branches when you are finished with them.
Although the above code deletes the branch remotely, you may notice that locally you still have a bunch of old branches. You can view your local branches like so:
You can then locally delete a branch like this:
Note that you can use the capital D flash (-D instead of -d) to force the deletion of the branch.
It should also be noted that another really easy way to delete a branch remotely is through the "delete branch" button that appears on the github.com web interface after you accept a pull request and merge the branch into it's upstream parent branch. This is a good practice since in general you want to keep your branches focus on a certain thing and then open a pull request when that thing is finished. Then you can start working on a different feature in another branch while someone else reviews your pull request. This is just a nifty little shortcut for deleting your no-longer-needed branches. Ultimately, it's up to you to keep your repo clean!
The posts on this site are written and maintained by Jim Lynch. About Jim...
Want FREE access to
my daily stock tips
Sign up here:
- Triple Gainers
- Rippers N' Dippers
- Growingest Growers