Why is there no master in my git branch?

I am new to git and I continue to read about the "master" branch. Is "master" just an ordinary name that people used or has a special meaning, for example HEAD ?

When I make a git branch on the clone that I have, I see only one branch - the one on which I am included. There is no โ€œmaster" at all. If I type git checkout master (as I see in many tutorials or manuals), I get

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

I am just confused why my clone does not have master , which apparently implies that it always exists.

+68
git
Sep 02 '10 at 4:08
source share
8 answers

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

However, if you are cloning a repository, you have a default branch that the remote HEAD points to ( HEAD is actually a symbolic ref pointing to the name of the branch). So, if the cloned repository had a HEAD specified by, say, foo , then your clone will have a foo branch.

A remote user that has cloning can still have a master branch (you can check with git ls-remote origin master ), but you would not create a local version of this branch by default, because git clone only checks the remote HEAD .

+54
Sep 02 '10 at 4:10
source share

To check for a branch that does not exist locally but is in a remote repo, you can use this command:

 git checkout -t -b master origin/master 
+68
Jan 24 '14 at 11:01
source share

master is just the name of the branch, there is nothing magical about it, except that it is created by default when creating a new repository.

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

+32
Sep 02 '10 at 4:12
source share

I had the same problem with a completely new repository. I even tried creating one with git checkout -b master , but it did not create a branch. Then I realized that I made some changes and made them, git created my main branch.

+20
Aug 21 2018-12-21T00:
source share

In my case, there was a development branch, but not the main branch. So I cloned the repository, pointing the newly created HEAD to an existing branch. Then I created the missing leading branch and updated HEAD to point to the new main branch.

 git clone git:repositoryname --branch otherbranch git checkout -b master git update-ref HEAD master git push --set-upstream origin master 
+11
Jun 04 '14 at 9:50
source share

If this is the new repo you cloned, it could still be empty, in which case:

git push -u origin master

should probably figure it out.

(done in my case. not sure if this is the same problem, thought i should post this just incase. may help others.)

+9
Mar 21 '13 at 17:48
source share

I ran into the same problem and figured out this problem. When you initialize the repository, there are actually no branches. When you run the git add . project git add . and then git commit , and the main branch will be created.

Without checking anything, you do not have a main branch. In this case, you need to follow the steps that other people have suggested.

+4
Nov 24 '15 at 23:44
source share

There seems to be at least one local commit on the main branch:

 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 "..." 
0
Jan 24 '17 at 22:47
source share



All Articles