2

我在本地有两个相同的 git 存储库副本。这两个副本各有自己的本地分支机构。我可以以某种方式“统一”这两个存储库并创建一个具有两个存储库的本地分支的存储库吗?

4

2 回答 2

3

虽然通常用于引用 GitHub 之类的中央服务器,但 git 的“远程”概念实际上可以链接任意两个存储库,包括本地计算机上的两个目录。

因此,如果您在 /srv/foo 有一个副本,在 /srv/bar 有一个副本,您可以像这样将所有分支从一个获取到另一个:

cd /srv/foo
git remote add bar /srv/bar
git fetch bar

然后,这会将它们作为“远程跟踪分支”引入,因此“bar”副本上名为“feature-42”的分支将可以作为“bar/feature-42”访问。如果您删除 /srv/bar,它仍然会存在,就像如果您无法访问互联网,仍然可以访问来自 GitHub 的分支。

要将它们变成实际的本地分支机构,即。在没有“bar/”前缀的情况下访问它们,您可以依次检查它们,例如git switch feature-42

于 2021-05-28T07:56:34.970 回答
2

解决方案非常简单:

  1. 从存储库副本A将所有分支推送到 git 服务器。
  2. 从副本B中执行相同操作(两个存储库副本中的分支必须具有不同的名称)。
  3. 创建名为C的第三个存储库。
  4. 添加另外两个链接到副本AB的遥控器。
  5. 从A中拉出所有内容。
  6. 从B中拉出所有内容。
  7. 将所有分支推送到新存储库。

编辑

为简单起见,我们将只使用 2 个 repo...


~/repo-A $ cd repo-A
~/repo-A $ git branch --all

*  master
   branch-1
   branch-2

~/repo-A $ git push --all
~/repo-A $ git branch --all

*  master
   branch-1
   branch-2
   remotes/origin/branch-1
   remotes/origin/branch-2

~/repo-A $ git remote -v

origin  http://host/repo-A.git (fetch)
origin  http://host/repo-A.git (push)

~/repo-A $ cd ../repo-B
~/repo-B $ git push --all
~/repo-B $ git branch --all

*  master
   branch-3
   branch-4
   remotes/origin/branch-3
   remotes/origin/branch-4

~/repo-B $ git remote add repo-A http://host/repo-A.git
~/repo-B $ git pull --all repo-A
~/repo-B $ git branch --all

*  master
   branch-1
   branch-2
   branch-3
   branch-4
   remotes/repo-A/branch-1
   remotes/repo-A/branch-2
   remotes/origin/branch-3
   remotes/origin/branch-4

~/repo-B $ git push --all
~/repo-B $ git remote remove repo-A
~/repo-B $ git branch --all

*  master
   branch-1
   branch-2
   branch-3
   branch-4
   remotes/origin/branch-1
   remotes/origin/branch-2
   remotes/origin/branch-3
   remotes/origin/branch-4

# Now you can destry, if you want, the **repo-A** and keep only the **repo-B**.

于 2021-05-28T07:45:51.097 回答