如何从特定目录而不是仓库的根目录执行git别名

如何从特定目录而不是仓库的根目录执行git别名

问题描述:

Git似乎从仓库的根目录开始执行所有别名.您可以使用以下别名查看此信息:

Git seems to execute all the aliases from the root of the repo. You can see this by using the following alias:

test = !pwd

甚至

test2 = "!f() { pwd ;}; f"

这两者都将显示存储库根目录,而不是您所在的子目录.

which will both display the directory of the root of the repo, not the subdirectory that you are in.

如何创建从您所在目录而不是根目录执行的别名?

How can I make an alias that executes from the directory that you are in, not the root?

您可以使用变量$GIT_PREFIX.例如:

You can use the variable $GIT_PREFIX. For example:

test = !echo $GIT_PREFIX

或更有效的用例:

cat-from = "!f() { git show $1:\"$GIT_PREFIX$2\" ;}; f"

可以用来从特定分支中收集文件,而无需检出它:git cat-from master foo.txt

which can be used to cat a file from a particular branch without checking it out: git cat-from master foo.txt

我在以下源代码中找到了它:

I found this by looking in the source code here: https://github.com/git/git/blob/35f6318d44379452d8d33e880d8df0267b4a0cd0/t/t1020-subdirectory.sh#L131

如果可以找到记录在哪里,请添加编辑/评论/答案.

Please add an edit/comment/answer if you can find where this is documented.