Git Aliases: Shortcuts, Shell Aliases and a Useful Collection
Create your own Git commands with aliases: simple shortcuts, aliases that combine options, shell aliases with ! and how they handle arguments and folders, functions for complex ones, listing and debugging aliases, and a collection of useful aliases explained one by one.
kvi5hnuCode Sprout team member5 min readOn this page
An alias is a name you define for a Git command, with any options you like. git st can mean git status --short --branch, and git undo can mean git reset --soft HEAD~1. Aliases live in your configuration, so they follow you to every repository, and they're the easiest way to make long commands you type daily short.
Simple aliases
git config --global alias.st "status --short --branch"
git config --global alias.last "log -1 --format='%h %s (%an, %ad)' --date=short"
git config --global alias.unstage "restore --staged"
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge" > ridge.txt; git add ridge.txt
git st
git unstage ridge.txt
git st
git last
echo "=== the alias, in the configuration file"
git config get --all --show-names --regexp '^alias\.'
## main
A ridge.txt
## main
?? ridge.txt
cf8c779 Add lake page (Sam Lee, 2026-03-02)
=== the alias, in the configuration file
alias.st status --short --branch
alias.last log -1 --format='%h %s (%an, %ad)' --date=short
alias.unstage restore --staged
git config --global alias.<name> "<command and options>"defines one, without the leadinggit. In~/.gitconfigit appears under[alias].- Arguments are appended:
git unstage ridge.txtrunsgit restore --staged ridge.txt. - An alias can use another alias.
- An alias can't override a built-in command:
alias.logis ignored, andgit logstill runs Git's own. That keeps scripts predictable. git help <alias>shows what it expands to:'st' is aliased to 'status --short --branch'.
Shell aliases with !
Starting the value with ! runs it as a shell command instead of a Git subcommand. That allows pipes, several commands, and other programs:
[alias]
# every alias, neatly
aliases = !git config get --all --show-names --regexp '^alias\\.' | sed 's/^alias\\.//'
# stage everything and commit with a message
save = !git add -A && git commit -m
# the folder at the top of the repository
root = rev-parse --show-toplevel
Two behaviours to know:
- Shell aliases run from the top of the repository, not your current folder. The original folder is in the variable
GIT_PREFIX, so usecd "${GIT_PREFIX:-.}"when paths matter. - Arguments are appended to the end of the command. In
save = !git add -A && git commit -m,git save "Fix typo"becomesgit add -A && git commit -m "Fix typo", which works. But to use an argument in the middle of a command, or more than once, wrap the command in a shell function.
Functions for arguments
[alias]
# git ignore "*.log" -> appends a pattern to .gitignore
ignore = "!f() { echo \"$1\" >> .gitignore; }; f"
# git find-commit "text" -> commits whose message mentions text, on all branches
find-commit = "!f() { git log --all --oneline --grep=\"$1\"; }; f"
The function f receives the alias's arguments as $1, $2 and so on, and "$@" for all of them. Because Git appends the arguments to the end of the command line, they're passed to f and used only where you reference them.
git config --global alias.ignore '!f() { echo "$1" >> .gitignore; }; f'
git config --global alias.here '!echo "running in: $(pwd | sed "s|.*/||") called from: ${GIT_PREFIX:-(top)}"'
git config --global alias.find-commit '!f() { git log --all --format="%h %s" --grep="$1"; }; f'
git init -q trail-guide && cd trail-guide
mkdir -p docs/guides
git commit -q --allow-empty -m "Add lake page"
git commit -q --allow-empty -m "Fix lake distance"
cd docs/guides
git here
git ignore "*.log"
cat "$(git rev-parse --show-toplevel)/.gitignore"
git find-commit lake
running in: trail-guide called from: docs/guides/
*.log
1c6974c Fix lake distance
d7b060a Add lake page
git here ran at the top of the repository even though it was called from docs/guides, so git ignore wrote the top-level .gitignore, which is usually what you want for that one.
Quoting is the trickiest part of shell aliases. In git config commands, wrap the value in single quotes. In the file, wrap it in double quotes and escape inner double quotes as \" and backslashes as \\. When an alias grows beyond a line or two, write a script instead: an executable file called git-<name> anywhere on your PATH becomes git <name>, with no quoting problems, and works in every repository too.
Debugging aliases
git help st # 'st' is aliased to 'status --short --branch'
GIT_TRACE=1 git st # shows the expansion and the command actually run
git config get --all --show-names --regexp '^alias\.'
A useful collection
These aliases are widely used. Pick the ones that match how you work:
[alias]
# status, short with branch info
st = status --short --branch
# compact graph of all branches
lg = log --graph --format='%C(auto)%h%d %s %C(dim)(%ar, %an)' --all
# the last commit, in full
last = log -1 --stat
# commits on this branch not yet on its upstream
outgoing = log --oneline @{u}..
# commits on the upstream not yet here (after a fetch)
incoming = log --oneline ..@{u}
# undo the last commit, keeping its changes staged
undo = reset --soft HEAD~1
# unstage files
unstage = restore --staged
# amend the last commit without changing its message
amend = commit --amend --no-edit
# a fixup commit for an earlier commit: git fixup <commit>
fixup = commit --fixup
# force push safely
pushf = push --force-with-lease --force-if-includes
# recently used branches, newest first
recent = branch --sort=-committerdate --format='%(committerdate:relative)%09%(refname:short)'
# what's staged, word by word
dw = diff --staged --word-diff
# list aliases
aliases = !git config get --all --show-names --regexp '^alias\\.' | sed 's/^alias\\.//'
# delete local branches already merged into the current branch
tidy = "!git branch --merged | grep -v -E '^[*+]|^ *(main|master|develop)$' | xargs -r git branch -d"
Some notes on them:
lguses%C(auto)so branch names and IDs get their usual colours, and%dshows branch and tag names (article 9).outgoingandincominguse the upstream syntax from article 29.fixupcreatesfixup!commits, whichgit rebase -i --autosquashfolds in automatically (article 15).tidydeletes only branches Git can prove are merged, because it uses-d(article 23).
Many shells also let you define shell aliases for Git, such as alias gs='git status' in ~/.bashrc or ~/.zshrc. Frameworks like Oh My Zsh ship dozens. Git aliases have the advantage of working in every shell and on every machine where your .gitconfig goes.
What's current
- Since Git 2.54, alias names may include characters outside ASCII letters, digits and
-, using a subsection:[alias "名前"]withcommand = status --short. git config get --regexpwith--show-names(Git 2.46) replacesgit config --get-regexpfor listing aliases.- Tab completion completes alias names, and for simple aliases also the options of the command they expand to (article 37).
Common questions
Can I make git log always use my favourite options?
Not with an alias named log, because built-in names can't be overridden. Use configuration where it exists, such as log.date, log.decorate and format.pretty. For options with no setting, such as --graph, define a new alias like git lg.
Why does my shell alias work on the wrong files?
Shell aliases (!) run from the top of the repository. Paths relative to your current folder need cd "${GIT_PREFIX:-.}" first, or use absolute paths.
How do I pass arguments into the middle of an alias?
Wrap the command in a function: "!f() { git log --author=\"$1\" --since=\"$2\"; }; f", then git mine Ana 1.week.
Where should a long, complicated alias go?
In a script named git-<name> on your PATH. Git runs it as git <name>, and you can write it in any language, with proper quoting and comments.
Summary
git config --global alias.<name> "<command>"creates a shortcut, and arguments are appended.- Aliases can't replace built-in commands, but can call other aliases.
!makes a shell alias. It runs from the repository's top folder, with the original folder inGIT_PREFIX.- Use
"!f() { ...; }; f"to place arguments anywhere, and agit-<name>script for anything long. git help <alias>andGIT_TRACE=1show what an alias does.
Practice questions
Create an alias git graph that shows a one-line graph of all branches.
Answer
git config --global alias.graph "log --oneline --graph --all --decorate". Then git graph runs git log --oneline --graph --all --decorate, and extra arguments are appended, such as git graph -10.
Your alias count = !wc -l *.txt reports files from the top of the repository, even when you run it from a subfolder. Why, and how do you fix it?
Answer
Shell aliases run from the repository's top folder. Git puts the folder you called it from in GIT_PREFIX, so change into it first: count = "!cd \"${GIT_PREFIX:-.}\" && wc -l *.txt".
You want git mine 2.weeks to list your own commits from the last two weeks. Write the alias.
Answer
[alias]
mine = "!f() { git log --oneline --author=\"$(git config get user.email)\" --since=\"${1:-1.week}\"; }; f"
The function reads the first argument for --since, defaulting to one week, and filters by your configured email.
Previous: Git Configuration: Levels, Conditional Includes, One-Off Settings and Useful Defaults
Next: Line Endings and .gitattributes: autocrlf, eol, Binary Files and Diff Drivers

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.