# bug: setting GIT_DIR to $(git rev-parse --git-dir) changes behavior

2 messages from 2021-02-25 to 2021-02-25. Participants: Martin Nicolay, Junio C Hamano.
Thread: https://gitlist.dev/t/55210

## Martin Nicolay, 2021-02-25 14:54

Subject: bug: setting GIT_DIR to $(git rev-parse --git-dir) changes behavior
Message-ID: <alpine.LSU.2.20.2102251549330.20742@cpza.bfz-tzou.qr>
URL: https://gitlist.dev/e/alpine.LSU.2.20.2102251549330.20742%40cpza.bfz-tzou.qr

```
Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue)

$ env | grep GIT
$ git --version
git version 2.30.1
$ git init t
Initialized empty Git repository in /tmp/t/.git/
$ mkdir t/foo
$ cd t/foo
$ git rev-parse --show-toplevel
/tmp/t
$ GIT_DIR=$(git rev-parse --git-dir) git rev-parse --show-toplevel
/tmp/t/foo
$ git rev-parse --git-dir
/tmp/t/.git

What did you expect to happen? (Expected behavior)

"git rev-parse --show-toplevel" should be the same in both invocation.

What happened instead? (Actual behavior)

"git rev-parse --show-toplevel" shows the current working directory if GIT_DIR is set.

What's different between what you expected and what actually happened?

Setting GIT_DIR to $(git rev-parse --git-dir) should not change anything.

Anything else you want to add:

Please review the rest of the bug report below.
You can delete any lines you don't wish to share.


[System Info]
git version:
git version 2.30.1
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 5.3.18-22-default #1 SMP Wed Jun 3 12:16:43 UTC 2020 (720aeba) x86_64
compiler info: gnuc: 9.1
libc info: glibc: 2.26
$SHELL (typically, interactive shell): /opt/local/bin/bash


[Enabled Hooks]

```

## Junio C Hamano, 2021-02-25 18:29

Subject: Re: bug: setting GIT_DIR to $(git rev-parse --git-dir) changes behavior
Message-ID: <xmqqtuq0uhkg.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqtuq0uhkg.fsf%40gitster.g
In-Reply-To: <alpine.LSU.2.20.2102251549330.20742@cpza.bfz-tzou.qr>

```
Martin Nicolay <m.nicolay@osm-ag.de> writes:

> What did you do before the bug happened? (Steps to reproduce your issue)

Thanks for concise and clear descriptions for us to learn everything
you did.  I wish all the reports were written like this one.

> $ env | grep GIT
> $ git --version
> git version 2.30.1

Showed there is no funny environment variable involved and the
version.  Good.

> $ git init t
> Initialized empty Git repository in /tmp/t/.git/
> $ mkdir t/foo
> $ cd t/foo
> $ git rev-parse --show-toplevel
> /tmp/t

The root of the working tree is at /tmp/t/, the repository at
/tmp/t/.git/, and you are in the foo/ subdirectory.  When you ask
"where is the top level of the working tree in this state, because
you do not have GIT_DIR or GIT_WORK_TREE environment variables, you
are asking Git to "discover" both the .git/ directory and the top of
the working tree, by starting at the current directory,
i.e. /tmp/t/foo, which is where you are.

First Git looks to see /tmp/t/foo/.git/ exists and is a repository;
it is not.  So it goes one level up and does so for /tmp/t/.git/ and
it finds that it is the repository.  The directory in which the
repository was discovered is the top of the working tree, hence you
get /tmp/t back.

> $ GIT_DIR=$(git rev-parse --git-dir) git rev-parse --show-toplevel
> /tmp/t/foo

When you give the GIT_DIR environment variable, you are telling Git
not to perform the repository discovery, AND also you are telling
Git that you are at the top-level of the working tree.  So this is
quite expected (it is a feature that is used by folks who want to
have their repository data in a distant and unrelated directory from
their working tree).

In other words, this is 100% expected behaviour.

If you want to also tell Git where the top-level of your working
tree is, you can export GIT_WORK_TREE at the same time.

    Side note: the latter environment variable was invented for this
    exact reason.  Back when only GIT_DIR existed as a way to tell
    Git where the distant repository is, those who wanted to use the
    "split" layout had to stay at the top-level of their working
    tree, and they got tired of not being able to work from a
    subdirectory.  GIT_WORK_TREE was introduced to tell where the
    top-level is separately from GIT_DIR when GIT_DIR environment
    variable is in use.

> $ git rev-parse --git-dir
> /tmp/t/.git

```
