Search code examples
gitmercurialgit-submodulesmercurial-subrepos

How to share a .git folder among several projects?


I want to work with Git repositories or sub-repositories like in Mercurial Share extension.

So, here's what I have:

mkdir orig
cd orig
echo "osthuaosteh" > abc
git init --shared
git add abc
git commit -m 'init'
cd ..
mkdir another

How can I initialize a repo in another so that it shares the repository with orig?

I need this for a big library that I want to include as a sub-repository. The repo weighs hundreds of megs, so I want to reuse some of the folders.

Update: I want to be able to have different revisions in different working folders.


Solution

  • What I would ask you is: do you really need to share the repository?

    Like Mercurial, git creates hard-links between repositories when you make a local clone, resulting in only little extra disk space consumption. E.g.:

    git clone http://example.org/repo repo
    git clone repo repo-copy1
    git clone repo repo-copy2
    

    Most files in the repo-copy1 and repo-copy2 repositories will hard-link to repo, and will not consume extra disk space. Only the files in the working copy are actual copies.

    You can confirm this behaviour like this:

    $ df -l
    Filesystem    512-blocks      Used Available Capacity  Mounted on
    /dev/disk0s2   976101344 217966872 757622472    23%    /
    $ git clone --no-checkout repo repo-copy
    Cloning into repo-copy...
    done.
    $ du -cs repo-copy/.git
    63528   .
    63528   total
    $ df -l
    Filesystem    512-blocks      Used Available Capacity  Mounted on
    /dev/disk0s2   976101344 217967536 757621808    23%    /
    

    As you can see, after cloning a 65880-block repository (of 512 bytes each), the block count on the file system went down by only 664 blocks.

    If you clone a (sub)repository from a remote server you may have to manually create the hard links to other local clones; for Mercurial you would use the relink extension for that; the git equivalent also seems to be called that.