Git fetch remote branch
Solution 1
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
Solution 2
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>
.
Git 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, Git automatically copies the name of the remote branch for me. Thanks Git!
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.
Solution 3
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.
Solution 4
To checkout myBranch that exists remotely and not a locally - This worked for me:
git fetch --all
git checkout myBranch
I got this message:
Branch myBranch set up to track remote branch myBranch from origin
Switched to a new branch 'myBranch'
Solution 5
git fetch origin <branchName> # Will fetch the branch locally
git checkout <branchName> # To move to that branch
David
Updated on July 16, 2022Comments
-
David almost 2 years
My colleague and I are working on the same repository. We've branched it into two branches, each technically for different projects, but they have similarities, so we'll sometimes want to commit back to the *
master
from thebranch
.However, I have the
branch
. How can my colleague pull that branch specifically?A
git clone
of the repository does not seem to create the branches locally for him, though I can see them live on unfuddle after a push on my end.Also, when I originally made the branch, I did
-b checkout
. Does that make much difference?$ git branch -r origin/HEAD -> origin/master origin/daves_branch origin/discover origin/master $ git fetch origin discover $ git checkout discover
These are the commands I ran. But it definitely is not working.
I want to be able to check out that branch and then push and commit back just the branches changes from various collaborators or workstations.
-
David about 12 yearsDoes this need to be proceeded with a git fetch discover?
-
ralphtheninja about 12 yearsGit fetch is only needed if something has changed on the remote, e.g. if Dave has pushed a branch to the main repo and your repo doesn't yet have any references to it.
-
David about 12 yearsWhich is actually true in this instance.
-
ralphtheninja about 12 years"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.
-
Neil Barnwell about 12 yearsI 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?
-
Sergey Kotyushkin about 12 years@NeilBarnwell As Magnus said, do a
git fetch
-
Lucas Wilson-Richter over 11 years@NeilBarnwell I needed to do a
git fetch --all
(which fetches all remotes) to get this to work. -
Shofiqul Alam over 11 years@NeilBarnwell Read this answer how to fix it
-
Erin about 11 years
git checkout remotes/repo/branch
makes git checkout look for a pathspec, not a remote repo. -
Daniel Lee about 11 yearsYes, is it even possible to checkout a branch on the remote repo? Obviously (or maybe it wasn't so obvious), remotes are first fetched so that you have them locally. The git book has a good section on them: git-scm.com/book/en/Git-Branching-Remote-Branches
-
Peter Ehrlich almost 11 years@NeilBarnwell this worked for me:
git remote show origin
git remote update
(explanation at stackoverflow.com/questions/945654/…) -
codeling almost 11 years
fatal: cannot update paths and switch to branch '...' at the same time. Did you intend to checkout 'origin/...' which can not be resolved as commit?
??? -
Charlie almost 11 yearsLooks like git 1.5.6.5 needs this instead: git checkout --track -b origin/daves_branch
-
Mark Mikofski over 10 yearsYes for git commands to be auto-completed then git-completion.bash must be enabled. Directions "to use these routines" is included at the top of the source file. You may also wish to use git-prompt as well (directions included in source). Note on a Macintosh,
~/.bashrc
is~/.bash-profile
. During Git installation on Macintosh, these two files are not included, and the.bash-profile
file in$HOME
is not altered to add theses commands. -
JoshP about 10 yearsI ran into
warning: refname 'origin/<branch>' is ambigious
. The fix for me was to instead rungit checkout --track remotes/origin/<branch>
-
Guillaume Vincent about 10 yearsfrom git checkout documentation: If <branch_name> is not found but there does exist a tracking branch in exactly one remote with a matching name, treat as equivalent to:
git checkout -b <branch_name> --track <remote>/<branch_name>
-
Andrew Samuelsen about 10 yearsIn other words you don't have to write
-t
? -
Leniel Maccaferri almost 10 yearsThanks Guillaume! I just used this command and wrote a post about it to depict my exact case: leniel.net/2014/05/…
-
aknosis over 9 yearsThis 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.
-
K-Gun over 9 yearsAnd approve that switched to branch that you want to be in
git branch
. -
haridsv almost 9 yearsFor 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 thatpathspec did not match any file(s) known to git.
, but therbranch:lbranch
approach worked. Interestingly, it also fetched all the tags that started with the same prefix, like it is a wildcard (rbranch*
). -
Alan Moore almost 9 yearsThis 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!
-
Derek Foulk almost 9 yearsYeah I would not use this command. It may be because I have multiple remotes in my config, but I am now in the same boat as 'Alan Moore'. I just deleted the local repo and then cloned it from GitHub again... But before I did that, I was having a hard time undoing whatever the above command did. Even after
$ git branch -d origin/mybranch
... -
Mike Scott almost 9 yearsYou 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
-
stanri over 8 yearsI think there's an error in this answer. I originally did the command without
-t
and gotYou are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
because there wasn't a local branch of the same name. I had to re-run with-t
to fix. -
legalize over 8 yearsNit: git doesn't do the autocompleting, it is the bash shell that is doing it.
-
Roberto about 8 years@MikeScott
git checkout --track -b daves_branch origin/daves_branch
, otherwise it tells me --track is not a valid branch. -
skwidbreth almost 8 yearsThis worked well for me - my colleague had added a new remote branch that I wanted to add to my local repo. I kept fetching but didn't see the new branch appearing locally. Didn't realize that I could just run
checkout
for it to create it. Thanks! -
skwidbreth over 7 yearsThis really helped me - I simply wanted to fetch a remote branch that my colleague had added to the origin. A lot of documentation seems to me to be missing the critical piece about adding the name of the new local branch that you want the fetch to be directed to -
<rbranch>:<lbranch>
. Thank you very much. -
Richard Whitehead about 7 yearsYou do need to do
git fetch
first if the branch is not present locally. -
tenor528 about 7 yearsFWIW, 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. -
leRobot almost 7 yearsdepends on git versions. Latest version, as said elsewhere here, only needs git checkout <name of branch on origin>. BE CAREFUL WITH SAME-NAME LOCAL vs REMOTE BRANCHES - they will mess stuff up
-
Conor over 6 yearsThis is perfect for tracking a remote branch of a repo you've previously forked (e.g. tracking the dev branch of an OSS project you contribute to). Thanks!
-
lase over 6 yearsThis specific approach has the added benefit of only retrieving the desired branch (rather than all remote branch references), this was important for me on a slow connection interfacing with a rather large repo.
-
Benjamin Harel over 6 yearsDoes not bring other branches
-
Sachidananda Naik over 6 years<!-- git checkout <remote-branch-name> --> works as expected, Thanks Guillaume!
-
James Rochabrun about 6 years@BenjaminHarel the question says "fetch a remote branch" not all branches. for that follow this may be helpfull for you stackoverflow.com/questions/10312521/…
-
Minh Tran almost 6 years@Mike Scott Regarding "
You need to create a local branch that tracks a remote branch
". Does it matter what name is given to the local branch or should it match that of the remote repo's? -
Ian Poston Framer over 5 yearsRizo, git pull origin branch_name should be the best solution. You are the only person that posted this as a solution and it worked for me. This works because it will update your branch with the master branch. simple and uncomplicated.
-
PatS over 5 yearsWhat happens when you run the command
git push
without any other arguments? Is the local branch namedremote-branch-name
automatically associated with (tracking to) the remote branch namedorigin/remote-branch-name
. Or do you need to rungit push -u origin remote-branch-name
-
Akin Hwan over 5 yearsthis will cause HEAD detached state
-
Abhijeet over 5 yearsAfter using this
fetch
command required branch will be available on local machine.git checkout -b 'your_branch' origin/'remote branch'
is required to checkout this branch. -
DaveyDaveDave over 5 yearsWhile this snippet might answer the question, it's better to include some explanation about what it does and how it differs from the very large number of answers already here.
-
cosmoloc over 5 yearsI personally do not like this option. Because in case you are creating a new branch from Master and then pulling data from remote branch, that remote branch may or may not be on the same page as the master and might invite some unnecessary code updates
-
Bernardo Dal Corno about 5 yearsthe problem with this is that it will try to merge that remote branch with your CURRENT branch, which is not the remote one (since that is new for your local repo)
-
Bernardo Dal Corno about 5 yearsThose shorthands were a lesson
-
Nicholas Petersen almost 5 yearsHow does this question not have more upvotes? I could be wrong but this sure seemed to do the trick, fetched a branch I didn't have on local from remote...
-
mfaani almost 5 yearsI just did
git fetch origin <remoteBranch>
now how can I locally check that branch out? NeverMind. I figured it out. I can simply dogit checkout <remoteBranch>
and it will just create a local branch with the name of<remoteBranch>
. I'm just not so sure how I should process that. I wrote a question here if you can answer... -
Mark Mikofski almost 5 yearsHi @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>
" -
Eem Jee almost 5 yearsThis will merge to your current branch.
-
Prashant almost 5 years
git fetch origin mybranch
neither showed any error, nor did it show mybranch in branch list withgit branch -v
.. And then doinggit checkout mybranch
had no effect. So I tried,git checkout --track origin/mybranch
, and it worked! Thanks @ralphtheninja -
koppor almost 5 yearsThis also works with partial clones: Example:
git fetch --depth=20 <remote> <rbranch>:<lbranch>
-
Ping Woo almost 5 yearsUsually, I use git fetch, but the thing is what is the difference between git fetch and git fetch origin ?
-
paneer_tikka over 4 years@PingWoo Assuming that the branch you want to fetch resides in origin remote, both
git fetch
andgit fetch remote
will do the same thing. If you need to fetch from a remote other thanorigin
, you could do that usinggit fetch <other_remote_name>
. This situation is highly uncommon, just mentioned here for completeness. -
David Foster over 4 yearsCan this be done to create local versions of all remote branches, without specifying each individually?
-
Peter Mortensen over 4 yearsRelated: How do I clone a single branch in Git? - "git 1.7.10 (April 2012) actually allows you to clone only one branch:"
-
Peter Mortensen over 4 yearsSome explanation would be in order.
-
mfaani about 4 years
--all
is never a good idea, because it will download every file on every branch. It will take more time and space. It's better to be specific with the branch name and do like this -
Anthony Lei over 3 years
git checkout -t remote_branch_name
is probably the most simple and laziest way out of all answers. -
Fuseteam about 3 yearsi get
fatal: Refusing to fetch into current branch refs/heads/fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize of non-bare repository
-
Fuseteam about 3 yearsi 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
-
Mark Mikofski about 3 years@Fuseteam is it possible you are trying to update the branch that you're already on? See: stackoverflow.com/questions/2236743/…. Try to create a remote first:
git remote add FOO url/of/repo
then fetch the branch from the remote:git fetch FOO fuse_keybindings...
. This may create a new local branch or update the existing local branch depending. See the git-fetch docs & good luck! -
Fuseteam about 3 yearshi @MarkMikofski i am trying to fetch a remote branch to have it locally, I have already added the remote, and I tried
git fetch --all
to no avail. i can't remember if I triedgit fetch remote-name branch
thanks for the tip -
Peter Mortensen about 2 yearsAn explanation would be in order.