问题描述
我在本地有两个相同的 git 存储库副本。这两个副本都有自己的本地分支机构。我可以以某种方式“统一”这两个存储库并创建一个将来自两个存储库的本地分支吗?
解决方法
虽然通常用于引用像 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
解决方案很简单:
- 从仓库复制A将所有分支推送到git服务器。
- 对副本 B 执行相同操作(两个存储库副本之间的分支名称必须不同)。
- 创建第三个名为 C 的存储库。
- 添加另外两个链接到复制A和B的遥控器。
- 从 A 中提取所有内容。
- 从 B 中提取所有内容。
- 将所有分支推送到新存储库。
编辑
为了简单起见,我们将只使用 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**.