I added an alias in my ~/.gitconfig
:
[alias]
h = "!git status -s | LC_ALL=C sort -k1 | my_function"
But when I call git h
, I get:
> git h
git status -s|LC_ALL=C sort -k1| my_function: my_function: command not found
Now, I know that my function exists in my local shell. It's in my .bashrc
and I can see it when I call compgen -A function
:
> compgen -A function|grep my_function
my_function
> my_function
(expected output)
Why isn't git
recognizing that I've already defined the function in my shell?
When git
runs an alias, it actually runs it in a subprocess. If you start the alias with !
, then it is translated into sh -C "$aliasWithoutBang"
by the Git processor itself. This means it does not actually run in the same shell with the same variables or functions as the environment calling git.
h = bash -ic 'git status -s | LC_ALL=C sort -k1 | my_function'
This has two benefits.
-i
) and so loads the .bashrc
.bash
.Thanks to Glenn Jackman for the comments.
I expect there is a better way to do this, but I was able to resolve my problem by changing the alias to:
h = "!. ~/.bashrc && git status -s | LC_ALL=C sort -k1 | my_function"
Basically, I forced Git to re-load the environmental variables for this particular alias.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.