Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include part of another git repository using git subtree and merge updates in both directions

I have two git repositories show below. The first is structured like a typical python project.

foo_repo/
    .git/
    setup.py
    foo/
         __init__.py
         some_code.py
    tests/

bar/
    .git/

I would like to include the foo_repo/foo/ directory in bar/ as a subtree and I want to be able to merge updates to foo_repo/foo/some_code.py both from the foo_repo repository to bar and vice versa.

The initial setup isn't too bad. From the foo/ directory I use:

git subtree --prefix=foo/ split -b export

Then I have a new branch in foo_repo with only the contents of the foo_repo/foo/ directory. To bring this into bar, I just go to the bar/ directory and:

git subtree --prefix=foo/ add ../foo_repo/.git export

Now that I'm all set up, I'd like to do some code development and keep foo/ up to date in both repos. Pushing from bar I think I have figured out. From bar/ directory:

touch foo/more_code.py
git add foo/more_code.py
git commit -m "more code"
git subtree --prefix=foo/ push ../foo_repo/.git export

Then from the foo_repo/ directory:

git checkout master
git subtree --prefix=foo/ merge export

Merging the other way is where I'm stuck. From foo_repo/:

git checkout master
touch foo/yet_more_code.py
git add foo/yet_more_code.py
git commit -m "yet more code"
???

Where the ??? is a command that merges the foo/ directory with the export branch. Then from bar/:

git subtree --prefix=foo/ pull ../foo_repo/.git export

So I'm basically looking for the line that goes in the ??? spot, or a different workflow that does the same thing. I've tried repeating git subtree --prefix=foo/ split -b export_foo by that doesn't work.

like image 693
kiyo Avatar asked Sep 20 '13 00:09

kiyo


People also ask

Can we merge two different repositories?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

Can we merge different branches from different repositories in a single branch?

Adaptions. If you already have a local copy of a repository containing the code, you could also use that for the merging process. You just need to make sure you do not mix up local development and merging and maybe you need to handle more remote repositories as you have some more for your development.


1 Answers

If you split again foo/ the new commits created for this subtree will be created on top of the old commits already existing in export, including the merges that are needed for such:

git subtree --prefix=foo/ split

Notice that if using the -b option the branch should not exist previuosly, so you might need to create a new branch or later force it to change.

like image 161
Maic López Sáenz Avatar answered Sep 23 '22 18:09

Maic López Sáenz