有没有办法在没有存储库的情况下对存储库执行Git命令?
比如这样的事情:git /home/repo log
?
请不要告诉我cd
.我是通过exec
电话这样做的.
cal*_*doa 180
使用-C
选项(docs):
git -C /home/repo log
Run Code Online (Sandbox Code Playgroud)
这几乎等同于--git-dir
和--work-tree
不附加平常.git
文件夹
编辑:
回答downvoted ......非常惊人,严格来说,这是这个问题的唯一正确答案.选项--git-dir
和--work-tree
不存在从工作树之外访问该存储库,它们被用于移动.git
别的地方,他们要复杂得多在某些情况下使用.
例如,要/home/repo/subdir
仅获取日志:
git -C /home/repo/subdir log .
Run Code Online (Sandbox Code Playgroud)
要么
git -C /home/repo log subdir
Run Code Online (Sandbox Code Playgroud)
这是不可能的使用log .
与--git-dir
或--work-tree
.必须处理路径以提取相对于工作树顶部的子路径,即使在这种情况下,如果不使用该--
选项,git也不会将其识别为路径,因此唯一可行的方法是:
git --git-dir /home/repo/.git log -- subdir
Run Code Online (Sandbox Code Playgroud)
此外,使用我的版本(git 1.9.1)的子命令--work-tree
完全不起作用log
.它被忽略了:
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
Run Code Online (Sandbox Code Playgroud)
我甚至不知道这是一个错误还是一个功能......像往常一样有很多git设计选择.
max*_*max 79
尝试:
git --git-dir=/home/repo/.git log
Run Code Online (Sandbox Code Playgroud)
将路径一直提供到存储库的.git目录非常重要.否则,您将只收到一条错误消息,内容如下:
fatal: Not a git repository
Run Code Online (Sandbox Code Playgroud)
alb*_*o56 17
实际上你需要一起使用--git-dir和--work-tree.这是一个例子:
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
Run Code Online (Sandbox Code Playgroud)
我试过很多次了!我终于明白了!
git -C dir --no-pager log --format='%an' -1 filename
请记住,请不要添加.git给你
-C dir
小智 6
对于任何 git 命令,您可以执行以下操作:
git --git-dir=<PATH-TO-REPO>/.git --work-tree=<PATH-TO-REPO> <git-command>
Run Code Online (Sandbox Code Playgroud)
例如,如果你想执行 git status:
git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/ status
Run Code Online (Sandbox Code Playgroud)
或者如果您想检查存储库所在的分支:
git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/ rev-parse --abbrev-ref HEAD
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26051 次 |
最近记录: |