Sub topic 6
Shell aliases with the ! prefix
This chapter assumes you can edit ~/.gitconfig and run git config. It also assumes you have written simple aliases such as st = status -sb. Here you will learn how to run arbitrary shell commands from an alias.
Why a plain alias is not enough
A plain alias expands only to a Git subcommand and its options. It cannot run grep, pipe output into another program, or call a second Git command after the first. When the alias value starts with !, Git passes the rest of the text to your shell instead.
[alias]
root = !pwd
whoami = !echo "$(git config user.name) <$(git config user.email)>"Running git root prints the top-level directory of the repository. Running git whoami prints the identity Git will use for commits.
How Git runs a shell alias
Git looks up the alias, sees the !, and starts sh -c with the text that follows. Git appends any arguments you typed to the end of that text.
Two details matter in practice:
- Git runs the shell alias from the top-level directory of the repository, even if you typed it in a subdirectory. Git sets the environment variable
GIT_PREFIXto the subdirectory you came from. - The shell is
sh, not your login shell. Avoid Bash-only or Zsh-only syntax.
Pipelines
Shell aliases let you chain commands with pipes. This alias deletes local branches that are already merged:
[alias]
cleanup = "!git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d"The value is in double quotes, so the config parser turns \\ into a single backslash. The shell therefore sees grep -v '\*', which skips the line for your current branch.
Function aliases and arguments
Git appends your arguments to the end of the alias text. That works for a single command, but it breaks when the arguments need to go somewhere in the middle. The usual fix is a shell function that you define and then call in the same line:
[alias]
fixup = "!f() { git commit --fixup=\"$1\"; }; f"Running git fixup 3f2a9c1 makes the shell execute f() { ... }; f "$@". Git adds the "$@" after the final f, so the function receives 3f2a9c1 as $1. Without the trailing f, the function would be defined but never called.
The \" inside the value is how you write a literal double quote inside a double-quoted config string.
A function also lets you use the arguments more than once:
[alias]
new = "!f() { git switch -c \"$1\" && git push -u origin \"$1\"; }; f"Setting a shell alias from the command line
Quoting is the hard part when you use git config instead of editing the file. Use single quotes so your shell does not expand $1 early:
git config --global alias.new '!f() { git switch -c "$1" && git push -u origin "$1"; }; f'
git config --global --get alias.newThe second command prints the stored value so you can check what Git saved.
Common pitfalls
| Symptom | Cause | Fix |
|---|---|---|
| `$1` is empty or contains the wrong text | Your shell expanded `$1` before Git saw it | Use single quotes on the command line, or edit the file directly |
| Arguments land at the end of a pipeline | No function wrapper | Wrap the commands in `f() { ...; }; f` |
| Relative paths point to the wrong place | Git runs the alias from the repository root | Use `$GIT_PREFIX` to build paths from where you typed the command |
| `unexpected token` or similar errors | Bash-only syntax run by `sh` | Use POSIX syntax only |
Exercise
In fixup = "!f() { git commit --fixup=\"$1\"; }; f", what does the trailing f do, and what would happen without it?
Show answer
The trailing f calls the function that was just defined. Git appends the typed arguments after it, so f receives them as $1. Without it, the shell would define the function and exit without running it, so git fixup <commit> would do nothing.
Exercise
You run git root from ~/project/src/ and the alias is root = !pwd. Which directory does it print, and why?
Show answer
It prints ~/project, the top-level directory of the repository. Git runs shell aliases from the repository root, and puts the original subdirectory in GIT_PREFIX.