有没有办法执行Git命令对存储库,而不是在该存储库文件夹?如何执行Git命令而不在存储库文件夹中?
例如,如下所示:git /home/repo log
?
请不要告诉我cd
它。我正在通过拨打exec
来完成此操作。
有没有办法执行Git命令对存储库,而不是在该存储库文件夹?如何执行Git命令而不在存储库文件夹中?
例如,如下所示:git /home/repo log
?
请不要告诉我cd
它。我正在通过拨打exec
来完成此操作。
事实上,你需要使用--git-DIR和 - 共同努力树在一起。这里有一个例子:
local [] Desktop: mkdir git
local [] Desktop: cd git
local [] git: touch README.txt
local [] git: git init
Initialized empty Git repository in /Users/albert/Desktop/git/.git/
local [] git: cd ..
local [] Desktop: git --work-tree=git --git-dir=git/.git add .
local [] Desktop: git --work-tree=git --git-dir=git/.git commit -a -m 'initial commit, called from outside the git directory'
[master (root-commit) ee951b1] initial commit, called from outside the git directory
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.txt
local [] Desktop: cd git
local [] git: git log --pretty=oneline
ee951b161053e0e0948f9e2a36bfbb60f9c87abe initial commit, called from outside the git di
这比单独使用--git-dir的回答更正确。 https://git-scm.com/blog/2010/04/11/environment.html –
使用-C
选项(docs):
git -C /home/repo log
这是几乎相当于--git-dir
和--work-tree
不附加通常.git
文件夹
编辑:
答案downvoted ...相当了不起,严格地说,这是这个问题的唯一正确答案。选项--git-dir
和--work-tree
不存在以从工作树外部访问存储库,它们用于将.git
移动到其他位置,并且在某些情况下使用它们要复杂得多。
例如,获得的只是/home/repo/subdir
日志:
git -C /home/repo/subdir log .
或
git -C /home/repo log subdir
这是不可能使用log .
与--git-dir
或--work-tree
。必须处理路径以提取相对于工作树顶部的子路径,即使在这种情况下,如果不使用--
选项,git也不会将其识别为路径,因此唯一可行的方法是:
git --git-dir /home/repo/.git log -- subdir
此外,--work-tree
根本无法与log
子命令一起使用我的版本(git 1.9.1)。它只是被忽略:
git --git-dir /home/repo/.git --work-tree /home/repo/subdir log -- subdir
git --git-dir /home/repo/.git --work-tree /home/repo/whatever log -- subdir
我甚至不明白这是一个错误还是一个功能......像往常一样有很多git设计选择。
这与@ max的答案类似。不幸的是, - git-dir没有做我所需要的。 编辑回想起来,我没有阅读的另一个[上一个]答案建议使用--work-tree
。我不知道使用环境或标志是否更合适,所以我会离开我的,以防有人发现使用的答案,但我会切换到使用--work-tree
/--git-dir
。
有两大回购,一个在另一个内,但不是一个子模块;外回购忽略它:
tools (outer repo)
gcc/4.9.2 (inner repo)
我想要的是什么git rev-parse --show-prefix
相对的输出到外部回购。以下是我想出了(bash的语法,分裂成多个行以提高可读性):
prefix_dir=$(cd ..; git rev-parse --show-toplevel)
prefix_dir=$(GIT_WORK_TREE=${prefix_dir} git rev-parse --show-prefix)
当从内4.9.2它产生的字符串gcc/4.9.2
执行。
我试过很多次了! 我终于明白了!
git -C dir --no-pager log --format='%an' -1 filename
千万记住,请不要git的添加到您的
-C DIR
你就不能 “CD /首页/回购&& git的日志”作为你的执行电话? –
我有一些奇怪的问题。我开始了一个新的过程,并不是真正的高管,但我不太了解他们的差异。我发现找到一个指定git目录的参数比较容易。的[我可以在一个仓库里的文件执行git的操作(添加/比较/提交)时,我的当前目录是不是在仓库] – Tower
可能重复(http://stackoverflow.com/questions/3205867/can-i-执行-混帐的操作加-DIFF提交-ON-文件-IN-A-存储库时,我的) – Hilikus