You happened to have which defined as:
which(){
/usr/bin/which -a "$@" |
xargs ls -l |
tr -s ' ' |
cut -d ' ' -f 9-
}
ls sorts the filename lexically on output and /usr/local/bin/git comes after /usr/bin/git because l comes after b in your locale.
The GNU implementation of ls has a -U option to disable that sorting.
Your /usr/bin/which command seems to be one that prints the path of all command names found in $PATH when passed a -a option. With zsh builtins, you can do the same with whence -pa¹.
So you could do something like:
mywhich() (
set -o pipefail
zmodload zsh/stat
whence -pa "$@" |
while IFS= read -r f; do
if [[ -L $f ]] && stat -A l +link -- $f; then
print -r -- "$f -> $l"
else
print -r -- $f
fi
done
)
(here assuming that none of the file paths contain newline characters).
A more correct version of yours, on a GNU system, would be like:
mywhich() (
set -o pipefail
command which -a "$@" |
xargs -rd '\n' ls -ndU -- |
sed -E 's/([^ ]+ +){8}//'
)
In any case, note that bash doesn't have a which builtin, so what which outputs there is independent of the shell. Only tcsh and zsh have which builtin.
¹ though, like your /usr/bin/which (but contrary to zsh's builtin which), it won't necessarily tell you which command the shell would run as it ignores aliases, functions, builtins and even the $hash table of executables. See also Why not use "which"? What to use then?