简体   繁体   English

将两个 git 仓库合并为一个更高级别的仓库

[英]Combine two git repositories into one higher level repository

My project consists of two folders, each with its own repository:我的项目包含两个文件夹,每个文件夹都有自己的存储库:

project/
|--frontend/
|--|--.git
|--backend/
|--|--.git

So far I have worked on them locally.到目前为止,我已经在本地处理过它们。 Now I want to create one global repository for my project to push it into remote while saving both histories in it.现在我想为我的项目创建一个全局存储库,以将其推送到远程,同时在其中保存两个历史记录。 What is the right way to do it?正确的做法是什么?

project/
|--.git - has entire history of project (both folders)
|--frontend/
|--|--.git - can be deleted if method requires it
|--backend/
|--|--.git - can be deleted if method requires it

I suspect you can do this by moving the path of each and merging one into the other我怀疑你可以通过移动每个路径并将一个路径合并到另一个路径来做到这一点

This assumes it's desirable to add the frontend into the backend repository, though this might not be the case for you - repeat the steps for each if creating a new repository这假定将前端添加到后端存储库是可取的,尽管这对您来说可能不是这种情况 - 如果创建新存储库,请对每个步骤重复这些步骤

WARNING I HAVE NOT TESTED THIS YET警告我还没有测试过这个

Make a backup of your repositories备份您的存储库

cp -r project project.bak  # FIXME targz

Check out a custom branch (in backend)检查自定义分支(在后端)

(frontend)% git checkout -b custom-move-frontend
(backend)% git checkout -b custom-move-backend

update the remote so they are part of the same repository更新遥控器,使它们属于同一个存储库

get the remote from each从每个人那里得到遥控器

(frontend)% git remote -v  # display remote URL
(backend)% git remote -v

update the backend's remote更新后端的远程

(backend)% git remote set-url origin "$FRONTEND_REMOTE"

Move the contents of each into its future name将每个的内容移动到其未来的名称中

(frontend)% git mv . ./frontend
(frontend)% git add .
(frontend)% git commit -m "MY UPDATE TEXT FRONTEND"
(frontend)% git push  # original URL

(backend)% git mv . ./backend
(backend)% git add .
(backend)% git commit -m "MY UPDATE TEXT BACKEND"
(backend)% git push  # branch is pushed to frontend's URL

join the unrelated histories加入不相关的历史

The branches do not share a common history分支机构没有共同的历史

Git refusing to merge unrelated histories on rebase Git 拒绝在 rebase 上合并不相关的历史记录

(frontend)% git pull
(frontend)% git checkout -b custom-merge-merge  # from custom-move-frontend
(frontend)% git merge --allow-unrelated-histories origin/custom-move-backend

the repositories are now merged存储库现在已合并


extras临时演员

I highly recommend adding a file with notes about what happened and at what specific commits我强烈建议添加一个文件,其中包含有关发生的事情以及特定提交的注释

prefixing your commits with a unique string may help you find them later为您的提交加上一个唯一的字符串前缀可能会帮助您稍后找到它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM