Sub-directory into independent repository and later merge back into main repository

戏子无情 提交于 2019-12-22 09:56:17

问题


I want to create independent repository out of a sub-directory of an existing git repository and later be capable to merge it back to main repository. Basically I want to separate one sub-directory of a monolithic git repository for specific work and be able to merge commits back into monolithic repo.

What I did try was split a subtree:

% git subtree split -P project_a/directory_a -b directory_a_branch

Then I could clone directory_a_branch into separate repository and start working on it:

% git clone -b directory_a_branch --single-branch file:///path/to/main_repo

Later I could push changes back to main repository (under directory_a_branch) -- everything is normal and by the book right now.

Problems start with my strategy in merging directory_a_branch, which fail since I clearly fail to fully understand what I'm doing.

I read "Subtree Merging" from Pro Git book: https://git-scm.com/book/en/v1/Git-Tools-Subtree-Merging and tried:

% git read-tree --prefix=project_a/directory_a -u directory_a_branch
error: Entry 'project_a/directory_a/README' overlaps with 'project_a/directory_a/README'.  Cannot bind.

Similar issue is discussed here: git: merging a subtree from one branch to another, but lacks sufficient answer for me.

Creating subtree branch is not my intention and this problem may be solved other ways. Maybe with submodules?

My intention is that one repository is like sub-repository to another and merging work back to main repository would be as easy as possible, since both repositories share the same files.

Is it possible to solve this task with Git without ugly hacks?


回答1:


One way to solve this issue (which may fill particular usecase) is described in this post: https://lostechies.com/johnteague/2014/04/04/using-git-subtrees-to-split-a-repository/

Basically you remove project_a/directory_a from master (& commit) and then subtree add --prefix from (remote) branch like this:

git subtree add --prefix=project_a/directory_a remote branch

But this is not 100%, since there is better way to solve this.




回答2:


One-liner which solves this:

 git merge -s subtree directory_a_branch master


来源:https://stackoverflow.com/questions/30704352/sub-directory-into-independent-repository-and-later-merge-back-into-main-reposit

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