117

I'm a git newbie and I keep reading about a "master" branch. Is "master" just a conventional name that people used or does it have special meaning like HEAD?

When I do git branch on the clone that I have, I only see 1 single branch - the one I'm on. No "master" at all. If I type git checkout master (as I see in alot of tutorials or guides), I get

error: pathspec 'master' did not match any file(s) known to git.

I'm just confused as to why my clone doesn't have a master that everyone seems to imply that it always exists.

aberrant80
  • 12,815
  • 8
  • 45
  • 68

11 Answers11

91

Most Git repositories use master as the main (and default) branch - if you initialize a new Git repo via git init, it will have master checked out by default.

However, if you clone a repository, the default branch you have is whatever the remote's HEAD points to (HEAD is actually a symbolic ref that points to a branch name). So if the repository you cloned had a HEAD pointed to, say, foo, then your clone will just have a foo branch.

The remote you cloned from might still have a master branch (you could check with git ls-remote origin master), but you wouldn't have created a local version of that branch by default, because git clone only checks out the remote's HEAD.

Amber
  • 507,862
  • 82
  • 626
  • 550
89

To checkout a branch which does not exist locally but is in the remote repo you could use this command:

git checkout -t -b master origin/master
robstarbuck
  • 6,893
  • 2
  • 41
  • 40
Bunyk
  • 7,635
  • 8
  • 47
  • 79
49

master is just the name of a branch, there's nothing magic about it except it's created by default when a new repository is created.

You can add it back with git checkout -b master.

Matt Curtis
  • 23,168
  • 8
  • 60
  • 63
  • `git checkout -b master` just adds a new branch off the current one for me. – nnyby Oct 08 '13 at 19:17
  • 1
    @nnyby `git checkout -b master` will create a `master` branch from whatever `HEAD` is - so if you're on another branch, it will create a `master` branch off that. Except if you already have a `master` branch (which you will, unless e.g. you've deleted it or never committed on it). If you already have a `master` branch, this command will just give you an error. – Matt Curtis Oct 16 '13 at 11:41
  • 2
    My question is: how did I accidentally delete master? – Eric Walker Mar 26 '15 at 23:33
  • @EricWalker `master` is just a branch, which can be deleted with `git branch -d master`. Although git will protect you from deleting the branch you are currently on, there is nothing special protecting the `master` branch. More specifically than that, it's difficult to say how you did it. Perhaps you use your shell's `history` command to take a look? – Matt Curtis Mar 28 '15 at 06:29
  • 2
    Hi @MattCurtis, your answer is currently misleading. `git checkout -b master` will only work when HEAD is set to origin/master. In any other case (e.g. you're on 'develop'), `git checkout -b master` will create a branch named 'master' which is based off the current HEAD position (e.g. from 'develop'). You need to also state which branch will be the bast for the new one. @Bunyk has the correct answer on this thread: http://stackoverflow.com/a/21330943/287109 – AVIDeveloper Jan 05 '16 at 09:46
24

I actually had the same problem with a completely new repository. I had even tried creating one with git checkout -b master, but it would not create the branch. I then realized if I made some changes and committed them, git created my master branch.

eacousineau
  • 3,457
  • 3
  • 34
  • 37
  • 2
    The misleading part is that people say I do have the master, but whatever I try to do I would get error messages saying I don't have the master. (I was trying to create a dev branch while my repo was still empty.) By committing something (any file), the master now came into being, and I was able to go on to doing other things. I tried the things in the other answers, but nothing helped. This one answer here may apply to many people out there. (I read much about hashing in Git. I guess if there is nothing at the beginning, there is nothing to hash.) – Fai Ng Jan 23 '15 at 20:12
20

In my case there was a develop branch but no master branch. Therefore I cloned the repository pointing the newly created HEAD to the existing branch. Then I created the missing master branch and update HEAD to point to the new master branch.

git clone git:repositoryname --branch otherbranch
git checkout -b master
git update-ref HEAD master
git push --set-upstream origin master
Antonio Barbuzzi
  • 646
  • 7
  • 11
13

if it is a new repo you've cloned, it may still be empty, in which case:

git push -u origin master

should likely sort it out.

(did in my case. not sure this is the same issue, thought i should post this just incase. might help others.)

digit
  • 275
  • 1
  • 3
  • 8
9

I ran into the same issue and figured out the problem. When you initialize a repository there aren't actually any branches. When you start a project run git add . and then git commit and the master branch will be created.

Without checking anything in you have no master branch. In that case you need to follow the steps other people here have suggested.

Nick ONeill
  • 7,341
  • 10
  • 47
  • 61
  • 2
    true. do a "git init" followed by "git checkout -b somebranchname" and you'll have no master branch – koem Dec 24 '15 at 08:32
  • This also may be not enough if the folder is empty. So, to make this working you should add some files. This another awkwardness is actually disappointing. – alehro Jun 01 '16 at 07:55
1

It seems there must be at least one local commit on the master branch to do:

git push -u origin master

So if you did git init . and then git remote add origin ..., you still need to do:

git add ...
git commit -m "..."
Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
0

If you create a new repository from the Github web GUI, you sometimes get the name 'main' instead of 'master'. By using the command git status from your terminal you'd see which location you are. In some cases, you'd see origin/main.

If you are trying to push your app to a cloud service via CLI then use 'main', not 'master'.

example: git push heroku main

Ajiroghene
  • 127
  • 1
  • 9
0

I had the same problem. I executed the "git init", but the main/master branch was not created. Possibly because I used a branch with another name and this one became the default. The solution that worked for me was:

  1. I created the branch "main"

git checkout -b main
  1. I went to the option Settings->Branches of my directory and changed it to the new branch "main" enter image description here

  2. With this change, Github itself notified me that my default branch (main) was behind my other branch and opened the window for me to confirm a PR applying the changes to the branch main. And following this, merging the pull request. enter image description here

0

Go to your GitHub settings and set master as your default and not main if you want it that way . Hope that helps

Kipruto
  • 721
  • 6
  • 16