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

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: 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?

  • Shared Library in repository with visual studio
  • merge branch with subtree to develop
  • What are git subtree push assertions?
  • Subtree-split with empty-ident commit; or, deleting a commit without rebase: an unusual use-case
  • git subtree pull says that the working tree has modifications, but git status says it doesn't. What gives?
  • git: subtree merge from subtree and pulling updates
  • How to change the source of a Git subtree merge
  • can't push/pull subtree using sourcetree
  • 2 Solutions collect form web for “Sub-directory into independent repository and later merge back into main repository”

    One way to solve this issue (which may fill particular usecase) is described in this post:

    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.

    One-liner which solves this:

     git merge -s subtree directory_a_branch master
    Git Baby is a git and github fan, let's start git clone.