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


当前回答

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

其他回答

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

关于

git checkout-b实验来源/实验

使用

git checkout -t origin/experimental

或者更冗长,但更容易记住

git checkout --track origin/experimental

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

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

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
}

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

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

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

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

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

git branch

输出将是这样的;

*your current branch
some branch2
some branch3

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

我也需要做同样的事情。这是我的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]