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


当前回答

只需执行以下操作:

$ 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

你看,gitclonegit://example.com/myprojectt获取所有内容,甚至是分支,您只需签出它们,就可以创建本地分支。

其他回答

复制粘贴到命令行:

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下载所有远程分支,但仍将其视为“远程”,即使文件位于新存储库中。有一个例外,那就是克隆过程从名为“master”的远程分支创建了名为“master”的本地分支。默认情况下,gitbranch只显示本地分支,这就是为什么您只看到“master”。

gitbranch-a显示所有分支,包括远程分支。


如何获得本地分支机构

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

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

在本例中,branchone是基于origin/branchone创建的本地分支的名称;如果您希望创建具有不同名称的本地分支,可以执行以下操作:

git branch localbranchname origin/branchone

创建本地分支后,可以使用git分支查看它(记住,不需要-a来查看本地分支)。

关于

git checkout-b实验来源/实验

使用

git checkout -t origin/experimental

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

git checkout --track origin/experimental

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

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

首先,克隆一个远程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

要处理该分支,请创建一个本地跟踪分支,该分支通过以下方式自动完成:

$ git checkout experimental

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

这里,“新分支”只是指从索引中获取分支并在本地为您创建。正如前一行告诉的那样,正在设置分支以跟踪远程分支,这通常意味着origin/branch_name分支。

您的本地分支机构现在应该显示:

$ git branch
* experimental
  master

您可以使用gitremote跟踪多个远程存储库:

$ 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 &