Brendan Enrick

Daily Software Development

Deleting a Remote Git Branch

Another bit of git command line that a lot of people struggle to remember is the syntax to delete a remote branch. If you read my post from a couple of days ago, I mentioned a couple of things about deleting git branches. That’s how you delete them locally. If you want to push that deletion up to the remote repository as well, you need to take one additional step.

The command I use to delete remote branches is this:

git push origin :my-branch-name

It’s that “:” that tells it to delete the branch. Yes, it may seem confusing, but there is a reason for it. I’ll explain below for those who are interested in learning more.

Here is a slightly easier syntax, but I don’t like typing the additional characters.

git push origin --delete my-branch-name

Feel free to use this syntax, it’s newer, but you most likely have support for it. It’s been out for years now.

Here is the reason (other than its being shorter) that I like the first syntax better.

If you want to push our a git branch, you use this command:

git push origin my-branch-name

If you wanted it to have a different name remotely than it has locally, you would do this:

git push origin my-local-name:my-remote-name

Which means that if you wanted to push “nothing” over the remote branch with that name you would do this:

git push origin :my-remote-name

Notice how the command is pushing empty over that branch, which is then interpreted as a “delete” by git. That’s how I make sense of it, plus this is a neat little bit of info about the command.

I hope this helps people adopt and use git more easily. If you’re new to git or GitHub, I recommend that you check out my Pluralsight course, GitHub for Windows Developers. The course takes an easy-to-follow approach to getting you set up using Git, GitHub, and GitHub for Windows.

Speaking at CodeMash 2015

I am very grateful to be speaking at my 6th CodeMash. I’ve again been selected to present a precompiler on Software Craftsmanship (a topic I am very passionate about). I hope that everyone can attend my workshops. I’ve got two of them again, so I’ll be spending a full day working with software craftsman coding. We’ll be focusing as always on practicing, pairing, testing, and applying all of these effectively. We’ll work through ways you can use practice to learn new concepts and patterns to improve your skills at building great software.

SpeakerSelection

Once again, Steve Smith and I will be presenting these Precompiler workshops together, so you’re sure to have a blast!

Our sessions are usually held back-to-back, so that you can attend the beginner session in the morning and follow it up with the intermediate session in the afternoon. Last year, our afternoon session had to have extra chairs and tables brought in for all of the extra people who showed up. My goal every year is to put on a workshop that will be the highpoint of your CodeMash! Join us for some great coding, learning, and practicing of your software skills.

If you’re in the Northeast Ohio area and want to learn more about software craftsmanship, you should check out HudsonSC. Our October meeting has been scheduled already. We meet on the third Wednesday of every month in Hudson, Ohio.

Deleting Git Branches Carefully

I noticed someone recently using a “hard delete” in the git command line recently. I commented on it being brave, but it turns out that he didn’t realize there was a different way to delete a branch in git. In case anyone else is wondering the difference, here is a quick tip on it.

In the git command line, you can use the “-D” parameter to delete a branch. It looks like this:

git branch –D my-branch-name

This will delete the branch regardless of whether it’s been merged back in. This can be dangerous, since you may lose changes that you’ve not yet merged elsewhere.

If you want to be more careful with your branch deletion, you should use a lowercase “d” with the “-d” or “—delete” parameter. That would look like this:

git branch –d my-branch-name

This will delete the branch only if you have merged the branch up already. This means that the branch’s changes should have been saved in the parent branch already, so it’s safe to delete. If you try this when it hasn’t been merged, you’ll received a message telling you that the branch was not deleted for this reason.

If you want to see the branches that still need to have their changes merged, you can do that using the following command:

git branch --no-merged

If you want to see the branches that can safely be deleted, because their changes have already been merged upstream, you can use this command:

git branch --merged

I hope you found these quick tips useful while you’re using git. If you’re new to git or GitHub, I recommend that you check out my Pluralsight course, GitHub for Windows Developers. The course takes an easy-to-follow approach to getting you set up using Git, GitHub, and GitHub for Windows.