How do you merge two Git repositories when one is a subdirectory of the other without losing commit history?

拟墨画扇 提交于 2019-12-25 08:59:38

问题


Current folder structure

  • /root: server code exists here
  • /root/client: client code exists here (a subdirectory within the server's directory).

Current git tracking

  • 1 repo tracking Server code in /root that gitignores the entire client directory
  • 1 repo tracking Client code only within /root/client.

The Problem

I am trying to merge these two repos while keeping one as the subdirectory of the other, combining the historical commits of both (not losing any).

Findings So Far

I have found a lot of situations resolved on the Internet in which two repos have either 1) the same files or 2) will eventually be merged together at the same folder level, but have not found much on merging two repos if one is a subdirectory within another.

Any help would be greatly appreciated. Thank you!


回答1:


This steps will merge both master branches:

  1. Move the client project folder to a folder to outside the server folder (for example, tmp_client)
  2. Inside the new client project folder (tmp_client) create a new folder called client.
  3. Move all the content from the tmp_client to the new client folder
  4. Add and commit the changes:

    git add -A . to add the changes

    git commit -m "Move content to client folder"

  5. Add a new remote branch for the server:

    git remote add server git_server_repo_url

  6. Pull changes from the server master branch

    git pull server master This will merge the changes

  7. Push changes to the server git repository

    git push server master

PD: If you want to be sure all is ok in the server branch, you can create a new branch instead of pushing it to the working bramch of the server and then merge the new branch.



来源:https://stackoverflow.com/questions/44658948/how-do-you-merge-two-git-repositories-when-one-is-a-subdirectory-of-the-other-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!