Git Reference ยท Configuration Options
The alias.* settings
Reference for alias.*: define short names for Git commands, pass extra arguments, and write shell aliases with a leading exclamation mark for scripts.
An alias is a name you invent for a command line. Define alias.st as status -sb and git st runs git status -sb. Aliases live in ordinary config files, so you can keep personal ones in your global config and project ones in a repository's config.
There are two kinds. A Git alias expands to another Git command plus options. A shell alias starts with ! and runs through the shell, so it can chain several commands, use pipes and call tools that are not Git at all.
git config --global alias.st "status -sb" git config --global alias.last "log -1 --stat" git config --global alias.count '!git rev-list --count HEAD' git config --get-regexp '^alias\.' # list them
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git config [--global] alias.<name> "<git command and options>" git config [--global] alias.<name> '!<shell command>'
| Part | Description |
|---|---|
<name> |
The new subcommand. Letters, digits and - work; avoid built-in command names, which always win. |
| plain value | Words after git. Quoting follows config rules: use double quotes inside for arguments with spaces. Extra arguments typed after the alias are appended. |
! value |
Run by sh -c. Extra arguments are appended to the command line too, so wrap logic in a function (!f() { ...; }; f) to place $1 where you want. |
Environment for shell aliases: the working directory is the top level of the working tree, and GIT_PREFIX holds the path you were in. GIT_DIR is set when run in a repository.
Errors: a loop (a expands to b, b to a) is detected: "fatal: alias loop detected: expansion of 'a' does not terminate".
Usage notes
- Built-ins win. Defining
alias.statushas no effect, as the example shows. Name yours differently (st,s). - Aliases of aliases work, up to a point; a shell alias cannot be reached from another alias that expects Git options.
- No spaces in alias names, and the config key is case-insensitive in its name part (
alias.Ciisci). - Quoting is the main trap. A shell alias is a string in the config file, so
$1, quotes and backslashes pass through two layers. Test withgit config alias.xto see what is stored, or put complex logic in a script namedgit-xon yourPATH, which Git runs forgit xwithout any alias. help.autocorrectcan guess a mistyped command; aliases are checked before autocorrection.- Global versus project: committing an alias helps the team only if each clone copies it. Use a documented
.gitconfiginclude or a setup script. git <alias> --helpreports "'st' is aliased to 'status -sb'".- Shell aliases run with the repository's trust. Be careful when cloning other people's configuration into your global file.
Related
help.autocorrect,git config,git log,git status- Lessons: Aliases, Cheat sheet