Steve He Steve He - 1 month ago 12
Git Question

What is the standard way to create a local branch from a central repo?

Let's say that the central repo has

master
,
branch_A
and
branch_B
.
In my local working space, I have only
master
and
branch_A
.

I want to get a local branch
branch_B
in my work-space in a clean way.

i.e. I want to get the same exact contents from central repo's
branch_B
and nothing else.

I thought that
git checkout -b my_name/branch_B
would do this.
However, when I did this, I got a lot of conflicts, which I do not know why.

I do not want to resolve conflicts manually. All I want is to get the same exact contents from the central repo's
branch_B
and nothing else (i.e. discard any local changes if there is any)

Answer

First git fetch

After a git fetch your local repo should have fetched all the branches from the central repo, which usually is called origin.

This will have created a local remote-tracking branch called origin/branch_B:

$ git fetch
 * [new branch]      branch_B -> origin/branch_B

This remote-tracking branch origin/branch_B tracks in your repo what are the latest changes fetched from central repo.

You're not supposed to commit to it, so you need to create an actual branch to work on.


Then git checkout branch_B

Then simply git checkout branch_B should create a local branch_B that automatically tracks origin/branch_B:

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

If you want to list both your local and your remote branches, you can use git branch -va.

Comments