got ‘fatal: branch ‘master’ does not exist’ in git

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)