Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we are going to learn how to delete a remote branch. Essentially, what we are going to do is be able to tell GitHub that it should erase one of the branches in its repo. First, let's decide what branch we want to get rid off. You can see that I am inside my lynda_version folder on the master branch, if I do git branch -a, you can see that I have got this non_tracking branch locally, and I have also got non_tracking up on the remote repository. That's the one that I want to tell GitHub to delete. There are two ways that we can do this. First, I want to show you the older and less intuitive way, then I will show you a newer one after that.
The first way is to use git push and just like we did before when we are using git push, we push to the remote, and when we did it before, we did it like this, we said, push the contents of non_tracking up it the server. In order to delete it, you put a colon in front of it. Git push origin, and a :non_ tracking will have the effect of deleting the branch on the remote server. Let's try it. So it wants my username, my password, it comes up, and it says that it deleted it, and now if we say git branch -r, you can see that it's gone, and if we go to GitHub, if you have Firefox, reload the page, and sure enough under Branches, you can see that we only have a master branch now.
Now notice that if I do git branch for my local branches, it's still here. All I did was a push up to origin, I didn't do anything to my local branches, so I still have all that information. Why this awkward non-intuitive way of doing it? What's the colon all about? Let me give you some insight into that. When we did our original push of the branch up to origin, we did it like this, git push origin/non_tracking. That's actually shorthand for git push origin non_tracking:non_tracking.
What this is saying is push to origin my local branch non_tracking, to the remote branch called non_tracking. That's what it's doing. When we only have one, it assumes that they are the same, which they often are. But this colon divides those two. So when we are doing a delete, what you are actually doing is saying, push to origin nothing up to the branch non_tracking. So that's why that colon is there. That's where it comes from. But that's not very intuitive.
Let me show you the newer way, this is a little easier to remember. First, let's push that branch back up there again, remember it is going to push my local branch non-tracking up to the remote repository, so there it is now, git branch -r, you can see it's there. Now let's do another delete, git push origin again, but this time it's --delete and then non_tracking. It requires a little more typing, but it is a little easier to remember, a little more intuitive. So now we hit Return.
It's going to want me to log in again, and once again, it's deleted it. So there it is now it's gone. So that's all there is to being able to delete a remote branch.
Get unlimited access to all courses for just $25/month.Become a member