I want to do a pull a branch branch_1
that exists on remote repository.
Results: I have branch_1
on my local repository tracking the same branch_1
on the remote repository.
Normally you have a single remote. By convention it's usually called origin
. So let's look at that case first:
Your local repo has to be aware of the remote branch. It may be already, but you can make sure with
git fetch
If you do a git branch -a
you should now see something like
origin/branch_1
Then simply
git checkout branch_1
This wll see that you don't have a local branch by that name, and that there is exactly one remote branch by that name, so it will create the local branch and set up tracking automatically.
If the remote has a different name, the procedure is the same; the output of the branch -a
command above will be a little different, since what you're looking for is the remote ref name. (That is to say, the above output origin/branch_1
is made up of the remote name origin
and the branch name branch_1
.)
But what if you have multiple remotes? For example, you might have an origin
and also an upstream
remote from which origin
was forked. In this case, it's possible that both remotes have different versions of the same branch.
$ git branch -a
master
origin/master
origin/branch_1
upstream/branch_1
In that case, git won't know what to do if you just say git checkout branch_1
, so it requires you to be more explicit. This is when you would have to use
git checkout -b branch_1 --track origin/branch_1
(This assumes you're wanting to locally track your origin's version; you could say --track upstream/branch_1
instead, and then you'd see the state of the project from which your origin forked.)
Lastly, be aware that pull
has a specific meaning in git, and the way you're using it isn't really correct. This may seem like a nitpick, but there's enough going on in git that this kind of thing really affects your ability to communicate with other git users.
To pull
a set of changes is to incorporate those changes into the current local branch (usually by a merge or fast-forward). This is most commonly used to pull changes into a local branch from the corresponding remote branch ("pull the branch"); but that's a bit different from the case where you want to create the local branch from the current state of the remote branch.