Search code examples
gitsourcetree

Can I have one sub repository inside another, and update the sub repo globally?


Let's say that I have a tool that it's used by every program I made, and sometimes I update this tool and I want it to be updated on every other repo that uses it. Is there a way to achieve this?

I'm not asking to have ONE repository inside another, I'm asking about having the same repository inside several other's repos, and being able to update them in a way different to coping/pasting the original folder's repo.

If posible by using Git + Sourcetree

Thanks!


Solution

  • Submodule is a good way to achieve this, provided you set that submodule to follow its own master branch

    cd /a/repo1
    git submodule add -b master -- /url/to/repo/tool tool
    

    (repeat for all repos needing the tool repo)

    Each time you update that tool repo, all you need to do within a repo is:

    cd /a/repo1
    git submodule update --remote
    git add .
    git commit -m "update tool"
    git push
    

    Remember, each time you change a submodule, you need to record its gitlink (special entry in the index of the parent repo) to keep track of that change.