19

I am working on a project and I created a repository with a master branch. Someone who is working on it added a branch named new-branch -- their code changes are in this branch.

However, when I clone the repository:

$ git clone git@github.com:me/my-repo.git

I can clone it successfully, but it only shows the master branch. I do not know how I can view/get the new-branch.

How would I pull this branch to my repository?

David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

41

When you clone a repository, all remote branches are created as "remote tracking branches" in your repository. These aren't shown by default, but you can see these with:

git branch -a

If you do a git checkout new-branch, git will find the remote tracking branch of the same name, automatically create a new local branch from the same commit, and switch to the new local branch.

For future work, the git fetch command will update all the remote tracking branches with their latest commit from the remote.

Community
  • 1
  • 1
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Didnt know that worked - I thought you need the -b on checkout to create a new branch – Adrian Cornish Jan 17 '12 at 04:52
  • @Adrian Cornish: this DWIM behaviour has been present since git 1.6.6, introduced in [70c9ac2f](https://github.com/git/git/commit/70c9ac2f1999b7e0c17a864235537cffe29dfea4) – Mark Longair Jan 17 '12 at 14:39
  • What happens if I am disconnected? "remote tracking branches" sounds like they are not physically present in the cloned repo. Would git checkout work if I have just found it via git branch -a in a disconnected state? Would git branch -a list the branch in that state at all? – timmkrause Jan 13 '14 at 08:34
  • 1
    @timmkrause: Despite the name, remote tracking branches *are* physically present in your cloned repo. They mirror the same named branch on the server. A checkout of a remote tracking branch will work while disconnected. – Greg Hewgill Jan 13 '14 at 08:36
0

In my case, i cloned the Repo correctly but i was trying to find the branches in the internal folder. so i was unable to see any.

My Project has below folder structure.

ABC/
ABC_XYZ / 
ABC_Core
ABC_Web
x
y
z
.....

so i was trying to check the branches in ABC_XYZ instead of ABC.

nikhil2000
  • 178
  • 3
  • 15