Tim
Tim

Reputation: 99398

Why is a branch not shown by git branch?

In Windows 10's cmd

>git branch
* master

There is only one branch shown, which is the master branch. I was told that there is another branch named "staging", but I didn't see it.

When I run the following command

>git checkout staging
Branch staging set up to track remote branch staging from origin.
Switched to a new branch 'staging'

Then

>git branch
* master
  staging

Why is the staging branch not shown at first and then shown now?

Thanks.

Upvotes: 2

Views: 953

Answers (3)

mkrieger1
mkrieger1

Reputation: 23123

When cloning a repository, only one "normal" (local) branch is created in the cloned repository (in your case master). The other branches of the source repository are stored as "remote" branches in the cloned repository. You can see them by adding the -r (lists only remote branches), or -a (lists local and remote branches) option to git branch.

In your case, it might have looked like this

> git branch -a
* master
  origin/master
  origin/staging

To create a new local branch which points to the same commit as origin/staging, run

> git branch staging origin/staging

or, to check that new branch out at the same time:

> git checkout -b staging origin/staging

As shown in torek's answer, explicitly naming origin/stagin is optional.

Quoting the git-checkout manual:

git checkout <branch>

[…]

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

$ git checkout -b <branch> --track <remote>/<branch>

This has the advantage of automatically setting up the new local branch to track the remote branch.

Upvotes: 2

torek
torek

Reputation: 487725

Note that Git said:

Switched to a new branch 'staging'

(emphasis mine). The branch staging did not exist until you asked Git to check it out. Git tried to check it out, which failed—so then it tried to create it, and that worked. This is a special operation built in to git checkout: if you ask to check out a branch by name, and the name does not correspond to any existing branch, Git will see if there's one so-called remote-tracking branch with a "close enough" name.

To list these remote-tracking branches, use:

git branch --list --remotes

or abbreviate this as git branch -lr. If you have origin/zerblatt, you can create your own zerblatt by running git checkout zerblatt. If you don't have that, git checkout will just complain that zerblatt is not a branch or a path that it understands.


In a comment, you asked:

is [this] because the clone operation didn't pull the staging branch, but only the master branch?

Watch out for Git's peculiar terminology: you don't "pull" a branch by cloning. The word pull means the git pull command, which I suggest you avoid: it's meant as a convenience shortcut, but it will do things you won't want it to.

The clone command clones everything by default, but it makes, for you, your own private repository, separate from the other Git repository. That other Git repository has its own branches, which are now different from your branches. To keep them separate for you, your Git renames all their branches: their master becomes your origin/master, and their staging becomes your origin/staging.

These origin/* names are (your, not their) remote-tracking branches, and correspond to their (regular or local) branches. Your own local branches are yours to do with as you like, but initially, Git just creates one, normally master.

The really tricky thing here is what a branch actually means and is. You may wish to read What exactly do we mean by "branch"? (save this for later, if you're very new to Git). There are several different things that Git uses that are all loosely called "branch"; and while they are all related, they are not the same.

Upvotes: 2

etusm
etusm

Reputation: 4210

git only lists local branches until you checkout a remote branch.

git branch -r

will list all remote branches also

Upvotes: 2

Related Questions