I am in directory A
. How do I execute git log
for the git repository in directory B
?

- 24,552
- 19
- 101
- 135

- 4,590
- 3
- 34
- 48
-
1With git 1.8.5 (Q4 2013), you will have another choice, instead of setting `--git-dir`: see [my answer below](http://stackoverflow.com/a/20115753/6309) – VonC Nov 21 '13 at 08:24
-
Possible duplicate of [git pull while not in a git directory](http://stackoverflow.com/questions/5083224/git-pull-while-not-in-a-git-directory) – Code-Apprentice Dec 30 '16 at 21:27
-
1`(cd ../other/dir && git log)` - just enclosed in round brackets, this runs a new subshell so after returning you end up in the original location. – bloody Dec 04 '20 at 12:53
-
Does this answer your question? [How do I execute a Git command without being in the repository?](https://stackoverflow.com/questions/7149984/how-do-i-execute-a-git-command-without-being-in-the-repository) – Top-Master Aug 20 '22 at 07:16
3 Answers
From, man git
:
You can do this with the --git-dir
parameter, before passing any commands.
git --git-dir /foo/bar/.git log
(Specifying the .git
directory is necessary.) From the documentation:
--git-dir=<path>
Set the path to the repository. This can also be controlled by setting the
GIT_DIR
environment variable. It can be an absolute path or relative path to current working directory.
-
31You have to reference the `.git` directory. Try `git --git-dir=/foo/bar/.git log`. – mipadi Sep 22 '10 at 18:29
-
6sorry, my wrong you don't put the = between --git-dir and directory name. so it becomes git --git-dir /foo/bar log – rgngl Sep 23 '10 at 06:39
-
29Warning! This approach is broken for operations other than `log`. Try `git --git-dir /foo/someclone/.git status` - git gets totally confused. I recommend `-C` as noted below. – Air Sep 25 '14 at 22:25
-
I have yet to come across a git help page that includes the string "--git-dir" :'( – thinsoldier Mar 31 '15 at 22:26
-
1@thinsoldier `man git` does on my system (2.3.5). But @VonC answer works much better. – Calimo Apr 17 '15 at 12:25
With git 1.8.5 (Q4 2013), you will have another choice, instead of setting --git-dir
.
If you want to execute git log
in folder B
, type:
git -C B log
Just like "
make -C <directory>
", "git -C <directory> ...
" tells Git to go there before doing anything else.
See commit 44e1e4 by Nazri Ramliy:
It takes more keypresses to invoke git command in a different directory without leaving the current directory:
(cd ~/foo && git status)
git --git-dir=~/foo/.git --work-tree=~/foo status
GIT_DIR=~/foo/.git GIT_WORK_TREE=~/foo git status(cd ../..; git grep foo)
for d in d1 d2 d3; do (cd $d && git svn rebase); done
The methods shown above are acceptable for scripting but are too cumbersome for quick command line invocations.
With this new option, the above can be done with fewer keystrokes:
git -C ~/foo status
git -C ../.. grep foo
for d in d1 d2 d3; do git -C $d svn rebase; done

- 1,262,500
- 529
- 4,410
- 5,250
-
28This is a much more reliable approach and works for commands other than `log`. – Air Sep 25 '14 at 22:25
-
wow, and nothing in the man page! What a shame. (ok, now I see it added in 2.7 man page) – akostadinov Feb 05 '16 at 09:34
-
Sadly, this is still not quite as nice as Mercurial's `-R` option, since it messes up relative paths. For instance, if I type `git -C subdir log subdir/myfile` it gives an error (since it looks for the file `subdir/subdir/myfile`). – jwd Mar 20 '17 at 23:16
-
--git-dir is handier when you do something like this: `for p in (find . -type d -name ".git"); do git --git-dir $p ...; done` – shabunc Dec 03 '17 at 21:13
-
1@jwd: I'm not sure how that's a problem? What you quoted is the precise behaviour I would expect, and I wouldn't want `git` to think it was cleverer than me and what I typed. If I say 'run this in directory X', I don't mean 'except sometimes, do something else'. – underscore_d Jan 27 '18 at 18:29
-
@underscore_d: It's just a bit of inconvenience, IMO. I don't think it's a particularly clever or unexpected idea: If I give a command the path to a file (perhaps using tab-completion to get the name easily), I would expect that command to know what file I'm talking about, even if it might have a different name relative to its enclosing repo. – jwd Mar 23 '18 at 23:08
-
I tried this using for-each-ref and it failed on me :/ standard commands seem to work find tho. Command: `git -c ~/projects/my-project for-each-ref --format='%(refname:short)' refs/heads/` Error: `error: key does not contain a section: ~/projects/my-project` – Jackson Blankenship Sep 16 '21 at 20:21
-
1@JacksonBlankenship `-c` would not be right (it is for setting config); `-C` would (it it to change path) – VonC Sep 16 '21 at 20:41
-
All possible options:
-C <path>
: Runs command as if git were started in<path>
.--git-dir=<path>
: Sets the path to the ".git" directory.--work-tree=<path>
: Sets path to the working tree.
Usually, -C
is what most people want. For other uses, consider combining the above.

- 24,552
- 19
- 101
- 135