How do Git branches work? Can I remove the master branch?

22

9

I have a project that has 2 branches, development and production. I'm not using the master branch and each time I write git status it tells me how far ahead my branch is from the master.

When I try to delete the master branch git branch -d master it deletes it only on my local repository and not on the remote git server. Any ideas?

ufk

Posted 2009-12-08T14:04:23.793

Reputation: 1 055

Answers

34

In Git, a branch is just an ordered list of commits (a.k.a.: checkins). Something that can be a bit confusing for new users is that branches don't need to have a name (although in most circumstances you want one); and there is nothing particularly special about any particular branch (the master branch is just the default one that's created for you when you initialize a repository).

You probably already know this, but Git is different than some other version control systems like the popular "Subversion", because every "working copy" (in Subversion language) is a repository of it's own... in fact, there is nothing particularly special about any particular copy; except that one copy has been generally agreed-upon as the "canonical" one that is used to store the final product.

So, back to your question... the "canonical" repository that you cloned when you started your local copy contained a "master" branch by default; and it's stuck around. Now, if you had access to the computer that contains the master repository you could log in and run:

git branch -d master

However, if you aren't able to do that, you can still do it from your local machine. The git branch command has a -r option which affects the remote repository. In other words, running the following command should work:

git branch -d -r master

Note that in both of those cases; I am assuming that master has been completely merged into the development history that your local copy is currently sitting at. If you have never used master before (i.e.: you've only ever checked in to development or production), you have nothing to worry about. However, if you (or someone else) has been checking things in to master, then you might have a problem. You can force a delete by changing the -d to -D in the above commands; but I highly recommend checking to see what's in master beforehand! If you don't have access to the remote computer, you probably won't be able to recover it!


By the way; if you (or anyone else) is new to Git, I highly recommend reading Git from the Bottom Up by John Wiegley. Even though I had used Git a little bit on my own before found this article, I didn't really understand how it worked until I read it. It is quite useful!

mparker17

Posted 2009-12-08T14:04:23.793

Reputation: 531

Should also note that when you clone a repository you normally default to checking out master. This depends on the HEAD ref in the repository you are cloning. So after master is deleted, make sure the HEAD points to the branch you want as default. – Zitrax – 2016-06-03T12:11:51.157

Your link to Git from the Bottom Up has died, but it seems this is the text you are referring to (?) Could you confirm and maybe update the link in the answer?

– O. R. Mapper – 2016-11-23T13:44:33.380

O. R. Mapper: That is the text I'm referring to: thank you for the new link! I've updated the link in the answer. – mparker17 – 2017-08-02T16:37:06.750

3Some time since I wrote my original comment, git's behaviour changed slightly. As of git 1.7.12, git branch -d -r master no longer deletes the remote branch — it deletes your local copy's knowledge of the remote branch. Next time you git fetch, the branch will be back! Instead, you'll want to run git push origin :master. Essentially what you're doing here is pushing a null branch (the empty branch name to the left of the :) over top of the remote branch (the branch name to the right-side of the :), effectively deleting it. – mparker17 – 2012-09-14T14:21:56.827

4

master is the default branch for git. I don't know why it's so terrible for you when git tells you how far away from master you are, but if you want to delete a branch on your remote repository, deleting it locally isn't enough. Try this instead:

git push origin :master

This will push nothing (the part before the colon) to your origin server and overwrite master. In other words, it should delete the master branch remotely.

innaM

Posted 2009-12-08T14:04:23.793

Reputation: 9 208

I get: remote: error: refusing to delete the current branch: refs/heads/master To https://github.com/***.git ! [remote rejected] master (deletion of the current branch prohibited) error: failed to push some refs to 'https://github.com/***.git'

– Totty.js – 2013-02-07T22:13:22.890

1

@Totty perhaps this can help: http://matthew-brett.github.io/pydagogue/gh_delete_master.html

– MEM – 2013-04-12T07:20:26.437