How do I fetch only one branch of a remote Git repository?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

git fetch <remote_name> <branch_name>

Worked for me.

Solution 2 - Git

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.

Solution 3 - Git

I know there are a lot of answers already, but these are the steps that worked for me:

  1. git fetch <remote_name> <branch_name>
  2. git branch <branch_name> FETCH_HEAD
  3. 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:

Solution 4 - Git

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.

Solution 5 - Git

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

Solution 6 - Git

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.

Solution 7 - Git

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.

Solution 8 - Git

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

Solution 9 - Git

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:

  1. Check with 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>
  1. Fetch the branch you have added from the remote:
  • 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.
  1. Create and checkout a new local branch with "checkout --track", which will be given the same "branch_name" as a tracking branch:
  • git checkout --track <remote_name>/<branch_name>

Solution 10 - Git

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

Solution 11 - Git

git version: 2.74

This is how I do it:

git remote add [REMOTE-NAME] [REMOTE-URL]
git fetch [REMOTE-NAME] -- [BRANCH]

Solution 12 - Git

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

Solution 13 - Git

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>

Solution 14 - Git

Let me put in my two pence with a twist to MrMadsen's answer:

  1. git fetch <remote_name_or_url> <branch_name>
  2. 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.

Solution 15 - Git

  1. Pick any <remote_name> you'd like to use (feel free to use origin and skip step 1.)
  2. git remote add <remote_name> <remote_url>
  3. git fetch <remote_name> <branch>
  4. Pick any <your_local_branch_name> you'd like to use. Could be the same as <branch>.
  5. git checkout <remote_name>/<branch> -b <your_local_branch_name>

Hope that helps!

Solution 16 - Git

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'

Solution 17 - Git

The reply depends on what you want to accomplish.

  1. If it is a one time deal from a different repository and you don't need a reference (e.g. to merge GitHub Pull Requests, where is like https://github.com/USER/REPO.git), then you can use:
    git checkout -b <local_branch> <local_branch_to merge_into>
    git pull <remote_url> <remote_branch>
    
  2. If you want to update and track the branch you have to set first the remote and there are 4 alternatives:
    1. If you are cloning a new repository (e.g. working only on it)
      git clone --single-branch --branch remote_branch remote_url
      
    2. If you are adding a new remote to your working directory
      # multiple -t options are allowed
      git remote add -t <remote_branch> <remote_repo> <remote_url>
      
    3. If you are adding the branch restriction to an existing remote in your working directory
      # 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>
      
    4. You could also skip the restrictions because clone by default fetches only the main branch and remote add does not fetch branches. Buth then you'll have to mention the remote branch all the remote branch all the time you fetch the remote_repo.
      git remote add <remote_repo> <remote_url>
      
    After setting the remote you can fetch the remote branch, checkout and pull:
    # 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: '/' is not a commit and a branch '' cannot be created from it". I fixed it with: git remote set-branches <remote_repo> \*.

Solution 18 - Git

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmellisView Question on Stackoverflow
Solution 1 - GitAbdulsattar MohammedView Answer on Stackoverflow
Solution 2 - Githcs42View Answer on Stackoverflow
Solution 3 - GitMrMadsenView Answer on Stackoverflow
Solution 4 - GitIkar PohorskýView Answer on Stackoverflow
Solution 5 - GitklodeView Answer on Stackoverflow
Solution 6 - GitastronautView Answer on Stackoverflow
Solution 7 - GitColinMView Answer on Stackoverflow
Solution 8 - Gituser1338062View Answer on Stackoverflow
Solution 9 - GitAlexKView Answer on Stackoverflow
Solution 10 - GitAmitesh BhartiView Answer on Stackoverflow
Solution 11 - GitscottalanView Answer on Stackoverflow
Solution 12 - GitKarthikeyan MohanView Answer on Stackoverflow
Solution 13 - GitliyView Answer on Stackoverflow
Solution 14 - GitToJoView Answer on Stackoverflow
Solution 15 - GitLukasz CzerwinskiView Answer on Stackoverflow
Solution 16 - GitFrancis BaconView Answer on Stackoverflow
Solution 17 - GitmarcoView Answer on Stackoverflow
Solution 18 - GitDan M.View Answer on Stackoverflow