Git Advanced Aliases


Example

Git lets you use non-git commands and full sh shell syntax in your aliases if you prefix them with !.

In your ~/.gitconfig file:

[alias]
    temp = !git add -A && git commit -m "Temp"

The fact that full shell syntax is available in these prefixed aliases also means you can use shell functions to construct more complex aliases, such as ones which utilize command line arguments:

[alias]
    ignore = "!f() { echo $1 >> .gitignore; }; f"

The above alias defines the f function, then runs it with any arguments you pass to the alias. So running git ignore .tmp/ would add .tmp/ to your .gitignore file.

In fact, this pattern is so useful that Git defines $1, $2, etc. variables for you, so you don't even have to define a special function for it. (But keep in mind that Git will also append the arguments anyway, even if you access it via these variables, so you may want to add a dummy command at the end.)

Note that aliases prefixed with ! in this way are run from the root directory of your git checkout, even if your current directory is deeper in the tree. This can be a useful way to run a command from the root without having to cd there explicitly.

[alias]
    ignore = "! echo $1 >> .gitignore"