This is normal: you have just created a new project:
$ mkdir newproj
$ cd newproj
$ git init
Initialized empty Git repository in .../newproj
At this point, no branches exist. Nonetheless, as git status
will tell you, you are on the master
branch:
$ git branch
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
$
You can switch to another branch, using git checkout -b
:
$ git checkout -b xyz
Switched to a new branch 'xyz'
and now you still have no branches, but now you are on branch xyz
, as git status
will tell you.
This odd state is normal, because there are no commits. When there are no commits, there can be no branches, because a branch name simply contains the hash ID of some existing commit. Without commits, no branch name can exist.
When you create a new commit, Git does the following steps (the first several are not in any particular order; only the last few have an actual order):
- Gather your name and email address, to use in the new commit.
- Get (your computer's idea of) the current time, to attach to your name and email address.
- Gather a log message.
- Freeze the contents of the index (the index being where you build new commits:
git add
copies files from the work-tree into the index).
- Get the hash ID of the current commit, if there is a current commit. Get the hash ID of any commits being merged as well, if that's going on.
- Use all of this information to create a new commit. Creating the commit assigns it its hash ID, which is a cryptographic checksum of all of these contents (your name and email address with the time stamp, your log message, the hash ID of the frozen tree, and so on).
- Write the hash ID obtained by writing the new commit. This new commit's hash ID goes into the current branch name. If the branch name did not exist before this point, well, now it exists.
Hence, it's not until you make at least one commit that you can have any branch names. But you can still choose which branch name is your current branch, even if that branch can't exist: it's the creation of the first commit that will create the branch name.
(Once you do have at least one commit, you can attach as many names as you like to the one commit. If you have more than one commit, you can attach as many names as you like to every commit, though most commits don't need any names attached to them. Git finds them by working backwards from a later commit. Only the last commit—the tip of a branch—ever needs a name, so that Git can find it without first finding a later commit.)