How to pull a remote branch locally?

Refresh

April 2019

Views

31 time

1

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.

2 answers

2

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 remotes 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)

1

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