gitgit-submodulesmultiple-repositories

How do I work with a git repository within another repository?


I have a Git media repository where I'm keeping all of my JavaScript and CSS master files and scripts that I'll use on various projects.

If I create a new project that's in its own Git repository, how do I use JavaScript files from my media repository in my new project in a way that makes it so I don't have to update both copies of the script when I make changes?


Solution

  • The key is git submodules.

    Start reading the Submodules chapter of the Git Community Book or of the Users Manual

    Say you have repository PROJECT1, PROJECT2, and MEDIA...

    cd /path/to/PROJECT1
    git submodule add ssh://path.to.repo/MEDIA
    git commit -m "Added Media submodule"
    

    Repeat on the other repo...

    Now, the cool thing is, that any time you commit changes to MEDIA, you can do this:

    cd /path/to/PROJECT2/MEDIA
    git pull
    cd ..
    git add MEDIA
    git commit -m "Upgraded media to version XYZ"
    

    This just recorded the fact that the MEDIA submodule WITHIN PROJECT2 is now at version XYZ.

    It gives you 100% control over what version of MEDIA each project uses. git submodules are great, but you need to experiment and learn about them.

    With great power comes the great chance to get bitten in the rump.