Is there a way to execute Git commands against a repository without being in that repository?
For example something like this: git /home/repo log
?
Please do not tell me to cd
to it. I'm doing this via an exec
call.
Is there a way to execute Git commands against a repository without being in that repository?
For example something like this: git /home/repo log
?
Please do not tell me to cd
to it. I'm doing this via an exec
call.
I have tried many times! I finally got it!
git -C dir --no-pager log --format='%an' -1 filename
do remember, please don't add .git to your
Try:
It is important to give the path all the way up to the .git directory of your repository. Otherwise you will get only an error message that says something like:
In fact you need to use --git-dir and --work-tree together. Here is an example:
Use
-C
option (docs):This is almost equivalent to
--git-dir
and--work-tree
without appending the usual.git
folderEdit:
Answer downvoted... quite amazing, as, strictly speaking, this is the only correct answer to the question. The options
--git-dir
and--work-tree
are not existing to access the repository from outside the work tree, they are used to move the.git
somewhere else, and they are much more complicated to use in some cases.For instance, to get the log of
/home/repo/subdir
only:or
It is not possible to use
log .
with--git-dir
or--work-tree
. The path must be processed to extract the subpath relative to the top of the work tree, and even in that case, git will not recognize it as a path if you do not use the--
option, so the only possible way is:Furthermore,
--work-tree
is not working at all withlog
subcommand with my version (git 1.9.1). It is just ignored:I do not even understand if this is a bug or a feature... as usual with many git design choices.
This is similar to @max's answer. Unfortunately, --git-dir didn't do what I needed. edit In retrospect, another [previous] answer I hadn't read suggested using
--work-tree
. I'm not sure whether using environment or flags is more appropriate so I'll leave my answer in case someone finds use in it, but I'll be switching to use--work-tree
/--git-dir
.There's two repos, one inside the other but not a submodule; the outer repo ignores it:
What I wanted was the output of
git rev-parse --show-prefix
relative to the outer repo. Here's what I came up with (bash syntax; split across lines for readability):When executed from within 4.9.2 it produces the string
gcc/4.9.2
.