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

345 views Asked by At

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?

2

There are 2 answers

0
ilvez On BEST ANSWER

One-liner which solves this:

 git merge -s subtree directory_a_branch master
0
ilvez On

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.