如何从特定目录而不是仓库的根目录执行git别名 [英] How can I execute a git alias from a specific directory, not the root of the repo

查看:170
本文介绍了如何从特定目录而不是仓库的根目录执行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.

这篇关于如何从特定目录而不是仓库的根目录执行git别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆