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


当前回答

从本地回购中克隆不能与gitclone和gitfetch一起使用:许多分支/标签将保持不匹配。

获取包含所有分支和标记的克隆。

git clone --mirror git://example.com/myproject myproject-local-bare-repo.git

要获取包含所有分支和标记但也包含工作副本的克隆,请执行以下操作:

git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master

其他回答

要创建存储在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中的所有文件都显示在暂存区域中。只是疯子。

为什么你只看到“大师”

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单行程序,但您可以将自己的定义为

git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'

然后将其用作

git clone-branches

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

哇!你有所有的树枝!