# GIT_WORK_TREE and GIT_DIR with git clone

3 messages from 2011-03-26 to 2011-03-26. Participants: Kacper Kornet, Junio C Hamano.
Thread: https://gitlist.dev/t/26872

## Kacper Kornet, 2011-03-26 12:58

Subject: GIT_WORK_TREE and GIT_DIR with git clone
Message-ID: <20110326125818.GA2621@camk.edu.pl>
URL: https://gitlist.dev/e/20110326125818.GA2621%40camk.edu.pl

```
Is there a reason why git clone does not respect GIT_DIR
variable. I have found it confusing that:

GIT_WORK_TREE=repo GIT_DIR=repo/.git git clone <path>/repo.git

is not equivalent to

git clone <path>/repo.git

-- 
  Kacper

```

## Junio C Hamano, 2011-03-26 19:40

Subject: Re: GIT_WORK_TREE and GIT_DIR with git clone
Message-ID: <7vtyep3c1h.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vtyep3c1h.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20110326125818.GA2621@camk.edu.pl>

```
Kacper Kornet <kornet@camk.edu.pl> writes:

> Is there a reason why git clone does not respect GIT_DIR
> variable.

Are you sure it does "not respect GIT_DIR"?  When you are running clone
locally, like that:

> GIT_WORK_TREE=repo GIT_DIR=repo/.git git clone <path>/repo.git

you have _two_ repositories that the command operates on on your system,
the one that you are cloning _from_, and the new one that you are
creating.

Which repository does your GIT_DIR talk about?

```

## Kacper Kornet, 2011-03-26 23:37

Subject: Re: GIT_WORK_TREE and GIT_DIR with git clone
Message-ID: <20110326233728.GA9445@camk.edu.pl>
URL: https://gitlist.dev/e/20110326233728.GA9445%40camk.edu.pl
In-Reply-To: <7vtyep3c1h.fsf@alter.siamese.dyndns.org>

```
On Sat, Mar 26, 2011 at 12:40:58PM -0700, Junio C Hamano wrote:
> Kacper Kornet <kornet@camk.edu.pl> writes:

> > Is there a reason why git clone does not respect GIT_DIR
> > variable.

> Are you sure it does "not respect GIT_DIR"?  When you are running clone
> locally, like that:

> > GIT_WORK_TREE=repo GIT_DIR=repo/.git git clone <path>/repo.git

> you have _two_ repositories that the command operates on on your system,
> the one that you are cloning _from_, and the new one that you are
> creating.

> Which repository does your GIT_DIR talk about?

I would expect that GIT_DIR always talks the new one. For example after:

GIT_WORK_TREE=dir GIT_DIR=dir/.git git clone git://git.kernel.org/pub/scm/gitk/gitk

I would expect to have repo in dir/.git. What happens is that in dir
there is a working tree and in local directory gitk there is a bare
repo. The following patch should fix the issue:

diff --git a/builtin/clone.c b/builtin/clone.c
index c6e10bb..082ee53 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -442,11 +442,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 			die("working tree '%s' already exists.", work_tree);
 	}
 
-	if (option_bare || work_tree)
-		git_dir = xstrdup(dir);
-	else {
+	git_dir = getenv("GIT_DIR");
+	if (option_bare || work_tree) {
+		if (!git_dir)
+			git_dir = xstrdup(dir);
+	} else {
 		work_tree = dir;
-		git_dir = xstrdup(mkpath("%s/.git", dir));
+		if (!git_dir)
+			git_dir = xstrdup(mkpath("%s/.git", dir));
 	}
 
 	if (!option_bare) {





-- 
  Kacper 

```
