Sub topic 4
Your first aliases
This chapter assumes you can run Git commands in a terminal and know what status, checkout, branch and log do. It also assumes you know that git config --global writes to your personal config file, which the earlier chapters on scopes and the git config command covered.
What an alias is
An alias is a name you define in your Git configuration that stands for a longer Git command. When you type git st, Git finds no built-in command called st. It then looks in your configuration for alias.st and runs the text it finds there.
Creating an alias with git config
The quickest way is to set a key named alias.<name>:
git config --global alias.st "status -sb"
git config --global alias.co checkout
git config --global alias.br branchNow git st runs git status -sb, which prints a short status with branch information. git co main runs git checkout main.
Quote the value when it contains spaces. Without quotes, the shell splits status -sb into separate arguments and git config rejects the extra one.
Creating aliases by editing the file
Each git config --global alias.x ... command adds a line to ~/.gitconfig. You can also write them yourself. Open the file with git config --global --edit and add an [alias] section:
[alias]
st = status -sb
co = checkout
br = branch
lg = log --graph --oneline --decorate --all
last = log -1 HEAD --stat
unstage = restore --staged --
amend = commit --amend --no-edit
aliases = config --get-regexp ^aliasEach line has the form name = expansion. The expansion is what you would type after git, so you leave out the word git itself.
A few of these are worth understanding:
lastshows the most recent commit and the files it changed.unstageends with--, sogit unstage notes.txtbecomesgit restore --staged -- notes.txt. The--marks the end of options, which keeps a file name from being read as an option.git restoreneeds Git 2.23 or later.amendadds your staged changes to the previous commit and keeps its message.aliaseslists your aliases. It is an alias that inspects the alias list.
Extra words you type after an alias are appended to the expansion. git lg -5 runs git log --graph --oneline --decorate --all -5.
Checking what you have
To see what an alias expands to, ask Git:
git config --get alias.st
git config --get-regexp '^alias\.'The first command prints one expansion. The second lists every alias in all config files Git reads.
Common errors
- The alias is ignored. Git never lets an alias replace a built-in command. If you define
alias.status,git statusstill runs the real command. Choose a name that is not a Git command. - "expansion of alias failed" or "not a git command". A typo in the expansion is the usual cause, such as
statsu -sb. Run the expanded command by hand to find the mistake. - The alias works in one repository only. You probably used
--local, or left out--global. Local settings are stored in.git/configof that one repository. - Using
gitinside the expansion. Writingst = git statusmakes Git look for a command namedgit. Leave it out. - Names with spaces. An alias name is one word.
git config alias."my status"does not do what you want.
Practice
Exercise
You run git config --global alias.unstage "restore --staged --". What does git unstage app.py execute, and why does the expansion end with --?
Show answer
It executes git restore --staged -- app.py, which removes app.py from the staging area and leaves your working copy unchanged. The -- ends option parsing, so a file name that starts with a dash is treated as a path and not as an option.
Exercise
You add status = status -sb to your [alias] section, but git status still prints the long format. Why, and what should you do?
Show answer
Git never lets an alias override a built-in command, so the real git status always runs. Pick an unused name such as st and define st = status -sb.