Tom Hale Tom Hale - 2 months ago 17
Git Question

Which shell used for git '!' aliases?

What default shell is used by

git
when executing an alias starting with
!
?

I'm suspecting it's /bin/sh, because I'm getting different behaviour when using
read
in an alias vs in my shell (which is
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.


How can I set which shell is used in processsing aliases beginning with
!
?

Answer

As described here, you can see more with:

GIT_TRACE=1 git test

The workaround is to create a shell script (whose first line can be a shebang #!/bin/bash) named git-xxx. Then any git xxx call would invoke that script (provided it is in the $PATH).

As the OP Tom Hale adds in the comments, /bin/sh is used:

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