Sub topic 13
Shortening commands with aliases
This chapter assumes Git is installed, you are comfortable in a shell, and you have already set user.name and user.email. You should also know the global and local scopes from earlier chapters.
What an alias is
An alias is a git config entry under the alias. prefix. It gives a short name to a longer Git command. Git expands the alias when you run it, so git st behaves like git status.
Aliases follow the same scope rules as every other setting. A global alias is available in all your repositories. A local alias exists only in one repository and overrides a global alias with the same name.
Creating and removing an alias
Set an alias with git config and the key alias.<name>. Remove it with --unset.
git config --global alias.st status
git st
git config --global --unset alias.stThe first line writes st = status into the [alias] section of your global file. The last line removes it. After that, git st fails with an error saying st is not a git command.
Aliases with options
The value can hold more than one word. Git splits it into the subcommand and its arguments.
git config --global alias.lg "log --oneline --graph --decorate"
git config --global alias.unstage "restore --staged"Quote the value so your shell passes it to Git as one argument. Now git lg prints a compact history graph. git unstage notes.txt runs git restore --staged notes.txt, and extra arguments you type are appended to the expansion.
What the file looks like
Each alias is a line in the [alias] section of the configuration file. You can confirm this by opening the file with git config --global --edit.
[alias]
st = status
lg = log --oneline --graph --decorate
unstage = restore --stagedYou can also write the lines by hand. This is equivalent to running the commands above.
Common errors
- Unquoted values. Without quotes,
git config --global alias.lg log --onelinefails because Git receives extra arguments it does not expect. Quote any value that contains spaces. - Shadowing a built-in command. Git ignores an alias whose name matches a built-in command. Defining
alias.statusdoes not changegit status. Pick a name that is not already a Git command. - Wrong scope. An alias set with
--localis missing in other repositories. If an alias works in one project and not another, check where it is defined. - Aliases for other aliases. Git does not expand an alias that points to another alias. Write the full command in each one.
Checking where an alias comes from
Use the inspection commands from earlier chapters to see the value and its source file.
git config --show-origin --get alias.lg
git config --global --get-regexp '^alias\.'The first command prints the file that supplies alias.lg. The second lists every global alias.
Shell aliases
If the value starts with !, Git runs it as a shell command instead of as a Git subcommand. This lets an alias chain commands.
git config --global alias.last "!git log -1 --stat"Use these sparingly. They depend on your shell, so they may not work the same on Windows and Linux.
Exercise
You ran git config --global alias.status "status -sb", but git status still prints the long format. Why, and what is a fix?
Show answer
Git ignores aliases that share a name with a built-in command, so git status always runs the real command. Choose a different name, such as git config --global alias.ss "status -sb", and run git ss.
Exercise
Which command shows which file defines the alias lg?
Show answer
git config --show-origin --get alias.lg