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


当前回答

这个解决方案使我能够将一个存储库“复制”到另一个:

git merge path/to/source.git --mirror
cd source.git
git remote remove origin
git remote add origin path/to/target.git
git push origin --all
git push origin --tags

在目标存储库中,我可以看到与原始存储库相同的分支和标记。

其他回答

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

git pull --all

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


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

大鱼的回答戴夫的回答

复制粘贴到命令行:

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

这将:

签出master(以便我们可以删除我们所在的分支)选择要签出的远程(将其更改为您拥有的任何远程)循环通过远程的所有分支,主分支和HEAD除外0。删除本地分支(以便我们可以检出强制更新的分支)0。从远程签出分支检查一下主人(为了它)

这是基于VonC的答案。

在这里,我为您编写了一个很好的函数,使其易于重复

gitCloneAllBranches() { # clone all git branches at once easily and cd in
  # clone as "bare repo"
  git clone --mirror $1
  # rename without .git extension
  with_extension=$(basename $1)
  without_extension=$(echo $with_extension | sed 's/.git//')
  mv $with_extension $without_extension
  cd $without_extension
  # change from "bare repository" to not
  git config --bool core.bare false
  # check if still bare repository if so
  if [[ $(git rev-parse --is-bare-repository) == false ]]; then
    echo "ready to go"
  else
    echo "WARNING: STILL BARE GIT REPOSITORY"
  fi
  # EXAMPLES:
  # gitCloneAllBranches https://github.com/something/something.git
}

使用我的工具git_remote_branch(grb)。您需要在机器上安装Ruby)。它是专门为使远程分支操作非常容易而构建的。

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

如果您不希望grb代表您运行命令,请使用“解释”功能。这些命令将打印到您的控制台,而不是为您执行。

最后,所有命令都有别名,以便于记忆。

注意,这是alpha软件;-)

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

git_remote_branch version 0.2.6

  Usage:

  grb create branch_name [origin_server]

  grb publish branch_name [origin_server]

  grb rename branch_name [origin_server]

  grb delete branch_name [origin_server]

  grb track branch_name [origin_server]



  Notes:
  - If origin_server is not specified, the name 'origin' is assumed
    (git's default)
  - The rename functionality renames the current branch

  The explain meta-command: you can also prepend any command with the
keyword 'explain'. Instead of executing the command, git_remote_branch
will simply output the list of commands you need to run to accomplish
that goal.

  Example:
    grb explain create
    grb explain create my_branch github

  All commands also have aliases:
  create: create, new
  delete: delete, destroy, kill, remove, rm
  publish: publish, remotize
  rename: rename, rn, mv, move
  track: track, follow, grab, fetch

此变体将克隆具有本地可用的所有分支的远程回购,而无需逐个签出每个分支。不需要花哨的脚本。

创建一个与要克隆的存储库同名的文件夹,并将其放入cd中,例如:

mkdir somerepo
cd somerepo

现在执行这些命令,但使用实际的repo用户名/reportname

git clone --bare git@github.com:someuser/somerepo.git .git
git config --bool core.bare false
git reset --hard
git branch

哇!你有所有的树枝!