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?
This question is related to
git
git version: 2.74
This is how I do it:
git remote add [REMOTE-NAME] [REMOTE-URL]
git fetch [REMOTE-NAME] -- [BRANCH]
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
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.
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.
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>
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
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
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.
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:
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>
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
<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!
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.
git fetch <remote_name> <branch_name>
Worked for me.
Source: Stackoverflow.com