Git fetch remote branch
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 the
However, I have the
branch. How can my colleague pull that branch specifically?
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.
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 220.127.116.11 you needed this:
git checkout --track -b daves_branch origin/daves_branch
For Git 18.104.22.168 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
Full details are here: 3.5 Git Branching - Remote Branches, Tracking Branches
Read more... Read less...
git fetch <remote> <rbranch>:<lbranch> git checkout <lbranch>
<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
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.
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
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.
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.
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'
git branch -a (both local and remote branches) or
git branch -r (only remote branches) to see all the remotes and their branches. You can then do a
git checkout -t remotes/repo/branch to the remote and create a local branch.
There is also a git-ls-remote command to see all the refs and tags for that remote.
The easiest way to do it, at least for me:
git fetch origin <branchName> # Will fetch the branch locally git checkout <branchName> # To move to that branch