Syntax for Git aliases with multiple commands

$ git config alias.q '!echo a; echo b'

$ git q

Output:

a
b

I think this is (rudimentarily) documented in man git-config under alias.*

Note that git commands should include git, unlike in normal aliases. It is caused by fact that it is treated as a shell command, not as a git command (see manpage quoted in the question). For example to chain

git init

and

git commit --allow-empty -m "empty initial commit"

it is necessary to create

"!git init; git commit --allow-empty -m \"empty initial commit\""

alias.


Say the commands are echo a and echo b (not a and b), to add multiple commands for an alias q:

From the command line:
git config alias.q '!echo a; echo b'

Directly in the configuration file:

[alias]
    q = "!echo a; echo b"

For more complex things, define a shell function and call it:
'!f() { echo a ; echo b ; }; f'

For passing parameters to the commands see:
Git alias with positional parameters
Git Alias - Multiple Commands and Parameters

Based in Jonathan Wakely's comment


Addendum Answer: Often I need more complex commands that decide what to do through positional parameters and branch on the parameters or loop through parameters or input files.

Such commands are too complex for single liners and they are hard to read and edit on one line. But I found a real simple method to do very complex commands from files:

Assume you have a file called alias/cmd in your repository:

!function f {
    if [ -z "$1" ]
    then echo "Please give me some argument!" 1>&2
        exit -1
    fi
    echo "Hello $1"
}; f

then you can simply say

git config alias.cmd "`cat alias/cmd`"

to define the alias cmd from the file and

git config --get alias.cmd > alias/cmd

to write the defined alias to the file.

Tags:

Git