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


当前回答

除了用户没有人走在正确的轨道上之外,这些答案都无法解决问题。

我在将存储库从一个服务器/系统移动到另一个服务器或系统时遇到了问题。当我克隆存储库时,它只为master创建了一个本地分支,所以当我推送到新的远程时,只推送了master分支。

所以我发现这两种方法非常有用。

方法1:

git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo

方法2:

git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo

其他回答

除了用户没有人走在正确的轨道上之外,这些答案都无法解决问题。

我在将存储库从一个服务器/系统移动到另一个服务器或系统时遇到了问题。当我克隆存储库时,它只为master创建了一个本地分支,所以当我推送到新的远程时,只推送了master分支。

所以我发现这两种方法非常有用。

方法1:

git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo

方法2:

git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo

只需执行以下操作:

$ 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获取所有内容,甚至是分支,您只需签出它们,就可以创建本地分支。

我编写了这些小PowerShell函数,以便能够签出我的所有Git分支,这些分支位于源远程。

Function git-GetAllRemoteBranches {
     iex "git branch -r"                       <# get all remote branches #> `
     | % { $_ -Match "origin\/(?'name'\S+)" }  <# select only names of the branches #> `
     | % { Out-Null; $matches['name'] }        <# write does names #>
}


Function git-CheckoutAllBranches {
    git-GetAllRemoteBranches `
        | % { iex "git checkout $_" }          <# execute ' git checkout <branch>' #>
}

更多的Git函数可以在我的Git设置库中找到。

为什么你只看到“大师”

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通常(如果未指定)从一个或多个其他存储库中获取所有分支和/或标记(refs,请参见:Git-ls-refs)以及完成其历史所需的对象。换句话说,它获取已经下载的对象可以访问的对象。看:git fetch真正做什么?

有时,您可能有一些分支/标记没有直接连接到当前的分支/标记,因此git pull-all/git fetch-all在这种情况下不会有帮助,但您可以通过以下方式列出它们:

git ls-remote -h -t origin

并通过知道引用名称手动获取它们。

因此,要获取所有信息,请尝试:

git fetch origin --depth=10000 $(git ls-remote -h -t origin)

如果您对存储库进行了浅处理,--depth=10000参数可能会有所帮助。

然后再次检查所有分支:

git branch -avv

如果上述方法无效,则需要手动将丢失的分支添加到跟踪列表中(因为它们不知何故丢失了):

$ git remote -v show origin

...
  Remote branches:
    master      tracked

通过git远程设置分支,如:

git remote set-branches --add origin missing_branch

因此在获取后,它可能出现在remotes/origin下:

$ git remote -v show origin

...
  Remote branches:
    missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
 * [new branch]      missing_branch -> origin/missing_branch

故障排除

如果您仍然无法获得主分支以外的任何内容,请检查以下内容:

仔细检查遥控器(gitremote-v),例如。验证git-config-branch.master.remote是源代码。通过:git remote show origin检查origin是否指向正确的URL(参见本文)。