Reputation: 5121
I'd like to grab a single branch (not all of them) of a remote repository and create a local tracking branch that can track further updates to that remote branch. The other branches in the remote repository are very big, so I'd like to avoid fetching them. How do I do this?
Upvotes: 446
Views: 404131
Reputation: 10722
Update to help make it simpler to checkout that branch
git fetch <remote_name> <branch_name>:<branch_name>
git checkout <branch_name>
It doesn't setup tracking though.
git fetch <remote_name> <branch_name>
Worked for me.
Upvotes: 405
Reputation: 3012
I know there are a lot of answers already, but these are the steps that worked for me:
git fetch <remote_name> <branch_name>
git branch <branch_name> FETCH_HEAD
git checkout <branch_name>
These are based on the answer by @Abdulsattar Mohammed, the comment by @Christoph on that answer, and these other stack overflow questions and their answers:
If the step #2 is skipped, you will have this error:
error: pathspec '<branch_name>' did not match any file(s) known to git
Upvotes: 149
Reputation: 4052
In my case, I wanted to fetch a branch without creating a new remote, so this worked:
git fetch <remote url> <remote branch name>:<local branch name>
Upvotes: 4
Reputation: 580
The reply depends on what you want to accomplish.
git checkout -b <local_branch> <local_branch_to merge_into>
git pull <remote_url> <remote_branch>
git clone --single-branch --branch remote_branch remote_url
# multiple -t options are allowed
git remote add -t <remote_branch> <remote_repo> <remote_url>
# with --add it will add the branch instead of setting it
# you can add multiple branches with multiple --add lines
# wildcards are allowed,
# e.g. branch_v\* matching branch_v1, branch_v2, ...
git remote set-branches [--add] <remote_repo> <remote_branch>
git remote add <remote_repo> <remote_url>
# If you set only one <remote_branch> in the restrictions above (i.e no option 4),
# then you can omit it and still only <remote_branch> will be fetched
git fetch <remote_repo> [<remote_branch>]
# without -b the local branch name is guessed to be the same as the remote one
git checkout --track [-b <local_branch>] <remote_repo>/<remote_branch>
The best command to check a remote and the branches that have been already or will be fetched is git remote show <remote_repo>
. It prints the list of branches under "Remote branch:" and also tells you if they have been fetched and if they are tracked.
You can check the branch restrictions in a remote also by listing the known remote branches with git branch -r
, in combination with grep
if you have many remotes, or by checking the remote details in the git config file .git/config
. It will contain a section like:
[remote "<remote_repo>"]
url = <remote_url>
fetch = +refs/heads/<remote_branch>:refs/remotes/<remote_repo>/<remote_branch>
Editing the config file will work to change the restrictions but I agree with @alexk that is not a good idea.
NOTE: If a branch is not in the list of branches of a remote (visible in git remote show
or the config file), then you will not be able to have a reference to it, git will save it to the temporary FETCH_HEAD and you will not be able to track it or to use it directly in git checkout
. This is the problem that brought me to this thread (the opposite of the one in the question): I cloned a repo with GitHub client gh repo clone USER/REPO
and it added automatically "upstream", the repository forked from, restricted only to the branch "master". I was unable to checkout other branches and getting errors like "fatal: '<remote_repo>/<remote_branch>' is not a commit and a branch '<local_branch>' cannot be created from it". I fixed it with: git remote set-branches <remote_repo> \*
.
Upvotes: 1
Reputation: 1449
Let me put in my two pence with a twist to MrMadsen's answer:
git fetch <remote_name_or_url> <branch_name>
git checkout FETCH_HEAD -B <branch_name>
The main advantage of these two lines over MrMadsen's proposal is that it will even work if the branch already exists locally.
Upvotes: 12
Reputation: 4745
This way works for me.
fetch the remote branch of the target branch:
git fetch origin branch-name
check out the target branch:
git checkout -b branch-name origin/branch-name
Here, I tried to fetch release-20.10.08 successfully.
name:directory zgong$ git fetch release-20.10.04 release-20.10.04
fatal: 'release-20.10.04' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git fetch origin release-20.10.04
From ssh://stash.trusted.visa.com:7999/vdcbc3a/vmcp-android-mobile-app
* branch release-20.10.04 -> FETCH_HEAD
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git checkout -b release-20.10.08 origin/release-20.10.08
fatal: 'origin/release-20.10.08' is not a commit and a branch 'release-20.10.08' cannot be created from it
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git fetch origin release-20.10.08
remote: Counting objects: 637, done.
remote: Compressing objects: 100% (320/320), done.
remote: Total 637 (delta 303), reused 465 (delta 202)
Receiving objects: 100% (637/637), 312.26 KiB | 262.00 KiB/s, done.
Resolving deltas: 100% (303/303), done.
From ssh://stash.trusted.visa.com:7999/vdcbc3a/vmcp-android-mobile-app
* branch release-20.10.08 -> FETCH_HEAD
* [new branch] release-20.10.08 -> origin/release-20.10.08
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git checkout -b release-20.10.08 origin/release-20.10.08
M VMCP/fmcore_android
M VMCP/foundation_android
M VMCP/mep_3ds_android
M VMCP/mep_login_android
M VMCP/mep_provisioning_and
Branch 'release-20.10.08' set up to track remote branch 'release-20.10.08' from 'origin'.
Switched to a new branch 'release-20.10.08'
Upvotes: 5
Reputation: 191
The answer actually depends on the current list of tracking branches you have. You can fetch a specific branch from remote with git fetch <remote_name> <branch_name>
only if the branch is already on the tracking branch list (you can check it with git branch -r
).
Let's suppose I have cloned the remote with --single-branch option previously, and in this case the only one tracking branch I have is the "cloned" one. I am a little bit bewildered by advises to tweak git config manually, as well as by typing git remote add <remote_name> <remote_url>
commands. As "git remote add" sets up a new remote, it obviously doesn't work with the existing remote repository; supplying "-t branch" options didn't help me.
In case the remote exists, and the branch you want to fetch exists in that remote:
git branch -r
whether you can see this branch as a tracking branch. If not (as in my case with a single branch clone), add this branch to the tracking branch list by "git remote set-branches" with --add option:git remote set-branches --add <remote_name> <branch_name>
git fetch <remote_name> <branch_name>
Note: only after the new tracking branch was fetched from the remote, you can see it in the tracking branch list with git branch -r
.git checkout --track <remote_name>/<branch_name>
Upvotes: 18
Reputation: 15713
The simplest way to do that
git fetch origin <branch> && git checkout <branch>
Example: I want to fetch uat branch from origin and switch to this as the current working branch.
git fetch origin uat && git checkout uat
Upvotes: 13
Reputation: 118
git version 2.16.1.windows.4
Just doing a git fetch remoteRepositoryName branchName (eg: git fetch origin my_local_branch)
is enough. Fetch will be done and a new local branch will be created with the same name and tracking will be set to remote branch.
Then perform git checkout branchName
Upvotes: 5
Reputation: 15432
<remote_name>
you'd like to use (feel free to use origin
and skip step 1.)git remote add <remote_name> <remote_url>
git fetch <remote_name> <branch>
<your_local_branch_name>
you'd like to use. Could be the same as <branch>
.git checkout <remote_name>/<branch> -b <your_local_branch_name>
Hope that helps!
Upvotes: 3
Reputation: 111
My workarounds:
git fetch --depth=1
git checkout <branch_name>
if you don't have a local clone:
git clone --depth 1 -b <branch_name> <repo_url>
Upvotes: 6
Reputation: 1694
git version: 2.74
This is how I do it:
git remote add [REMOTE-NAME] [REMOTE-URL]
git fetch [REMOTE-NAME] -- [BRANCH]
Upvotes: 5
Reputation: 13936
If you want to change the default for "git pull" and "git fetch" to only fetch specific branches then you can edit .git/config so that the remote config looks like:
[remote "origin"]
fetch = +refs/heads/master:refs/remotes/origin/master
This will only fetch master from origin by default. See for more info: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
EDIT: Just realized this is the same thing that the -t option does for git remote add. At least this is a nice way to do it after the remote is added if you don't want ot delete the remote and add it again using -t.
Upvotes: 17
Reputation: 11061
One way to do it:
in .git/config fetch for the remote repo should be set to fetch any branch:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
to fetch the remote branch:
git fetch origin branch-name
to create a local branch 'branch-name' set up to track remote branch 'branch-name' from origin.
git checkout -b branch-name origin/branch-name
to list all branches
git branch -a
Upvotes: 34
Reputation: 13706
One way is the following:
git fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>
This does not set up tracking though.
For more information, see the documentation of git fetch.
EDIT: As @user1338062 notes below: if you don't already have a local clone of the repository where you want to add the new branch, but you want to create a fresh local repository, then the git clone --branch <branch_name> --single-branch <repo_url>
provides a shorter solution.
Upvotes: 95
Reputation: 12745
For the sake of completeness, here is an example command for a fresh checkout:
git clone --branch gh-pages --single-branch git://github.com/user/repo
As mentioned in other answers, it sets remote.origin.fetch
like this:
[remote "origin"]
url = git://github.com/user/repo
fetch = +refs/heads/gh-pages:refs/remotes/origin/gh-pages
Upvotes: 15
Reputation: 5039
To update existing remote to track specific branches only use:
git remote set-branches <remote-name> <branch-name>
From git help remote
:
set-branches
Changes the list of branches tracked by the named remote. This can be used to track a subset of the available remote branches
after the initial setup for a remote.
The named branches will be interpreted as if specified with the -t option on the git remote add command line.
With --add, instead of replacing the list of currently tracked branches, adds to that list.
Upvotes: 52
Reputation: 590
Copied from the author's post:
Use the -t
option to git remote add
, e.g.:
git remote add -t remote-branch remote-name remote-url
You can use multiple -t branch
options to grab multiple branches.
Upvotes: 17