远程存储库包含各种分支,如origin/davis_branch:

$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master

如何将daves_branch获取到本地存储库,以便它跟踪origin/davis_branch?

我尝试了:

$ git fetch origin discover
$ git checkout discover

当前回答

检查.git/config文件,特别是该远程设备的fetch跟踪。

[remote "randomRemote"]
    url = git@github.com:someUser/someRepo.git
    fetch = +refs/heads/*:refs/remotes/randomRemote/*

如果头/*指向randomRemote/*,当您运行gitfetchrandomRemote时,它将获取所有分支。

然后你就可以结账了。

否则

您需要使用此命令将远程分支添加到跟踪中。运行此命令后,请检查.git/config。你会明白的。git remote set branches--add randomRemote randomBranch运行git fetch randomRemote。这将获取远程分支。现在您可以运行git checkout randomBranch。

其他回答

有时,你被要求不要摆弄主分支,只在远程分支工作(正如我被要求的那样)。所以你只需要远程分支。

因此,要单独克隆远程分支(没有主分支),请执行以下操作

git clone url --branch remote_branch_name

哪里remote_branch_name是远程分支的名称

例如

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15

这将确保您使用远程分支的名称将远程分支克隆到本地分支。

现在,如果您提交代码并推送,代码将单独提交给该分支。

如果你已经知道你的远程分支了。。。

git remote
=> One
=> Two

并且您知道要签出的分支名称,例如br1.2.3.4,然后执行

git fetch One
=> returns all meta data of remote, that is, the branch name in question.

剩下的就是结账

git checkout br.1.2.3.4

然后用它做新的树枝。

要获取远程上存在的分支,最简单的方法是:

git fetch origin branchName
git checkout branchName

您可以通过以下方式查看它是否已存在于远程:

git branch -r

这会将远程分支提取到本地,并自动跟踪远程分支。

帮助我的是

1) 查看所有可用的远程分支(例如“远程分支名称”)

git branch -r

2) 使用远程分支名称创建本地分支

git fetch && git checkout 'remote-branch-name'

要签出远程而非本地存在的myBranch,这对我很有用:

git fetch --all
git checkout myBranch

我收到了这条消息:

Branch myBranch set up to track remote branch myBranch from origin
Switched to a new branch 'myBranch'