0

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.

culebrón
  • 34,265
  • 20
  • 72
  • 110

2 Answers2

2

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.

Laurens Holst
  • 20,156
  • 2
  • 29
  • 33
  • Great! Will check out `relink` too. – culebrón Nov 16 '11 at 13:15
  • This isn't the same as `share` as the clone is now an independent repository. I doubt that disk space is the issue here, more that the OP wants the two working copies to share a repository. – Paul S Nov 16 '11 at 13:18
  • @PaulS: the question says that it is because the subrepo is so large. Culebrón’s ‘this is exactly what I need’ comment is probably in response to a comment I made and then deleted to flesh it out into this answer. – Laurens Holst Nov 16 '11 at 16:28
1

With git-submodules that would be (and with your example) in path another:

git init # (to make another a git-repo)
git submodule add ../orig orig # to make orig a submodule of another
git commit # to commit the addition of the submodule

. Have you tried git submodule --help?

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • I read it and can't understand: will the (portions of) .git folder in `another/orig` be shared or not? I just want to save space and pull traffic. – culebrón Nov 16 '11 at 11:26
  • `git submodule add ../orig orig` => `remote (origin) does not have a url defined in .git/config`. I do `git status`, says "nothing to commit". – culebrón Nov 16 '11 at 11:31
  • Submodules are not the same (they are called subrepositories in hg). The share extension shares a single repository between multiple working copies. – Laurens Holst Nov 16 '11 at 11:50