2

I've got a Python library of my own in a Git repository. I'm starting a new Python application in a new Git repository, which requires the use of a subdirectory of that library, which I will surely modify.

How do I import a subfolder of the library's repo to the application repo, in a way that the modifications made during the application development can be taken back to the library's repo at the end. During the application development, the library itself will also evolve on its own.

I've seen git subtree here as an option to include a subfolder of a repo into another one, but I can't foresee how to take changes back to the library repo and merge them with the commits the library itself may have received meanwhile.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
  • `git subtree push`, `git subtree pull`. Docs: https://github.com/git/git/blob/master/contrib/subtree/git-subtree.txt, https://git-memo.readthedocs.io/en/latest/subtree.html – phd Jan 29 '20 at 11:52

1 Answers1

1

Ok, I've found the way to send back the results, these are the commands in case they help someone in the future.

From this:

Before adding the subtree to my-app-repo, split a subtree from my-company-library-repo:

# In my-company-library-repo
git subtree split -P src/app/providers/... -b feature-new feature

This will create a new history with the contents of src/app/providers/... at the root of the repo, starting at the feature branch, and create the branch feature-new at the end of this history.

Then add that new branch as a subtree to my-app-repo:

# In my-app-repo
git subtree add -P <destination-dir/feature> --squash <my-company-library-repo> feature-new

I complete with the commands to update my-company-library-repo with changes from my-app-repo

git subtree push -P <destination-dir/feature> <my-company-library-repo> feature-new

This will split the changes from <destination-dir/feature>, push them to the branch feature-new of my-company-library-repo. Then the commits can be merged/rebased into master of my-company-library-repo

  • Right on! The [subtree "split"](https://github.com/git/git/blob/master/contrib/subtree/git-subtree.txt#L101) command is exactly right. – Anthony Mastrean Feb 21 '20 at 20:34