我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?


当前回答

如果您有许多远程分支要同时获取,请执行以下操作:

git pull --all

现在,您可以根据需要签出任何分支,而无需访问远程存储库。


注意:这不会创建任何未签出分支的工作副本,这就是问题所在。对此,请参见

大鱼的回答戴夫的回答

其他回答

这并不太复杂。非常简单明了的步骤如下:;

gitfetchorigin:这将把所有远程分支带到本地。

gitbranch-a:这将显示所有远程分支。

git checkout--跟踪origin/<要签出的分支>

通过以下命令验证您是否在所需的分支中;

git branch

输出将是这样的;

*your current branch
some branch2
some branch3

请注意表示当前分支的*符号。

我认为这样做很有效:

mkdir YourRepo
cd YourRepo
git init --bare .git                       # create a bare repo
git remote add origin REMOTE_URL           # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/*   # fetch tags
git init                                   # reinit work tree
git checkout master                        # checkout a branch

到目前为止,这对我有效。

如果使用Bitbucket,则可以使用import Repository。这将导入所有Git历史记录(所有分支和提交)。

截至2017年初,该评论中的答案有效:

gitfetch<origin name><branch name>为您关闭分支。虽然这不会同时拉动所有分支,但您可以对每个分支单独执行此操作。

我无法编辑Bigfish的答案。他提出了一个bash脚本,我提供它来更新并提供更好的git集成。grep已经过时,应该用grep-E替换。

#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | grep -E --invert-match '(:?HEAD|master)$'); do
        git branch --track "${branch##*/}" "$branch"
done

您可以通过将此bash文件添加为git自定义子命令来扩展git:

$ mkdir ~/.gitbin; touch ~/.gitbin/git-fetchThemAll
$ chmod u+x ~/.gitbin/git-fetchThemAll

将bash脚本的内容放在gitfetchThemAll中。

$ echo 'export PATH="$HOME/.gitbin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc # update PATH in your current shell
$ git fetchThemAll

如果您愿意,可以使用用户cfi为这个oneliner使用shell别名

aliasfetchThemAll=gitbranch-a|grep-vHEAD|perl-ne'chomp($_);s |^\*?\s*||;如果(m|(.+)/(.+)|&&not$d{$2}){print qq(git branch--track$2$1/$2\n)}否则{$d{$_}=1}‘|csh-xfs