1

As explained in this post git checkout -b <mybranch> runs two commands simultaneously

  1. git branch <mybranch>
  2. git checkout <mybranch>

I created a new git repository with git init. Now When I run git branch development I get the error:

Not a valid object name: 'development'.

If I run git checkout newbranch. I get the error:

pathspec 'newbranch2' did not match any file(s) known to git.

But if I use git checkout -b BigShotBranch then it simply creates that local branch with the message:

Switched to a new branch 'BigShotBranch'

so my question is why was the -b flag able to create the branch while any other method could not? Does the -b flag only combine two commands or do anything more than that?

P.S: This seems to be a glitch in git. After git checkout -b development the master branch is actually renamed to development. There is no master branch now.

Community
  • 1
  • 1
user31782
  • 7,087
  • 14
  • 68
  • 143

3 Answers3

1

A newly created empty repository is a corner case. Though .git/HEAD refers to refs/heads/master, actually .git/refs/heads/ is yet empty. "git branch newbranch" tries to resolve HEAD (in order to start the new branch from the current commit) and fails.

Even though "git checkout -b" is explained with the help of "git branch", underneath the former doesn't directly call the latter (but they obviously share some lower level code). Note that git checkout supports creating root branches via the --orphan switch, which git branch cannot. Evidently, in the empty repository situation git branch -b is actually run as git branch --orphan.

Leon
  • 31,443
  • 4
  • 72
  • 97
1

This is happening because you ran git init, creating a new, empty repository.

A branch name must always point to some existing commit. A new, empty repository has no commits. This means it has no branches either.

It doesn't even have master.

What it does have is a current branch (name), which is sort of self-contradictory since the branch doesn't exist and therefore the name cannot possibly be valid. However, a new, empty repository must have a current branch, even though it cannot have a branch because there are no commits—so Git has a special-case provision, that you may have a so-called orphan branch, that has no "branch-ness" (it doesn't really exist) and yet is your current branch (so that it exists, sort of, in this weird limbo state).

When you make a commit, the orphan branch now has a commit it can—and does—point to. But there can only be one branch in this "yet to be created" state, and only git checkout -b can set it up. (Well, some plumbing commands can set it up too.) In a non-empty repository, you must use git checkout --orphan to set it up since git checkout -b will set the new branch to point to the current commit (since there is one).

Side note: you're mixing together GitHub (which is a web site) and Git (which is only connected to GitHub in that GitHub provides access to Git repositories).

torek
  • 448,244
  • 59
  • 642
  • 775
  • Actually I am using bitbucket with gitbash command line tool. – user31782 Oct 27 '16 at 10:26
  • 1
    OK, but you said that you "created a local github repository": I'm just saying, as an aside, that you didn't do that, you created a git repository. – torek Oct 27 '16 at 10:28
0

The reason for this behavior is that there is no commit (start-point) to which the new branch created by git branch <branch-name> [<start-point>] can point to, hence the error. You should read the documentation here, https://git-scm.com/docs/git-branch

Though as soon as you create a commit, you can use the same syntax to create branches. Since now there will be a commit the HEAD will point to which will be used to create the branch.

Secondly, having a master is just a convention which is created by default, if you create a branch before your first commit using git checkout -b development, your first commit will be on that branch and thus your default master branch will not be created automatically. Which is why your master branch disappeared. You can simply create the master branch by git branch master or git checkout -b master when you want.

Hassaan
  • 1,561
  • 1
  • 9
  • 15