Sub topic 8
What aliases cannot do and common pitfalls
This chapter assumes you can already define simple and shell aliases with git config and that you know the ! prefix runs a command in the shell. Aliases are convenient, but they have fixed limits, and a few of those limits fail without any error message.
Limits of the alias mechanism
| Limit | What happens | What to do |
|---|---|---|
| You cannot replace a built-in command | An alias named `status` or `commit` is ignored, and the built-in runs | Choose a new name, such as `st` |
| Arguments are only appended | `git since main` expands to `log --oneline main`, so you cannot place an argument in the middle | Use a function alias |
| Alias names are case-insensitive in the config file | `lg` and `Lg` are the same key | Define names in lowercase |
| Aliases exist only in your configuration | Teammates, CI jobs and scripts do not have them | Write the full command in shared scripts |
| An alias cannot loop | An alias that expands back to itself is rejected | Check the chain with `git config --get-regexp '^alias'` |
The built-in rule is the most common surprise. The following command succeeds, but it changes nothing:
git config --global alias.status "status -sb"
git statusGit runs the normal status. This protects you from breaking scripts that call built-in commands. To get the short form, define st as in the earlier chapters.
Arguments always go at the end
A simple alias is a string that Git splits into words. Anything you type after the alias name is added to the end. This works well for options:
git lg -5This runs git log --graph --oneline --decorate --all -5. It does not work when the argument belongs in the middle of a command. A function alias fixes that, because "$1" can go anywhere:
[alias]
since = "!f() { git log --oneline \"$1\"..HEAD; }; f"git since main now lists the commits on your branch that are not on main. The trailing ; f is required. Without it, Git defines the function but never calls it.
Shell aliases start at the repository root
Git runs a ! alias from the top of the working tree, not from the directory you are in. Relative paths in the alias therefore point at the wrong place. Git stores your original location, relative to the root, in GIT_PREFIX:
git config --global alias.here '!pwd; echo "prefix: $GIT_PREFIX"'
cd src
git hereThe output shows the repository root, then prefix: src/. Use cd "$GIT_PREFIX" at the start of an alias that must work with relative paths.
Quoting causes silent mistakes
Two layers of quoting apply: your shell parses the git config command, and then Git parses the stored value. When you set an alias from the command line, wrap the whole alias in single quotes and use double quotes inside:
git config --global alias.fixup '!f() { git commit --fixup="$1"; }; f'If you use double quotes on the outside, your shell expands $1 immediately, and the stored alias is broken. Inside the config file you must escape double quotes and backslashes yourself, as the fixup and cleanup examples earlier did. After any change, run git config --get alias.name to see exactly what was stored.
Aliases that hide risk
An alias runs without confirmation, so avoid putting destructive behavior in a short name. pushf is acceptable because it uses --force-with-lease, which refuses to overwrite commits you have not seen. An alias for plain push --force is not.
The cleanup alias has a subtler problem. git branch --merged lists every branch merged into the current one, and it can include main when you are on a feature branch. The grep -v '\*' step only protects the current branch. Add a second filter, such as grep -v -E 'main|master', before you rely on it.
flowchart TDExercise
You define alias.since = log --oneline main..HEAD and want git since develop to list commits since develop. Why does it fail, and how do you fix it?
Show answer
The name develop is appended to the end, giving log --oneline main..HEAD develop, which is not what you want. Use a function alias: since = "!f() { git log --oneline \"$1\"..HEAD; }; f".
Exercise
A shell alias that runs ls docs works from the repository root but fails from a subdirectory. What causes this, and what variable helps?
Show answer
Git runs ! aliases from the repository root, so docs is resolved there. The variable GIT_PREFIX holds your original subdirectory, so you can run cd "$GIT_PREFIX" first.