【问题标题】:Which shell used for git '!' aliases?哪个 shell 用于 git '!'别名?
【发布时间】:2026-01-27 22:35:02
【问题描述】:

git 在执行以! 开头的别名时使用什么默认 shell?

我怀疑它是 /bin/sh,因为在别名中使用 read 与在我的 shell(即 bash)中使用时会出现不同的行为。

+ravi@boxy:~/.dotfiles(master*% u+2)$ grep 'test =' ~/.gitconfig    
        test = "!f() { read -n1 -p "Works?" r; } ; f "
+ravi@boxy:~/.dotfiles(master*% u+2)$ git test
f() { read -n1 -p Works? r; } ; f : 1: read: Illegal option -n
+ravi@boxy:~/.dotfiles(master*% u+2)[2]$ read -n1 -p "Works?" r; # Try it in current shell (bash)
Works?Y+ravi@boxy:~/.dotfiles(master*% u+2)$ # OK, that worked...
+ravi@boxy:~/.dotfiles(master*% u+2)$ grep 'sh =' ~/.gitconfig
        sh = "!f() { exec \"$@\"; }; f"
+ravi@boxy:~/.dotfiles(master*% u+2)$ git sh env | grep SHELL
SHELL=/bin/bash
+ravi@boxy:~/.dotfiles(master*% u+2)$ # WTF??????????
+ravi@boxy:~/.dotfiles(master*% u+2)$ type read
read is a shell builtin
+ravi@boxy:~/.dotfiles(master*% u+2)$ which read
+ravi@boxy:~/.dotfiles(master*% u+2)[1]$ # none, so why is the shell behaving differently?
+ravi@boxy:~/.dotfiles(master*% u+2)[1]$ git sh bash -c read -n1
Hmm, this is ok??
+ravi@boxy:~/.dotfiles(master*% u+2)$ # Yes, that worked.

如何设置在处理以! 开头的别名时使用哪个 shell?

【问题讨论】:

    标签: git


    【解决方案1】:

    As described here,您可以通过以下方式查看更多信息:

    GIT_TRACE=1 git test
    

    解决方法是创建一个名为 git-xxx 的 shell 脚本(其第一行可以是 shebang #!/bin/bash)。然后任何git xxx 调用都会调用该脚本(前提是它在$PATH 中)。

    由于OP Tom Hale 加上in the comments,所以使用/bin/sh

    trace: exec: '/bin/sh' '-c' 'echo Which shell?' 
    'echo Which shell?'
    

    【讨论】:

    • trace: exec: '/bin/sh' '-c' 'echo Which shell?' 'echo Which shell?'
    • @TomHale 谢谢。我已将您的评论包含在答案中以提高知名度。
    • 特别喜欢如何创建 git 自定义命令的参考。我的外壳也有问题。我试着让它在路径中发出命令,它就像一个魅力。竖起大拇指!