Git alias with parameters

You can form more complex and useful git aliases when you realize you can write aliases like you would write a standard shell function. Prepending your alias with an exclamation mark tells git to expand the alias to your shell, enabling the use of standard unix tools to do some fun things.

Simple Example

[alias]
    ...
    sayhello = !(echo "Hello World")
    ...

This shows you how to run a simple single line bash command as a git alias. You first tell git you are going to run a shell command with !() then you give it your single line of bash.

Multi line aliases and passing parameters

The next step is to be able to run more of a shell script. Multiple lines and be able to pass in arguments.

[alias]
    ...
    say = "!f() { msg=${1-Hello World}; echo $msg;  }; f"
    ...

This illustrates the structure of a basic shell script git alias that allows for multiple lines and arguments. You first tell git you are going to run a shell command with !, then you define a shell function named f with f(){ };, then run it with f;. This enables you to accept arguments into your functions like $1 in our case.

Also, in case you are wondering, ${1-Hello World} assigns "Hello World" to $msg if no value is present in $1 which is how I chose to provide a default value.