Sub topic 7
Function Aliases and Argument Handling
This chapter assumes you can write simple aliases and know that an alias starting with ! runs in the shell. Here you will learn how to pass arguments to those shell aliases, and why a function is often the right tool.
Why arguments need care
Git appends whatever you type after an alias to the end of its expansion. For a plain alias this works well. git st -v becomes git status -sb -v.
Shell aliases behave the same way, and that causes trouble when the argument belongs in the middle of the command. Consider this alias:
[alias]
fixup = "!git commit --fixup=$1"Git runs the text through the shell and then appends your arguments at the end. Running git fixup abc123 executes git commit --fixup= abc123. The $1 is empty because the shell has no positional parameters at that point. The result is a broken command.
Wrapping the command in a function
A function gives the arguments a place to land. You define the function inside the alias, then call it on the same line.
[alias]
fixup = "!f() { git commit --fixup=\"$1\"; }; f"Read the value from left to right:
!tells Git to run the rest in a shell.f() { ... }defines a shell function namedf.; fcalls it at the end.
Git still appends your arguments after the final f. So git fixup abc123 runs f abc123, and inside the function $1 is abc123.
The backslashes in the file matter. The outer double quotes belong to the config syntax. The inner quotes around $1 must be escaped as \" so the config parser keeps them. The quotes protect values that contain spaces.
Passing all arguments
Use "$@" when the alias should accept any number of arguments.
[alias]
count = "!f() { git log --oneline \"$@\" | wc -l; }; f"Now git count main..feature counts commits in that range, and git count --author=Ada counts one author's commits. "$@" expands to each argument as a separate, correctly quoted word.
Setting defaults
Shell parameter expansion lets you supply a value when the user gives none.
[alias]
recent = "!f() { git log -\"${1:-5}\" --oneline; }; f"${1:-5} means "use the first argument, or 5 if it is empty or missing". git recent shows five commits and git recent 12 shows twelve.
The working directory
Git runs shell aliases from the top level of the working tree, not from the directory you are in. A relative path you type will not resolve as you expect when you are in a subdirectory. Git sets the environment variable GIT_PREFIX to your original subdirectory, relative to the top level. Use it to change back:
[alias]
here = "!f() { cd \"${GIT_PREFIX:-.}\" && ls; }; f"Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| `$1` is empty | Alias has no function wrapper | Use `!f() { ...; }; f` |
| Argument appears twice or in the wrong spot | Git appended arguments after the expansion | Wrap in a function |
| Quote errors when Git loads the config | Inner `"` not escaped | Write `\"` inside a double-quoted value |
| Paths are not found | Alias ran at the repository root | Use `GIT_PREFIX` |
Seeing what Git will run
You can inspect the stored value of an alias without running it.
git config --get alias.fixupIf the output looks different from what you typed, a quoting error is the likely cause. Compare it with the intended command character by character.
Exercise
Write an alias named ls-author that prints the number of commits by an author passed as the first argument. git ls-author Ada should run git log --author=Ada --oneline | wc -l. Why does it need a function?
Show answer
ls-author = "!f() { git log --author=\"$1\" --oneline | wc -l; }; f". It needs a function because Git appends the arguments after the whole expansion. Without the function, "Ada" would be passed to wc -l at the end of the pipeline, and $1 would be empty. The function receives the argument and lets you place it in the middle of the command.
Exercise
What does ${1:-10} do in an alias, and how would you use it to build git recent that shows 10 commits by default?
Show answer
It expands to the first argument, or to 10 if no argument is given. The alias is: recent = "!f() { git log -\"${1:-10}\" --oneline; }; f".