How do you merge two Git repositories when one is

2019-09-15 17:23发布

问题:

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.