Git Aliases

Updated · 9 min read
easyGit 2.53gitaliasesgit-configproductivityinterview

A Git alias is a short name that you define for a longer Git command. After git config --global alias.lg "log --oneline --graph --all", typing git lg runs git log --oneline --graph --all. Aliases are stored as ordinary settings in the alias section of your config. They are used to save typing for commands you run many times a day, to give friendlier names to commands you find hard to remember, and to bundle a few steps into one.

A phone's speed dial is the same idea: "1" calls home, "2" calls the office. The full number still exists. You just reach it faster.

Syntax

Terminal
git config --global alias.<name> "<command and options>"
git config --global alias.<name> "!<shell command>"
FormDescription
alias.st "status -s"It is used to run a Git command (without the word git) with options.
alias.<name> "!<command>"It is used to run any shell command. The ! means "not a Git command".
git config --get-regexp "^alias\."It is used to list all aliases.
git help <alias>It is used to show what an alias stands for.
git config --global --unset alias.<name>It is used to remove an alias.

The Starting Point

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Add the late fee"
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git log --oneline
Output
06617c2 (HEAD -> main) Add the hostel fee
1124e0b Add the late fee
6b651a0 Add the term fee

Example 1: Simple Aliases

Navika defines aliases for the commands she types most:

Terminal
git config --global alias.st "status -s"
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.last "log -1 --stat"
git config --global alias.unstage "restore --staged"
echo 'Exam fee: $800' >> fees.txt
git add fees.txt
git st
git unstage fees.txt
git st
git lg
Output
M  fees.txt
 M fees.txt
* 06617c2 (HEAD -> main) Add the hostel fee
* 1124e0b Add the late fee
* 6b651a0 Add the term fee

Explanation: each alias was replaced by its command before running: git st ran git status -s, git unstage fees.txt ran git restore --staged fees.txt (arguments after an alias are added to the end), and git lg drew the graph. Aliases also work with Git's tab completion in most shells.

To see what an alias means:

Terminal
git help lg
git config --get-regexp "^alias\."
Output
'lg' is aliased to 'log --oneline --graph --all'
alias.st status -s
alias.lg log --oneline --graph --all
alias.last log -1 --stat
alias.unstage restore --staged

Example 2: Shell Commands with !

An alias starting with ! runs as a shell command, so it can combine several commands or use other programs. Inside it, write git in full:

Terminal
git config --global alias.today '!git log --oneline --since=midnight --author="$(git config user.name)"'
git config --global alias.amend-all '!git add -A && git commit --amend --no-edit --quiet'
git amend-all
git last
Output
commit 6909a9a6214fdc8487fc62b0ca529df36ddd4b67 (HEAD -> main)
Author: Navika Anamaneni <navika@example.com>
Date:   Mon Jan 5 10:10:00 2026 -0600
 
    Add the hostel fee
 
 fees.txt | 2 ++
 1 file changed, 2 insertions(+)

Explanation: git amend-all ran two commands: it staged everything (the exam fee line) and amended the last commit with it, as git last confirms: "Add the hostel fee" now changes two lines. today would list your own commits since midnight. Shell aliases run from the top folder of the repository, whatever folder you are in.

How Git expands an alias

Example 3: Aliases with Arguments

Arguments after a shell alias are passed to the shell. The common pattern wraps the command in a small function, f, so that $1 is the first argument:

Terminal
git config --global alias.by '!f() { git log --oneline --author="$1"; }; f'
git by Advitha
Output
1124e0b Add the late fee

git by Advitha became f Advitha, and inside the function $1 is Advitha.

Aliases Can't Replace Git Commands

Git ignores an alias whose name is already a Git command, so that no setting can change what git log or git commit means for scripts and other people:

Terminal
git config --global alias.log "log --oneline"
git log -1
Output
commit 6909a9a6214fdc8487fc62b0ca529df36ddd4b67 (HEAD -> main)
Author: Navika Anamaneni <navika@example.com>
Date:   Mon Jan 5 10:10:00 2026 -0600
 
    Add the hostel fee

The full git log output appeared: the alias was ignored. Pick a new name, such as lg, instead.

A Useful Set of Aliases

AliasCommandUse
ststatus -sShort status
swswitchShorter switching
lglog --oneline --graph --allThe whole history at a glance
lastlog -1 --statWhat did the last commit change?
unstagerestore --stagedTake a file out of the staging area
undoreset --soft HEAD~1Undo the last commit, keep its changes staged
amendcommit --amend --no-editAdd staged changes to the last commit
pushfpush --force-with-lease --force-if-includesThe safe force push
branchesbranch -vv --sort=-committerdateBranches, most recently used first
wip!git add -A && git commit -m "WIP"Save work in progress quickly

Add them one at a time, as you notice which commands you type most.

Common Mistakes

Writing git inside a plain alias. alias.st "git status" runs git git status. Only ! aliases need the word git.

Expecting an alias to override a Git command. Git ignores it. Choose a different name.

Forgetting quotes. The whole command after the alias name must be one argument: git config --global alias.lg "log --oneline --graph".

Aliases nobody else understands, in shared instructions. Write the full commands in documentation and scripts. Aliases are personal.

Dangerous aliases. An alias for reset --hard or push --force makes destructive commands too easy to type by accident.

Under the Hood

Aliases are settings: git config --global alias.st "status -s" writes st = status -s under [alias] in ~/.gitconfig. When you run git st, Git first looks for a built-in command or a program called git-st on your PATH. Only if neither exists does it look up alias.st, splits its value into words, and runs the result with your extra arguments added. For a ! alias, it runs the value through the shell (sh -c) from the repository's top folder, with your arguments appended, which is why the f() { ...; }; f pattern can use them as $1, $2 and so on. Because built-in commands are checked first, an alias can never hide one.

Interview Corner

How do you create a Git alias?

git config --global alias.<name> "<command>", for example git config --global alias.lg "log --oneline --graph --all". Then git lg runs that command.

How do you make an alias that runs a shell command or several commands?

Start its value with !, for example git config --global alias.amend-all '!git add -A && git commit --amend --no-edit'. To use arguments, wrap the command in a function: '!f() { git log --author="$1"; }; f'.

Can an alias override a built-in Git command?

No. Git checks built-in commands first and ignores an alias with the same name.

How do you see what an alias does?

git help <alias> prints "'' is aliased to '...'", and git config --get-regexp "^alias\." lists all aliases.

Practice

Challenge: an undo alias. Create an alias undo for reset --soft HEAD~1. Use it once, then print the short status and the one-line log.

What does Git print? Before you open the solution, decide which commit disappears from the log, and what the status shows.

Show solution
Terminal
git config --global alias.undo "reset --soft HEAD~1"
git undo
git st
git log --oneline
Output
M  fees.txt
1124e0b (HEAD -> main) Add the late fee
6b651a0 Add the term fee

The last commit, "Add the hostel fee", left the branch, and its changes are staged again (M ), ready to be committed differently. The alias was simply replaced by git reset --soft HEAD~1, as "Git Reset" describes.

Checkpoint. Add five aliases you will really use, including one ! alias with an argument. Use them for a week, then remove any you didn't use with git config --global --unset alias.<name>.

Quick check: Git aliases

4 questions
1Which command creates the alias st for status -s?
2What does a ! at the start of an alias mean?
3You define alias.log "log --oneline". What does git log do?
4How do you see what the alias lg stands for?

Key Takeaways

Quick reference

CommandIt is used to
git config --global alias.<name> "<command>"Create an alias
git config --global alias.<name> "!<shell command>"Create a shell alias
git config --get-regexp "^alias\."List aliases
git help <alias>Show an alias's command
git config --global --unset alias.<name>Remove an alias

Was this tutorial helpful?

Lesson 78 of 106