如何重定向/捕获"git"命令并在真正的git命令之前做一些事情?

m1w*_*ell 0 git bash shell zsh

实际上我正在写我的zshrc链接的这个函数:

function mgit {
  string='github'
  remote=$(git remote -v)
  if [[ ${remote} == *${string}* ]]; then
    git config --global user.name "name1"
    git config --global user.email "email1@test.com"
  else
    git config --global user.name "name2"
    git config --global user.email "email2@test.de"
  fi
  git "$@"
}
Run Code Online (Sandbox Code Playgroud)

如果我现在执行这工作得很好mgit --version,而不是git --version.

但有没有办法捕获真正的git命令并执行此功能?
因为现在我不能用我的别名,例如ga用于git add ....
而是它可能然后用像搬运工或别的东西所有的命令?

tha*_*guy 6

是.只需调用您的函数git并使用它command git来调用"真实" git:

git() {
  echo "Do some things"
  command git "$@"
}
Run Code Online (Sandbox Code Playgroud)

例:

% git --version
Do some things
git version 2.19.2 
Run Code Online (Sandbox Code Playgroud)