# Bug? clone ignores --git-dir

4 messages from 2019-11-26 to 2019-11-27. Participants: Jack Bates, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/52347

## Jack Bates, 2019-11-26 18:07

Subject: Bug? clone ignores --git-dir
Message-ID: <7d28416e-c927-4cd3-bac2-d8bfd02ce949@nottheoilrig.com>
URL: https://gitlist.dev/e/7d28416e-c927-4cd3-bac2-d8bfd02ce949%40nottheoilrig.com

```
If you `git rebase --exec 'gulp runtests-parallel --runners user'` this 
project, the tests perform Git operations on various subprojects [1], 
but because Git exports `GIT_DIR` and `GIT_WORK_TREE`, they end up 
operating on the parent project instead.

Adding `--git-dir` and `--work-tree` overrides `GIT_DIR` and 
`GIT_WORK_TREE` [2], forcing the tests to operate on the correct 
repositories, however `clone` ignores `--git-dir`:

> git init repository
> git --git-dir git-dir clone repository directory
I'd expect Git to create a directory `git-dir` and put the index, etc. 
there, but instead it creates `directory/.git`, as usual. I'm not sure 
if this is a bug or the expected behavior?

[1] 
https://github.com/microsoft/TypeScript/blob/master/src/testRunner/externalCompileRunner.ts#L58
[2] https://github.com/microsoft/TypeScript/pull/35362

```

## Jeff King, 2019-11-27 11:51

Subject: Re: Bug? clone ignores --git-dir
Message-ID: <20191127115124.GE22221@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20191127115124.GE22221%40sigill.intra.peff.net
In-Reply-To: <7d28416e-c927-4cd3-bac2-d8bfd02ce949@nottheoilrig.com>

```
On Tue, Nov 26, 2019 at 11:07:27AM -0700, Jack Bates wrote:

> If you `git rebase --exec 'gulp runtests-parallel --runners user'` this
> project, the tests perform Git operations on various subprojects [1], but
> because Git exports `GIT_DIR` and `GIT_WORK_TREE`, they end up operating on
> the parent project instead.
> 
> Adding `--git-dir` and `--work-tree` overrides `GIT_DIR` and `GIT_WORK_TREE`
> [2], forcing the tests to operate on the correct repositories, however
> `clone` ignores `--git-dir`:
> 
> > git init repository
> > git --git-dir git-dir clone repository directory
> I'd expect Git to create a directory `git-dir` and put the index, etc.
> there, but instead it creates `directory/.git`, as usual. I'm not sure if
> this is a bug or the expected behavior?

This is the expected behavior. "clone" and "init" are special in that
they are creating _new_ repositories, so they explicitly ignore the
any notion of the current repository.

I'm not quite sure what you're trying to do. I'd think:

  git clone --bare repository git-dir

would do what you want, but then I'm not sure why you're specifying
"directory" in the first place, or what you expect to happen. Are you
looking for clone's --separate-git-dir option, perhaps?

-Peff

```

## Junio C Hamano, 2019-11-27 12:33

Subject: Re: Bug? clone ignores --git-dir
Message-ID: <xmqqh82pv6qi.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqh82pv6qi.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <7d28416e-c927-4cd3-bac2-d8bfd02ce949@nottheoilrig.com>

```
Jack Bates <bk874k@nottheoilrig.com> writes:

> ... repositories, however `clone` ignores `--git-dir`:

An intentional design decision (and it may be an unfortunate one by
now [*1*]) was to have the GIT_DIR (hence --git-dir) talk about the
*source side* of the clone, not the destination, e.g.

	cd /home/me/working/tree
	export GIT_DIR=/home/me/repo/sitory
	export GIT_WORK_TREE=$(pwd)

	# the above told Git that /home/me/working/tree has a
	# checkout but its repository body is not anywhere in its
	# subdirectory -- it is /home/me/repo/sitory.

	# now we clone the repository we are currently working on
	# without GIT_DIR applying to the source side, it won't
	# work.
	git clone . /else/where

And we cannot allow /home/me/repo/sitory (i.e. GIT_DIR for the
source repository) become the GIT_DIR for the resulting /else/where
repository (you'd be cloning to yourself).


[Footnote]

*1* with use of "git worktree" gaining traction, perhaps the need to
    create local clone out of the shared/same GIT_DIR may have
    diminished.


```

## Jack Bates, 2019-11-27 20:19

Subject: Re: Bug? clone ignores --git-dir
Message-ID: <91fa0f24-fd11-a0ff-c258-c0f480910ade@nottheoilrig.com>
URL: https://gitlist.dev/e/91fa0f24-fd11-a0ff-c258-c0f480910ade%40nottheoilrig.com
In-Reply-To: <xmqqh82pv6qi.fsf@gitster-ct.c.googlers.com>

```
On 2019-11-27 5:33 a.m., Junio C Hamano wrote:
> An intentional design decision (and it may be an unfortunate one by
> now [*1*]) was to have the GIT_DIR (hence --git-dir) talk about the
> *source side* of the clone, not the destination, e.g.

Ah ha, the following does what I want, regardless of 
`GIT_DIR`/`GIT_WORK_TREE`:

> git init repository
> GIT_DIR=bogus GIT_WORK_TREE=bogus git --work-tree directory clone repository directory/.git

Thank you!

```
