m1well
m1well

Reputation: 884

How can I redirect/catch the "git" command and do some things before the real git command?

actually I am writing this function linked in my zshrc:

function mgit {
  string='github'
  remote=$(git remote -v)
  if [[ ${remote} == *${string}* ]]; then
    git config --global user.name "name1"
    git config --global user.email "[email protected]"
  else
    git config --global user.name "name2"
    git config --global user.email "[email protected]"
  fi
  git "$@"
}

This works fine if I now execute mgit --version instead git --version.

But is there a way to catch the real git command and execute this function?
Because now i cant use my aliases e.g. ga for git add ....
And is it then possible with all commands like docker or something else?

Upvotes: 0

Views: 45

Answers (1)

that other guy
that other guy

Reputation: 123500

Yes. Just call your function git and use command git to call the "real" git:

git() {
  echo "Do some things"
  command git "$@"
}

Example:

% git --version
Do some things
git version 2.19.2 

Upvotes: 6

Related Questions