vastlysuperiorman
vastlysuperiorman

Reputation: 1784

git update new repo from subtree

I have a tricky situation. There are two repositories involved, which we'll call monolith and module. module was created in Github based on a template, and therefore already contained commits. I split a subdirectory out of the monolith and merged with the new repo as follows:

monolith> git subtree split --prefix module --branch module-only
monolith> git push ../module module-only:module-only
monolith> cd ../module
module> git merge module-only --allow-unrelated-histories # Remember, the template commits?

I have since done some work on module, adding a few commits. I've learned that work also occurred on monolith in the module subdirectory. I'm being asked to update the module repo with the latest changes from monolith.

How do I update the subtree?

I'm answering my own question because I figured it out before I finished writing this. If you have a better answer, I'll happily give you the green check mark.

Upvotes: 1

Views: 134

Answers (1)

vastlysuperiorman
vastlysuperiorman

Reputation: 1784

This might not be the best solution, but this is what I ended up doing:

monolith> git branch -D module-only # I couldn't seem to update the subtree...
monolith> git subtree split --prefix module --branch module-only # So just recreate it

# These steps are the same as before
monolith> git push ../module module-only:module-only
monolith> cd ../module
module> git merge module-only

Note that the git options --prefix and --branch can be changed to short options -P and -b respectively. I used the long options for clarity in this answer. I'm adding this note because I keep coming back to my own answer to remember the flags... lol.


Things that didn't work

My attempts to update the subtree branch by various combinations of git subtree pull failed.

I tried updating directly from the module-only branch:

module> git pull ../monolith module-only:master

But this was rejected for being non-fast forward. So I tried to merge changes into the subtree first:

monolith> git checkout module-only
monolith:subtree> git pull ../module master:module-only

This was also rejected for being non-fast forward. Both ahead and behind I guess?

My rebase attempts were a nightmare.

Upvotes: 1

Related Questions