合并远程 Git 仓库到子文件夹。

huangapple go评论59阅读模式
英文:

Git merge remotes into a subfolder

问题

我试图从多个现有的仓库创建一个 monorepo。我已经创建了一个带有自己的 .git、README.md 等文件的项目,还有一个 /packages 文件夹,我想把合并的仓库放在这里作为子文件夹。但是在我执行以下操作后:

cd packages
git remote add existing-1 git@...
git fetch --all
git merge existing-1/master --allow-unrelated-histories

我遇到了合并冲突(README.md 等),因为 git 想要将仓库合并到根目录,而不是 /packages。我该怎么办?

英文:

I am trying to create a monorepo from multiple existing repos. I have created a project with its own .git, README.md, ... and a /packages folder, where I want to place the merged repos as subfolders. But after I do this

cd packages
git remote add existing-1 git@...
git fetch --all
git merge existing-1/master --allow-unrelated-histories

I get merge conflicts (README.md etc), because git wants to merge the repo into root, not /packages. What can I do?

答案1

得分: 2

最快的方法是将所有文件从旧存储库复制到新存储库,然后进行一次非常大的提交。

如果你坚持保留历史记录,我可以想到两种方法:

  1. 开始一个干净的存储库,像你已经在做的那样从旧存储库中获取。然后使用 git mv 命令将文件移动到你想要的新子文件夹结构中。对于每个要添加到新存储库的项目,重复这个过程。最后,为你的单体存储库添加脚手架。

  2. 在旧存储库中,使用 git mv 命令将文件移动到你想要的目录结构中。然后,类似于你已经在做的那样,将其合并到新存储库中。

英文:

The quickest way to do this is to copy all the files from the old repo into the new repo and make one very large commit.

If you insist on maintain histories, I can think of two ways:

  1. Start a clean repo and fetch the old repo like you are already doing. Then git mv the files into the new subfolder structure that you want. Rinse and repeat for each of the projects you want to add to the new repo. Then finally add the scafolding for your monorepo.

  2. In the old repo, git mv the files to the directory structure you want. Then merge into the new repo similar to what you are already doing.

答案2

得分: 1

抱歉,我不能关闭问题,因为已经有答案。我发现git-subtree是最好的解决方案。我只需要执行

git subtree add -P packages/existing-1 git@... master

其中git@...是远程URL。请参见https://stackoverflow.com/a/32684526/4125622

英文:

Sorry, I can't close the question since it is answered already. I found git-subtree to be the best solution. I just need to do

git subtree add -P packages/existing-1 git@... master

where git@... is the remote URL. See https://stackoverflow.com/a/32684526/4125622

huangapple
  • 本文由 发表于 2023年4月17日 13:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定