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


当前回答

下面是另一个简短的单行命令为所有远程分支创建本地分支:

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

如果已经创建了跟踪本地分支,它也可以正常工作。您可以在第一个git克隆之后或以后的任何时间调用它。

如果克隆后不需要签出主分支,请使用

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

其他回答

独立存储库

如果要查找包含所有远程分支和提交日志的自包含克隆或备份,请使用:

git clone http://user@repo.url
git pull --all

gitbranch-a的接受答案仅显示远程分支。如果您尝试签出分支,除非您仍然具有对源服务器的网络访问权限,否则将无法签出。

信贷:加布·科普利(Gabe Kopley)建议使用git pull-all。

注:当然,如果您不再具有远程/源服务器的网络访问权限,那么远程/源分支将不会在存储库克隆中反映任何更新。它们的修订将反映您执行上述两个存储库克隆命令的日期和时间的提交。


Checkout a *local* branch in the usual way with `git checkout remote/origin/` Use `git branch -a` to reveal the remote branches saved within your `clone` repository.

要使用一个命令将所有克隆分支签出到本地分支,请使用以下bash命令之一:

$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \ 
| grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done

OR

如果您的回购具有嵌套分支,则此命令还将考虑这一点:

for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done

上述命令将将本地分支签入本地git存储库,其名称与remote/orging/<branchname>相同,并将其设置为--如果您再次重新获得对原始存储库服务器的网络访问并以通常方式执行git pull命令,则跟踪远程/orging服务器上远程分支的更改。

以下是实现此目的的最佳方法:

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

此时,您已经拥有了远程存储库及其所有分支的完整副本(使用git分支进行验证)。如果远程存储库有自己的远程设备,则可以使用--mirror而不是--bare。

当您执行“gitclone”时git://location“,将获取所有分支和标记。

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

git checkout -b branch origin/branchname

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

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主机(github/bitbucket/etc)中的所有分支+引用+标记+等的“完整”备份,请运行:

mkdir -p -- myapp-mirror
cd myapp-mirror
git clone --mirror https://git.myco.com/group/myapp.git .git
git config --bool core.bare false
git config --bool core.logAllRefUpdates true
git reset --hard # restore working directory

这是根据我从其他答案中学到的一切整理而成的。

然后,您可以使用此本地回购镜像转换到不同的SCM系统/git主机,也可以将其作为备份。它作为搜索工具也很有用,因为大多数git主机只搜索每个repo的“main”分支上的代码,如果您使用git log-s“specialVar”,您将看到所有分支上的所有代码。

注意:如果您想在日常工作中使用此回购,请运行:

git config --unset remote.origin.mirror

警告:如果你试图在日常工作中使用它,你可能会遇到奇怪的问题。如果您的ide/编辑器正在进行一些自动提取,您的本地主控器可能会更新,因为您做了gitclone--mirror。然后这些文件会出现在git暂存区。事实上,我有一种情况,我在当地的一个特色分支。。该分支没有提交,repo中的所有文件都显示在暂存区域中。只是疯子。