Last Updated: February 25, 2016
·
428
· vladimirmikhailov

Remote private gem/repository - git subtree

Into

I believe, you came here due your already have decided why you would like to make a separate repository. And I won't waste your time with stories or pros / cons how cool is could be to store and share a part of an app in a separate place.


Problem:

You have a link to a private repo in you Gemfile and all is working fine until code hasn't delivered to remote servers (CI, Heroku, VPS, Whatever)

gem 'private', git: 'https://link_to_private'

For sure, remote server won't be able to download this gem due privacy and the deploying/testing process will stop.


Solutions:

Actually, the problem doesn't claim to be newest and there are several well known solutions:

  • Use services like Gemfury (can store and share you gem by special generated url with token)
  • Add a separate user into your repository with read-only access and use his credentials in url (you could past those credentials through ENV variables)
gem 'private', github: 'https://user:password@link_to_private

But, I have found myself more comfortable with git subtree merges:

The idea of the subtree merge is that you have two projects, and one of the projects maps to a subdirectory of the other one and vice versa. When you specify a subtree merge, Git is smart enough to figure out that one is a subtree of the other and merge appropriately — it’s pretty amazing.

How to add a new repository as a subtree

git remote add -f private git@host:private/private.git

git merge -s ours --no-commit private/master

# --prefix - the path for external project
git read-tree --prefix=vendor/gems/private -u private/master

git commit -m "Subtree merged in private"

And for pulling last changes from external repository:

git pull -s subtree private master
git commit -m "A new feature from private gem has been added"