Categories
branch git git-branch git-fetch

Git fetch remote branch

2870

The remote contains various branches such as origin/daves_branch:

$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master

How do I checkout daves_branch locally so that it tracks origin/daves_branch? I tried:

$ git fetch origin discover
$ git checkout discover

8

3878

Update: Using Git Switch

All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort.

If daves_branch exists on the remote repository, but not on your local branch, you can simply type:

git switch daves_branch

Since you do not have the branch locally, this will automatically make switch look on the remote repo. It will then also automatically set up remote branch tracking.

Note that if daves_branch doesn’t exist locally you’ll need to git fetch first before using switch.


Original Post

You need to create a local branch that tracks a remote branch. The following command will create a local branch named daves_branch, tracking the remote branch origin/daves_branch. When you push your changes the remote branch will be updated.

For most recent versions of Git:

git checkout --track origin/daves_branch

--track is shorthand for git checkout -b [branch] [remotename]/[branch] where [remotename] is origin in this case and [branch] is twice the same, daves_branch in this case.

For Git 1.5.6.5 you needed this:

git checkout --track -b daves_branch origin/daves_branch

For Git 1.7.2.3 and higher, this is enough (it might have started earlier, but this is the earliest confirmation I could find quickly):

git checkout daves_branch

Note that with recent Git versions, this command will not create a local branch and will put you in a ‘detached HEAD’ state. If you want a local branch, use the --track option.

Full details are here: 3.5 Git Branching – Remote Branches, Tracking Branches

20

  • 155

    “git fetch” to make sure your repo is updated with remote references and “git checkout –track origin/discover” should be enough. Then you can commit to that branch and a “git push” to sync the remote with your changes.

    Mar 2, 2012 at 17:54

  • 33

    I tried this and got “fatal: git checkout: updating paths is incompatible with switching branches. Did you intend to checkout ‘upstream/develop’ which can not be resolved as commit?”. Am I doing something wrong?

    Apr 8, 2012 at 22:40


  • 43

    Looks like git 1.5.6.5 needs this instead: git checkout –track -b origin/daves_branch

    – Charlie

    Jul 12, 2013 at 13:31

  • 37

    This made a mess for me, it created a local branch named origin/<branch> which is now ambiguous to the remote branch origin/<branch> and I don’t know how to get rid of the crazy local branch!

    Jul 9, 2015 at 17:00

  • 26

    You need to add the local branch name explicitly, otherwise git creates a new local branch with the full branch path, as @AlanMoore and @derekmx271 stated above: git checkout -b --track daves_branch origin/daves_branch

    Jul 22, 2015 at 15:57


1165

I have used fetch followed by checkout

git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>

…where <rbranch> is the remote branch or source ref and <lbranch> is the as yet non-existent local branch or destination ref you want to track and which you probably want to name the same as the remote branch or source ref. This is explained under options in the explanation of <refspec>.

Bash is so smart it auto completes the first command if I tab after the first few letters of the remote branch. That is, I don’t even have to name the local branch; Bash automatically copies the name of the remote branch for me. Thanks, Bash!

Also as the answer in this similar Stack Overflow post shows, if you don’t name the local branch in fetch, you can still create it when you check it out by using the -b flag. That is, git fetch <remote> <branch> followed by git checkout -b <branch> <remote>/<branch> does exactly the same as my initial answer. And evidently, if your repository has only one remote, then you can just do git checkout <branch> after fetch and it will create a local branch for you. For example, you just cloned a repository and want to check out additional branches from the remote.

I believe that some of the documentation for fetch may have been copied verbatim from pull. In particular the section on <refspec> in options is the same. However, I do not believe that fetch will ever merge, so that if you leave the destination side of the colon empty, fetch should do nothing.

NOTE: git fetch <remote> <refspec> is short for git fetch <remote> <refspec>: which would therefore do nothing, but git fetch <remote> <tag> is the same as git fetch <remote> <tag>:<tag> which should copy the remote <tag> locally.

I guess this is only helpful if you want to copy a remote branch locally, but not necessarily check it out right away. Otherwise, I now would use the accepted answer, which is explained in detail in the first section of the checkout description and later in the options section under the explanation of --track, since it’s a one-liner. Well… sort of a one-liner, because you would still have to run git fetch <remote> first.

FYI: The order of the <refspecs> (source:destination) explains the bizarre pre Git 1.7 method for deleting remote branches. That is, push nothing into the destination refspec.

15

  • 2

    This worked for me to get the remote code into a local branch. It did not however get my local branch to track the remote branch.

    – aknosis

    Sep 17, 2014 at 22:35

  • 1

    For some reason, git fetch remote branch didn’t add a branch head at all for me, though all the refs got fetched, so when I tried to follow the steps in the accepted answer, I got the error that pathspec did not match any file(s) known to git., but the rbranch:lbranch approach worked. Interestingly, it also fetched all the tags that started with the same prefix, like it is a wildcard (rbranch*).

    – haridsv

    Jun 30, 2015 at 8:36


  • 7

    Nit: git doesn’t do the autocompleting, it is the bash shell that is doing it.

    – legalize

    Dec 29, 2015 at 20:44

  • 2

    FWIW, I think the difference between this answer and the accepted answer is that this one tells you to do the fetch command. It the accepted answer makes sense though because OP notes that he already did the fetch. That’s at least the issue I ran into.

    – tenor528

    Mar 16, 2017 at 15:17

  • 1

    Hi @Honey, as the answer above states: “And evidently if your repo has only one remote, then you can just do git checkout <branch> after fetch and it will create a local branch for you. EG: You just cloned a repo and want to check out additional branches from the remote.” Also in the git-checkout docs: “If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to: $ git checkout -b <branch> --track <remote>/<branch>

    Jun 5, 2019 at 16:35

475

If you are trying to “checkout” a new remote branch (that exists only on the remote, but not locally), here’s what you’ll need:

git fetch origin
git checkout --track origin/<remote_branch_name>

This assumes you want to fetch from origin. If not, replace origin by your remote name.

3

  • Usually, I use git fetch, but the thing is what is the difference between git fetch and git fetch origin ?

    – Ping Woo

    Jul 26, 2019 at 21:12

  • @PingWoo Assuming that the branch you want to fetch resides in origin remote, both git fetch and git fetch remote will do the same thing. If you need to fetch from a remote other than origin, you could do that using git fetch <other_remote_name>. This situation is highly uncommon, just mentioned here for completeness.

    Aug 6, 2019 at 10:04

  • i get fatal: 'fusedwm/fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize' is not a commit and a branch 'fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize' cannot be created from it

    – Fuseteam

    Apr 18, 2021 at 15:22