在另一个文件夹中使用 git "log" 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3769137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use git "log" command in another folder
提问by AlphaB
I have some php files in a Folder A (which is a git project). In these php file I want to execute "git log" but for the folder B. Folder B is another git project (so log is different between A and B).
我在文件夹 A(这是一个 git 项目)中有一些 php 文件。在这些 php 文件中,我想执行“git log”,但对于文件夹 B。文件夹 B 是另一个 git 项目(因此日志在 A 和 B 之间不同)。
How I can do that with shell command ?
我怎么能用 shell 命令做到这一点?
回答by rgngl
From, man git
:
来自,man git
:
You can do this with the --git-dir
parameter, before passing any commands.
--git-dir
在传递任何命令之前,您可以使用参数执行此操作。
git --git-dir /foo/bar/.git log
(Specifying the .git
directory is necessary.) From the documentation:
(需要指定.git
目录。)来自文档:
--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.
--git-dir=<path>
设置存储库的路径。这也可以通过设置
GIT_DIR
环境变量来控制。它可以是当前工作目录的绝对路径或相对路径。
回答by VonC
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 1.8.5 (Q4 2013),您将有另一种选择,而不是设置--git-dir
.
如果要git log
在文件夹中执行B
,请键入:
git -C B log
Just like "
make -C <directory>
", "git -C <directory> ...
" tells Git to go there before doing anything else.
就像
make -C <directory>
", "git -C <directory> ...
" 告诉 Git 在做任何其他事情之前先去那里。
See commit 44e1e4by 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
在不离开当前目录的情况下,在不同目录中调用 git 命令需要更多的按键:
(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
上面显示的方法对于脚本编写是可以接受的,但是对于快速命令行调用来说太麻烦了。
使用这个新选项,可以通过更少的击键完成上述操作:
git -C ~/foo status
git -C ../.. grep foo
for d in d1 d2 d3; do git -C $d svn rebase; done