Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

got 'fatal: branch 'master' does not exist' in git

Tags:

git

git-branch

using git for a while, it's very powerful and beautiful..

but also got some confused about it:

it should under branch master after I init a git repo, isn't it?

but git branch -a, i got nothing man.
and I got fatal: branch 'master' does not exist when I try to set upstream for my branch.

users@debian MINGW64 ~/Desktop/taste
$ git init
Initialized empty Git repository in C:/Users/users/Desktop/taste/.git/

users@debian MINGW64 ~/Desktop/taste (master)
$ git remote add origin [email protected]:greedev/Test.git

users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -u origin/master
fatal: branch 'master' does not exist

users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -a

users@debian MINGW64 ~/Desktop/taste (master)
$ git fetch
The authenticity of host 'gitee.com (120.55.226.24)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,120.55.226.24' (ECDSA) to the list of know                n hosts.
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From gitee.com:greedev/Test
* [new branch]      master     -> origin/master

users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -a
  remotes/origin/master

users@debian MINGW64 ~/Desktop/taste (master)
$ git branch -u origin/master
fatal: branch 'master' does not exist

enter image description here

like image 749
Zander Wong Avatar asked Oct 24 '17 16:10

Zander Wong


People also ask

Why my master branch is not showing in git?

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 git ls-remote origin master doesn't show anything.

How do you fix fatal the current branch my branch has no upstream branch?

Fatal Git Error: Current branch has no upstream The simple solution to the current problem is easily solved by issuing the following Git push upstream command: git@upstream-error /c/branch/push (new-branch) $ git push --set-upstream origin new-branch Enumerating objects: 3, done.

Is master branch created automatically?

It's the first branch to be created automatically when you create a new repository. By default, this initial branch is named master .

What does it mean when a branch has no upstream?

The fatal: The current branch master has no upstream branch error occurs when you have not configured git in such a way that it creates a new branch on the remote. Therefore, you are only creating a new branch on the local computer.

What's the difference between GIT fatal and Git master?

git fatal: a branch named 'develop' already exists. fatal: a branch named 'development' already exists. local master and origin master not the same a branch named 'master' already exists.

How do I get to the master branch in Git?

In a new project with no additional branches, you will only see * master after running the git branch command. But, if you have multiple branches they will all be listed. Be sure to check out our full guide on creating and switching between branches in Git.

Why does my Git repository have no branches?

After the first three steps—creating a new, totally-empty repository—you have a repository that is in a peculiar state: it has no commits, so it has no branches. At the same time, it doeshave a current branch, which is master. In other words, the current branch is a branch that does not exist. This state is unusual, but normal.

Should I use 'main' or 'master' for Git init?

If you are trying to push your app to a cloud service via CLI then use 'main', not 'master'. 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.


Video Answer


2 Answers

TL;DR

You can git checkout master at this point.

Longer description (but still not that long)

You are doing this the hard way.

In the future, instead of:

mkdir repo
cd repo
git init
git remote add origin <url>
git fetch origin
git checkout master

you can simply run:

git clone <url> repo

since the six commands above are pretty much what git clone does.

After the first three steps—creating a new, totally-empty repository—you have a repository that is in a peculiar state: it has no commits, so it has no branches. At the same time, it does have a current branch, which is master.

In other words, the current branch is a branch that does not exist.

This state is unusual, but normal. If you run git checkout --orphan newbranch, you put your Git repository into that same state:1 on a branch that does not exist. The branch gets created once there is a commit hash to store under the branch name.

Whenever you run git checkout <name> and there is no branch named <name>, Git checks to see if there is exactly one remote-tracking branch such as origin/<name>. If so, Git creates a new branch named <name> that points to the same commit as origin/<name> and that has origin/<name> as its upstream.

Since this last step—git checkout master when master does not actually exist yet—is the final step of git clone, git clone will also create a new branch master that tracks the remote-tracking branch origin/master.


1Note that you retain the current index / staging-area content. This is true for the new empty repository as well, but since it's a new empty repository, the index / staging-area is also empty, and "retaining the empty set" does not feel much like retainment.

like image 95
torek Avatar answered Oct 22 '22 23:10

torek


After you run git init, the master you see is not completely created. It doesn't exist as it hasn't pointed to any commit yet. I once read that it was designed. But I think it's a puzzling bug. If you run git branch, it returns nothing.

After you run git fetch, a following git checkout master does the job. It is equivalent to:

git branch master origin/master
git checkout master
git branch -u origin/master
like image 30
ElpieKay Avatar answered Oct 22 '22 23:10

ElpieKay