# GIT_WORK_TREE and GIT_DIR with git clone

3 messages from 2014-01-20 to 2014-01-21. Participants: Cosmin Apreutesei, Junio C Hamano.
Thread: https://gitlist.dev/t/35693

## Cosmin Apreutesei, 2014-01-20 15:59

Subject: GIT_WORK_TREE and GIT_DIR with git clone
Message-ID: <CAKJdRamFDDWRZDHBCe7GrXg0OWhLfBPq_ofsvowaW9d8VOGTMA@mail.gmail.com>
URL: https://gitlist.dev/e/CAKJdRamFDDWRZDHBCe7GrXg0OWhLfBPq_ofsvowaW9d8VOGTMA%40mail.gmail.com

```
Hi,

I would like to have git clone respect GIT_DIR (and --git-dir) so I
can clone multiple repos into the same working directory (I know there
was another request for this[1]).

The pattern here is big projects that are modularized into multiple
git repositories, but supposed to be overlaid over a common directory
structure.

I would like to be able to tell my users that they can simply do:

git clone --git-dir=_/git/module1/.git module1-url
git clone --git-dir=_/git/module2/.git module2-url

which will overlay the files from both modules into the current
directory, which from git's perspective is the work-tree for both
modules.


[1] http://article.gmane.org/gmane.comp.version-control.git/170073/match=git_work_tree+git_dir+git+clone

```

## Junio C Hamano, 2014-01-21 20:23

Subject: Re: GIT_WORK_TREE and GIT_DIR with git clone
Message-ID: <xmqq4n4xulrm.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq4n4xulrm.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CAKJdRamFDDWRZDHBCe7GrXg0OWhLfBPq_ofsvowaW9d8VOGTMA@mail.gmail.com>

```
Cosmin Apreutesei <cosmin.apreutesei@gmail.com> writes:

> I would like to be able to tell my users that they can simply do:
>
> git clone --git-dir=_/git/module1/.git module1-url
> git clone --git-dir=_/git/module2/.git module2-url
>
> which will overlay the files from both modules into the current
> directory, which from git's perspective is the work-tree for both
> modules.

Would there be a sensible semantics in the resulting working tree?
Which repository would a new file in such a combined working tree
belong to?

```

## Cosmin Apreutesei, 2014-01-21 21:37

Subject: Re: GIT_WORK_TREE and GIT_DIR with git clone
Message-ID: <CAKJdRakBWR7ofvcc8uhQ09QwiAJs7uvRNwP3WyuF4ve7FWONgw@mail.gmail.com>
URL: https://gitlist.dev/e/CAKJdRakBWR7ofvcc8uhQ09QwiAJs7uvRNwP3WyuF4ve7FWONgw%40mail.gmail.com
In-Reply-To: <xmqq4n4xulrm.fsf@gitster.dls.corp.google.com>

```
Hi, thanks for answering.

>> I would like to be able to tell my users that they can simply do:
>>
>> git clone --git-dir=_/git/module1/.git module1-url
>> git clone --git-dir=_/git/module2/.git module2-url
>>
>> which will overlay the files from both modules into the current
>> directory, which from git's perspective is the work-tree for both
>> modules.
>
> Would there be a sensible semantics in the resulting working tree?
> Which repository would a new file in such a combined working tree
> belong to?

The developer would have to decide by way of `git add`.

Ignoring other repos' files would be done by way of local config
option `excludesfile` (.gitignore is out).

To make it easier to work with git this way, a script that creates a
subshell in the context of a repo can be done with something like
`PS1="[$1 $PS1]" GIT_DIR=_git/$1/.git bash -i" -- git would then work
as usual in that subshell for that specific repo, never leaving the
working tree.


I successfully employed the above scheme with luapower[1] packages,
which are all different and mostly unrelated libraries, but which need
to be overlaid over a common directory structure. And that's just an
example. I can think of many projects that are modularized and yet the
modules need to place many files in many places to make a working
system (web frameworks, the linux filesystem, etc.)

Currently, to clone a repo one has to do:

export GIT_DIR=_git/submodule/.git
git init
git config --local core.worktree ../../..
git remote add origin ssh://git@github.com/luapower/submodule.git
git fetch
git branch --track master origin/master
git checkout

That's 6 commands for what could be:

    git clone --git-dir=_git/submodule/.git
ssh://git@github.com/luapower/submodule.git

Or even better:

    git clone --git-dirs=_git ssh://git@github.com/luapower/submodule.git

[1] http://luapower.com

```
