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


当前回答

使用您可以记住的命令

我正在使用Bitbucket,一个Atlassian的存储库托管服务。所以我试着遵循他们的文档。这对我来说非常有用。使用以下简单而简短的命令,您可以签出远程分支。

首先克隆存储库,然后转到目标文件夹。最后,但并非最不重要的是,取件和结账:

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

就是这样。这里有一个更真实的例子:

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

您将在文档中找到有关命令的详细信息:克隆命令、获取命令、签出命令

其他回答

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

复制粘贴到命令行:

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的答案。

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

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

git checkout -b branch origin/branchname

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

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

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

使用我的工具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