将 GIT 分支克隆到另一个存储库 GIT 分支?

Cloning a GIT branch to another repositories GIT branch?

我有两个 GIT 仓库,一个用于工作,一个用于独立开发。我的公司对我一直在从事的项目表示了兴趣,并希望我将该分支 (master) 迁移到他们的 repo (Features/Dynamo) 上的一个新分支。 I have created a migrator in the past 但这破坏了被推送到的回购协议,因为它是直接的 --bare then --mirror。我需要做什么才能将一个分支从一个存储库移植到另一个存储库(同时在此过程中创建新分支)?

一个简单的方法是在一台机器上克隆两个存储库,然后在 "to" 存储库 git checkout -b new_branch 中,然后 cp 从另一台机器中复制文件项目目录。 git 添加并 git 提交,你就完成了。正如我上面所问的,这是否有效取决于您是否有相同的 dir/filenames。如果这样做,您会遇到合并冲突,并且两个不同的文件会以非常糟糕的方式混合在一起。

理论上,这就是您可以做的,但我认为拥有两个不同的存储库会更好。 注意:这不会带来您的整个 git 历史记录,它只会将项目的最新状态添加为一个分支。

$ git checkout -b OtherProject

-- to prevent losing your git database
$ mv .git/ /temp/gitBackup

-- clear out all the stuff from the main repository
$ rm -rf *

-- copy contents of the tip of master from your other project
-- but not the .git/ folder
$ cp -r /path/to/personal/project/* .
$ rm -rf .git/

-- bring back your old .git folder
$ mv /temp/gitBackup ./.git

-- add all your new files to this branch
$ git add -A .
$ git commit -am "The other project is now in this repo as a branch"

再次强调,这不是推荐的做法。您的 git 数据库将变得更大,这不是分支的预期用途。我强烈建议每个项目使用一个存储库,并可能使用 submodules 从 git 存储库中管理子项目。

另一种方法是添加另一个遥控器。

git remote add ehime https://github.com/your_username/your_repo_name.git

现在你所做的一切都像

git push origin some_branch

也可以做到

git push ehime some_branch

另一个遥控器。

因此两个遥控器和一个代码目录。

对于共享文件,合并时有很多选项,例如

git checkout branchA
git merge ours branchB

git checkout branchA
git merge -X theirs branchB

控制两个代码库之间的合并方式。