Sub topic 5
Aliases that bundle options and reuse other aliases
This chapter assumes you can run git config --global and know what the [alias] section of ~/.gitconfig looks like. You should also know what git log, git status and git restore do. Here you will build aliases that carry your preferred options, then build new aliases on top of existing ones.
Bundling options into an alias
An alias expands to a subcommand plus options. Whatever you type after the alias is appended to the end of the expansion. This lets you fix the options you always want and still add more at the call site.
[alias]
st = status -sb
lg = log --graph --oneline --decorate
last = log -1 HEAD --stat
unstage = restore --staged --
amend = commit --amend --no-editYou can write the same entries from the shell:
git config --global alias.st "status -sb"
git config --global alias.lg "log --graph --oneline --decorate"Some examples of what happens to extra arguments:
git lg -5runsgit log --graph --oneline --decorate -5.git unstage app.pyrunsgit restore --staged -- app.py. The trailing--makes sure the file name is read as a path and not as an option or a branch name.git amendrunsgit commit --amend --no-edit, which adds staged changes to the last commit and keeps its message.
Because arguments are appended, put the options you always want first and leave the end open for the caller. An alias that ends in -- is a common way to prepare for a path argument.
Reusing one alias inside another
An alias can start with the name of another alias. Git expands the first word repeatedly until it reaches a real command. So you can define a small base alias and derive variants from it:
[alias]
lg = log --graph --oneline --decorate
lga = lg --all
lgm = lg --author=ada --since=1.weekHere git lga -10 expands in two steps:
lga -10 -> lg --all -10 -> log --graph --oneline --decorate --all -10If you later change lg, both lga and lgm pick up the change. That is the main benefit: one place holds the shared options.
Only the first word of the expansion is checked for another alias. In lg --all, lg is expanded, but a word like --all is passed through untouched.
Limits of option bundling
These points cause most surprises at this stage:
| Situation | What happens |
|---|---|
| Alias named like a built-in command, such as `commit = commit -v` | Git ignores it. Built-in commands always win. |
| Two aliases that refer to each other | Git stops with an "alias loop detected" error. |
| You need an argument in the middle of the expansion | Plain aliases cannot do this. Arguments only go at the end. |
| Alias value contains spaces or options | Quote it when you set it with `git config`, or the shell splits it. |
To change an option with a value, remember that a later option usually overrides an earlier one. If lg includes --oneline and you want a different format for one run, use git lg --pretty=short. Check the result, since some option pairs conflict.
For a setting you want on every git commit, prefer a configuration option such as commit.verbose over an alias, because it also applies when other tools call Git. Aliases only change what you type.
Checking what an alias does
List every alias, or look up a single one:
git config --get-regexp '^alias\.'
git config --get alias.lgaFor a quick view of the whole alias list, add aliases = config --get-regexp ^alias to your [alias] section. After that, git aliases prints them all.
Exercise
Given lg = log --graph --oneline --decorate and lga = lg --all, what command does Git finally run for git lga -3? Why can you not define commit = commit -v?
Show answer
It runs git log --graph --oneline --decorate --all -3. Git expands lga to lg --all -3, then expands lg, and the extra -3 stays at the end. The alias commit = commit -v is ignored because aliases cannot override built-in commands. Use git config --global commit.verbose true instead.