Nicryc
Nicryc

Reputation: 576

How to pull a remote branch locally?

I created a project on Gitlab and cloned it on my computer. So I only have one branch (master) locally (git branch only shows master). A colleague created a new branch. So now there are 2 branches on Gitlab but not on my computer.

How do I do to have the branch created by my co-worker on my computer too so as git branch shows both master and new-branch ?

Thank you.

Upvotes: 27

Views: 57794

Answers (3)

Romain Valeri
Romain Valeri

Reputation: 22057

First, update your remote-tracking branches (local replicas of the remote branches, with which you can't interact in the same way you do with your own local branches). It's typically done with

git fetch

(without any parameters, --all is implied)

Your local repo will then know every new branch your coworker could have created since you last fetched (or pulled, since a pull does a fetch as a first step).

Then you'll be able to create a local counterpart for any of these remote branches with

git checkout <branchName>

Here, note that <branchName> is meant to be given without the <remote>/ prefix, or else git will attempt to check out the so-named remote-tracking branch, which it can't, by design. At this point it will then resolve the branch ref to the commit which this remote-tracking branch points to, check this commit out directly, resulting in a detached HEAD state. (which is no big deal but can unsettle people starting to use git)

Upvotes: 29

mzakaria
mzakaria

Reputation: 649

Assuming your remote is called origin your friend's branch is called Friend_Remote and you want to name the branch locally as Friend_Local

Create a new branch and name is Friend_Local:

git checkout -b Friend_Local

Then pull the remote branch to your local one

git pull origin Friend_Remote

Upvotes: 7

Dany M
Dany M

Reputation: 870

Try:

git fetch

This will updates all branches and pull them locally.

Or:

git fetch remote_repo remote_branch:local_branch

If you're interested in one branch only, then:

git checkout local_branch

Upvotes: 14

Related Questions