threads / discuss / 9133

executing git from a non git directory

Subject: executing git from a non git directory

## tl;dr

4 messages between Jul 22, 2007 and Jul 22, 2007.

replies: 3people: 3as markdown or json

Patrick Aljord· Jul 22, 2007, 06:55 UTC · lore
Hey all,
I have my git directory that is in /path/to/my_git_dir
I would like to check it from another direcory, I tried the following command:
$ git --git-dir=/path/to/my_git_dir status
and
$ GIT_DIR='/path/to/my_git_dir' && git status

but in both cases I get this error: fatal: Not a git repository: '/path/to/my_git_dir'

yet when I do "$ cd /path/to/my_git_dir && git status" I do get the results.

Any idea what I'm doing wrong or if there's a way to check a git dir from a non git dir?

thanx in advance
Pat
Shawn O. Pearce· Jul 22, 2007, 07:07 UTC · re: Patrick Aljord · lore

Re: executing git from a non git directory

Patrick Aljord <patcito@gmail.com> wrote:
Show 8 quoted lines
> I would like to check it from another direcory, I tried the following 
> command:
> 
> $ git --git-dir=/path/to/my_git_dir status
> 
> and
> 
> $ GIT_DIR='/path/to/my_git_dir' && git status

These are actually the same two commands. The --git-dir option just means "set GIT_DIR before calling the real command".

However, there are two problems here...
 
> but in both cases I get this error:
> fatal: Not a git repository: '/path/to/my_git_dir'

Right. That's problem number 1. /path/to/my_git_dir is probably not actually your Git repository. The git repository is actually in ".git", so you really need to use:

  $ git --git-dir=/path/to/my_git_dir/.git status

However, problem number 2 is that status requires a working directory. Setting GIT_DIR explicitly like this tells git that you don't have a working directory present. So status won't work.

> yet when I do "$ cd /path/to/my_git_dir && git status" I do get the results.

That's what you have to do if you want to run git-status. Or use a subshell as that won't change your current working directory:

  $ (cd /path/to/my_git_dir && git status)
-- 
Shawn.
Jeff King· Jul 22, 2007, 07:11 UTC · re: Patrick Aljord · lore

Re: executing git from a non git directory

On Sun, Jul 22, 2007 at 01:55:33AM -0500, Patrick Aljord wrote:
Show 8 quoted lines
> $ git --git-dir=/path/to/my_git_dir status
>
> and
>
> $ GIT_DIR='/path/to/my_git_dir' && git status
>
> but in both cases I get this error:
> fatal: Not a git repository: '/path/to/my_git_dir'

A git dir is the location where all of the repository information is stored; it is called '.git' for a directory with a working tree. Is /path/to/my_git_dir really a git dir, or is it a working directory that has a '.git' inside it?

If the latter, then you need to specify the .git directory. However, running 'git-status' won't make any sense then, since you won't be in the correct working tree (so of course it will look like files have been removed).

You could do something like "git --dir /path/to/my_git_dir/.git branch" and it would at least produce sensible results.

> yet when I do "$ cd /path/to/my_git_dir && git status" I do get the results.

Right. That's the way you have to do it in the case of 'status', since you need your current working directory to be in the right place.

If you are running a very recent git, you can now try:
  git --work-tree=/path/to/my_git_dir --git-dir=/path/to/my_git_dir/.git status
which is what you actually want.
-Peff
Patrick Aljord· Jul 22, 2007, 19:02 UTC · re: Jeff King · lore

Re: executing git from a non git directory

ok thanx to both of you Jeff and Shawn for the info.
On 7/22/07, Jeff King <peff@peff.net> wrote:
>   git --work-tree=/path/to/my_git_dir --git-dir=/path/to/my_git_dir/.git status
>
> which is what you actually want.
ok, I'm going to try that, thanx.
Pat

← back to recent threads