如何在Git中克隆所有远程分支?

我有一个master和一个development分支,都推到GitHub 。 我已经clone d, fetch ed,然后fetch ed,但是除了master分支之外,我仍然无法获得任何东西。

我确信我错过了一些明显的东西,但是我已经阅读了手册,而且我也没有得到任何的喜悦。

首先,克隆一个远程的Git仓库,然后cd进去:

 $ git clone git://example.com/myproject $ cd myproject 

接下来,查看您的存储库中的本地分支:

 $ git branch * master 

但是还有其他分支隐藏在你的仓库中! 你可以使用-a标志看到这些:

 $ git branch -a * master remotes/origin/HEAD remotes/origin/master remotes/origin/v1.0-stable remotes/origin/experimental 

如果您只想快速浏览上游分支,可以直接查看:

 $ git checkout origin/experimental 

但是,如果您想在该分支上工作,则需要创build一个本地跟踪分支,该分支自动完成:

 $ git checkout experimental 

你会看到的

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

最后一行提出了一些人:“新分支” – 嗯? 真正意义的是,分支是从索引中获取的,并在本地为您创build。 一行实际上是更多的信息,因为它告诉你分支正在被设置为跟踪远程分支,这通常意味着origin / branch_name分支

现在,如果你看看你当地的分支机构,你会看到:

 $ git branch * experimental master 

你可以使用git remote实际上跟踪多个远程仓库。

 $ git remote add win32 git://example.com/users/joe/myproject-win32-port $ git branch -a * master remotes/origin/HEAD remotes/origin/master remotes/origin/v1.0-stable remotes/origin/experimental remotes/win32/master remotes/win32/new-widgets 

在这一点上,事情变得非常疯狂,所以运行gitk来看看发生了什么事情:

 $ gitk --all & 

如果您想要一次获取多个远程分支机构,请执行以下操作:

 $ git pull --all 

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

这个Bash脚本帮了我:

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

它将创build所有远程分支的追踪分支,除了master(您可能从原始的clone命令中获得)。 我想你可能还需要做一个

 git fetch --all git pull --all 

为了确定。

一个class轮git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs像往常一样:在复制rm -rf universe之前testing你的设置,就像我们所知道的一样

单线的信用转到用户cfi

使用--mirror选项似乎正确地复制remote跟踪分支。 但是,它将存储库设置为裸存储库,因此您必须事后将其恢复为正常的存储库。

 git clone --mirror path/to/original path/to/dest/.git cd path/to/dest git config --bool core.bare false git checkout anybranch 

参考: Git的常见问题:我如何克隆与所有远程跟踪分支的存储库?

您可以轻松切换到分支,而不使用花哨的“git checkout -b somebranch origin / somebranch”语法。 你可以做:

 git checkout somebranch 

Git会自动做正确的事情:

 $ git checkout somebranch Branch somebranch set up to track remote branch somebranch from origin. Switched to a new branch 'somebranch' 

Git会检查一个分支是否存在于同一个远程分支中,如果是,则跟踪它,就像明确指定它是远程分支一样。 从Git 1.8.2.1的git-checkout手册页:

如果没有find<branch>,但是确实在一个远程(有一个匹配的名字称为<remote>)中存在一个跟踪分支,则视为等同于

 $ git checkout -b <branch> --track <remote>/<branch> 

关于,

$ git checkout -b实验起源/实验

运用

 $ git checkout -t origin/experimental 

或者更详细但更容易记住

 $ git checkout --track origin/experimental 

在跟踪远程存储库方面可能会更好。

您正在执行的提取操作应获取所有远程分支,但不会为其创build本地分支。 如果你使用gitk,你应该看到描述为“遥控/原点/开发”或类似的远程分支。

要创build基于远程分支的本地分支,请执行以下操作:

  git checkout -b dev refs / remotes / origin / dev 

这应该返回像这样的东西:

 分支开发设置来跟踪远程分支参考/远程/原产地/开发。
切换到一个新的分支“开发” 

现在,当你在开发分支上时,“git pull”会将你的本地开发更新到与远程开发分支相同的点。 请注意,它将获取所有分支,但仅将您所在的分支拉到树顶部。

当你做“git clone git:// location”时,所有的分支和标签都被提取。

为了在特定的远程分支上工作,假设它是远程的:

 git checkout -b branch origin/branchname 

使用别名。 虽然没有任何原生的Git单行程序,但是可以将自己定义为

 git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t' 

然后用它作为

 git clone-branches 

这不是太复杂,非常简单直接的步骤如下;

git fetch origin这将把所有的远程分支到你的本地。

git branch -a这将显示所有的远程分支。

git checkout --track origin/<branch you want to checkout>

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

 git branch 

输出将是这样的;

 *your current branch some branch2 some branch3 

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

为什么你只看到“主人”

git clone下载所有远程分支,但仍然认为它们是“远程的”,即使这些文件位于新的存储库中。 这有一个例外,那就是克隆过程从远程分支“master”创build一个名为“master”的本地分支。 默认情况下, git branch只显示本地分支,这就是为什么你只看到“主”。

git branch -a显示所有分支, 包括远程分支


如何获得当地的分支机构

如果你真的想在分支上工作,你可能需要一个“本地”版本。 要简单地从远程分支创build本地分支(不检查它们,从而更改工作目录的内容) ,可以这样做:

 git branch branchone origin/branchone git branch branchtwo origin/branchtwo git branch branchthree origin/branchthree 

在这个例子中, branchone是根据origin/branchone创build的本地分支的名称; 如果您不想使用不同名称创build本地分行,则可以这样做:

 git branch localbranchname origin/branchone 

一旦你创build了一个本地分支,你可以看到它与git branch (记住,你不需要-a看到本地分支)。

只要这样做:

 $ git clone git://example.com/myproject $ cd myproject $ git checkout branchxyz Branch branchxyz set up to track remote branch branchxyz from origin. Switched to a new branch 'branchxyz' $ git pull Already up-to-date. $ git branch * branchxyz master $ git branch -a * branchxyz master remotes/origin/HEAD -> origin/master remotes/origin/branchxyz remotes/origin/branch123 

你看,'git clone git://example.com/myprojectt'获取所有东西,甚至分支,你只需要检出它们,那么你的本地分支就会被创build。

迟到比从未好,但这是做到这一点的最佳方式:

 mkdir repo cd repo git clone --bare path/to/repo.git .git git config --unset core.bare git reset --hard 

在这一点上,你有一个完整的远程回购与所有的分支副本(validation与git branch )。 如果你的远程--bare有自己的遥控器,你可以使用--mirror而不是--bare

你只需要使用“git clone”来获得所有分支。

 git clone <your_http_url> 

即使你只看到主分支,你可以使用“git branch -a”来查看所有分支。

 git branch -a 

你可以切换到你已有的分支。

 git checkout <your_branch_name> 

不要担心在你“git clone”之后,你不需要连接远程的repo,当你closures你的wifi时,“git branch -a”和“git checkout”可以成功运行。 所以certificate当你做“git clone”的时候,它已经复制了远程回购的所有分支。 之后,你不需要远程回购,你的本地已经有所有分行的代码。

使用我的工具git_remote_branch (你需要在你的机器上安装Ruby)。 它是专门为了使远程分支操作变得简单而devise的。

每次代表您进行操作时,都会在控制台上以红色打印。 随着时间的推移,他们终于坚持到你的大脑:-)

如果您不想grb代表您运行命令,只需使用“解释”function即可。 这些命令将被打印到您的控制台,而不是为您执行。

最后,所有命令都有别名,使记忆更容易。

请注意,这是alpha软件 😉

以下是运行grb help时的帮助:

 git_remote_branch版本0.2.6

  用法:

   grb create branch_name [origin_server] 

   grb发布branch_name [origin_server] 

   grb重命名branch_name [origin_server] 

   grb delete branch_name [origin_server] 

   grb track branch_name [origin_server] 



  笔记:
   - 如果未指定origin_server,则假定名称为“origin” 
     (git的默认)
   - 重命名function重命名当前分支

  解释元命令:你也可以在前面添加任何命令 
关键字“解释”。 而不是执行命令,git_remote_branch 
将只输出你需要运行的命令列表来完成 
那个目标。

  例: 
     grb解释创build
     grb解释创buildmy_branch github

  所有的命令也有别名:
  创build:创build新的
  删除:删除,销毁,杀死,删除,RM
  发布:发布,远程
  重命名:重命名,RM,MV,移动
  跟踪:跟踪,跟随,抓取,抓取

git clone应该复制整个存储库。 尝试克隆它,然后运行git branch -a 。 它应该列出所有的分支。 如果你想切换到分支“富”而不是“主”,使用git checkout foo

看问题的答案之一,我注意到可以缩短它:

 for branch in `git branch -r | grep -v 'HEAD\|master'`; do git branch --track ${branch##*/} $branch; done 

但要小心,如果远程分支之一被命名为例如admin_master它不会被下载!

感谢bigfish原创的想法

我在这里看到的所有答案都是有效的,但是有一个更简洁的方法来克隆一个存储库并一次拉出所有的分支。

当你克隆一个存储库时,所有分支的信息实际上是下载的,但分支是隐藏的。 用命令

 $ git branch -a 

您可以显示存储库的所有分支,并使用该命令

 $ git checkout -b branchname origin/branchname 

您可以一次手动“下载”它们。


然而,当你想克隆一个有很多分支的回购时,上面所说明的所有方法都是漫长而乏味的,就我将要展示的一个更干净更快捷的方式而言,虽然这有点复杂。 你需要三个步骤来完成这个:

  1. 第一步

在您的机器上创build一个新的空文件夹,并从存储库克隆.git文件夹的镜像副本:

 $ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder $ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git 

文件夹my_repo_folder中的本地存储库仍然是空的,现在只有一个隐藏的.git文件夹,您可以通过terminal的“ls -alt”命令来查看。

  1. 第二步

通过将gitconfiguration的布尔值“裸”切换为false,将该存储库从空(裸)存储库切换到常规存储库:

 $ git config --bool core.bare false 
  1. 第三步

抓住当前文件夹内的所有内容,并创build本地机器上的所有分支,因此使这是一个正常的回购。

 $ git reset --hard 

所以现在你只需要input命令“git branch”,你就可以看到所有的分支都被下载了。

这是您可以快速克隆所有分支的git存储库的方法,但是这不是您想为每个项目做的事情。

我需要做的完全一样。 这是我的Ruby脚本。

 #!/usr/bin/env ruby local = [] remote = {} # Prepare %x[git reset --hard HEAD] %x[git checkout master] # Makes sure that * is on master. %x[git branch -a].each_line do |line| line.strip! if /origin\//.match(line) remote[line.gsub(/origin\//, '')] = line else local << line end end # Update remote.each_pair do |loc, rem| next if local.include?(loc) %x[git checkout --track -b #{loc} #{rem}] end %x[git fetch] 

复制粘贴到命令行中:

 git checkout master ; remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master 

更多的可读性:

 git checkout master ; remote=origin ; for brname in ` git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}' `; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master 

这会:

  1. 检查出主(这样我们可以删除我们的分支)
  2. select远程结帐(改变它到任何你有远程)
  3. 循环除了主设备和HEAD以外的所有分支
    1. 删除本地分支(这样我们可以检查出强制更新的分支)
    2. 从远程检查分支
  4. 检查出主(为了它)

基于VonC的 回答 。

Git通常(当没有指定时)从一个或多个其他存储库获取所有分支和/或标签(参考: git ls-refs )以及完成其历史logging所需的对象。 换句话说,它将获取已经下载的对象可访问的对象。 请参阅: git fetch究竟做了什么?

有时你可能有分支/标签不直接连接到当前的,所以git pull --all / git fetch --all都不会在这种情况下帮助,但是你可以通过以下方式列出它们:

 git ls-remote -h -t origin 

并通过知道ref名称手动获取它们。

所以把他们都拿走 ,试试:

 git fetch origin --depth=10000 $(git ls-remote -h -t origin) 

--depth=10000参数可能会帮助你如果你已经浅储存库。

然后再检查你所有的分支机构:

 git branch -avv 

如果上面的内容不起作用,你需要手动添加缺失的分支到追踪列表(因为他们迷路了):

 $ git remote -v show origin ... Remote branches: master tracked 

通过git remote set-branches如:

 git remote set-branches --add origin missing_branch 

所以它可能出现在remotes/origin之后:

 $ git remote -v show origin ... Remote branches: missing_branch new (next fetch will store in remotes/origin) $ git fetch From github.com:Foo/Bar * [new branch] missing_branch -> origin/missing_branch 

故障排除

如果您仍然无法获得主分支以外的任何东西,请检查以下内容:

  • 仔细检查你的遥控器( git remote -v ),例如
    • validationgit config branch.master.remoteorigin
    • 检查是否origin指向正确的URL通过: git remote show origin (见这个职位 )。
 #!/bin/bash for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do git branch --track ${branch#remotes/origin/} $branch done 

这些代码将所有远程分支代码拉到本地回购。

Cloning from a local repo will not work with git clone & git fetch: a lot of branches/tags will remain unfetched.

To get a clone with all branches and tags.

 git clone --mirror git://example.com/myproject myproject-local-bare-repo.git 

To get a clone with all branches and tags but also with a working copy:

 git clone --mirror git://example.com/myproject myproject/.git cd myproject git config --unset core.bare git config receive.denyCurrentBranch updateInstead git checkout master 

None of these answers cut it, except user nobody is on the right track.

I was having trouble with moving a repo from one server/system to another. When I cloned the repo, it only created a local branch for master so when I pushed to the new remote, only master branch was pushed.

So I found these two methods VERY useful. Hope they help someone else.

Method 1:

 git clone --mirror OLD_REPO_URL cd new-cloned-project mkdir .git mv * .git git config --local --bool core.bare false git reset --hard HEAD git remote add newrepo NEW_REPO_URL git push --all newrepo git push --tags newrepo 

Method 2:

 git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t' git clone OLD_REPO_URL cd new-cloned-project git clone-branches git remote add newrepo NEW_REPO_URL git push --all newrepo git push --tags newrepo 

Here is another short one-liner command which creates local branches for all remote branches:

 (git branch -r | sed -n '/->/!s#^ origin/##p' && echo master) | xargs -L1 git checkout 

It works also properly if tracking local branches are already created. You can call it after the first git clone or any time later.

If you do not need to have master branch checked out after cloning, use

 git branch -r | sed -n '/->/!s#^ origin/##p'| xargs -L1 git checkout 

Use commands that you can remember

I'm using Bitbucket, a Repository Hosting Service of Atlassian. So I try to follow their docs. And that works perfectly for me. With the following easy and short commands you can checkout your remote branch.

At first clone your repository, then change into the destination folder. And last but not least fetch and checkout:

 git clone <repo> <destination_folder> cd <destination_folder> git fetch && git checkout <branch> 

而已。 Here a litte more real world example:

 git clone https://username@bitbucket.org/team/repository.git project_folder cd project_folder git fetch && git checkout develop 

You will find detail information about the commands in the docs: Clone Command , Fetch Command , Checkout Command

I wrote this small Powershell functions to be able to checkout all my git branches, that are on origin remote.

 Function git-GetAllRemoteBranches { iex "git branch -r" <# get all remote branches #> ` | % { $_ -Match "origin\/(?'name'\S+)" } <# select only names of the branches #> ` | % { Out-Null; $matches['name'] } <# write does names #> } Function git-CheckoutAllBranches { git-GetAllRemoteBranches ` | % { iex "git checkout $_" } <# execute ' git checkout <branch>' #> } 

More git functions can be found on my git settings repo

As of early 2017, the answer in this comment works:

git fetch <origin-name> <branch-name> brings the branch down for you. While this doesn't pull all branches at once, you can singularly execute this per-branch.

I'm going to add my 2 cents here because I got here trying to find out how to pull down a remote branch I had deleted locally. Origin was not mine, and I didn't want to go through the hassle of re-cloning everything

This worked for me:

assuming you need to recreate the branch locally:

 git checkout -b recreated-branch-name git branch -a (to list remote branches) git rebase remotes/remote-origin/recreated-branch-name 

So if I forked from gituser/master to sjp and then branched it to sjp/mynewbranch it would look like this:

 $ git checkout -b mynewbranch $ git branch -a master remotes/sjp/master remotes/sjp/mynewbranch $ git fetch (habit to always do before) $ git rebase remotes/sjp/mynewbranch 

A little late to the party, but I think this does the trick:

 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 

If this does something undesirable, I'd love to know. However, so far, this works for me.