Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull a remote branch locally?

Tags:

git

branch

gitlab

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.

like image 532
Nicryc Avatar asked Sep 05 '25 21:09

Nicryc


2 Answers

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)

like image 165
Romain Valeri Avatar answered Sep 08 '25 11:09

Romain Valeri


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
like image 38
Dany M Avatar answered Sep 08 '25 10:09

Dany M