GitGit Aliases

Git Aliases

Git aliases let you create your own short names for long commands. After a few weeks of work you will find yourself typing the same eight-word commands over and over — aliases turn those into two or three letters. They are 100% personal, take a few minutes to set up, and pay back forever.

How aliases work

Aliases live in your Git config file under an [alias] section. Each alias maps a short name to a Git sub-command (or to a shell command). When you run git <alias>, Git substitutes the long form and runs it as if you had typed the whole thing.

Creating aliases

Add an alias from the command line

Bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit

After that, git st runs git status, git co main runs git checkout main, and so on.

Editing aliases in the config file

~/.gitconfig

Text
[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    sw = switch
    cm = commit -m
    cane = commit --amend --no-edit
    unstage = reset HEAD --

Editing the file directly is faster once you have a handful of aliases. Use git config --global --edit to open it.

Useful aliases to start with

Status, log, and inspection

Bash
git config --global alias.s "status -sb"
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.ll "log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.who "shortlog -s -n --no-merges"

Branching and switching

Bash
git config --global alias.brn "rev-parse --abbrev-ref HEAD"
git config --global alias.sw "switch"
git config --global alias.swn "switch -c"
git config --global alias.cleanup "!git branch --merged | grep -v '\*\|main\|master' | xargs -n 1 git branch -d"

Quick commits

Bash
git config --global alias.cm "commit -m"
git config --global alias.cane "commit --amend --no-edit"
git config --global alias.amend "commit --amend"
git config --global alias.wip "commit -am 'WIP'"

Undoing things

Bash
git config --global alias.unstage "reset HEAD --"
git config --global alias.discard "checkout --"
git config --global alias.uncommit "reset --soft HEAD~1"

Working with remotes

Bash
git config --global alias.pushf "push --force-with-lease"
git config --global alias.up "pull --rebase --autostash"
git config --global alias.sync "!git fetch --all --prune && git pull"
Aliases that call shell commands

Prefix an alias with ! and Git will pass it to your shell instead of treating it as a Git sub-command. This unlocks pipelines, multiple commands, and other shell features.

Shell-based aliases

Bash
# Open the repo on GitHub in the browser (macOS)
git config --global alias.web '!open $(git remote get-url origin | sed "s/git@github.com:/https:\/\/github.com\//;s/\.git$//")'

# Show files that have ever been removed
git config --global alias.deleted "!git log --diff-filter=D --summary | grep delete"

# Quickly amend, then force-push safely
git config --global alias.fix "!git commit --amend --no-edit && git push --force-with-lease"

# Show a count of commits per author
git config --global alias.contrib "!git shortlog -sn --all --no-merges"
Listing your aliases

See what's configured

Bash
git config --get-regexp '^alias\.'
# alias.st status
# alias.co checkout
# alias.br branch
A useful pretty log

One of the most popular aliases in the Git community is a prettier log. Pick your favourite:

A nice graph log

Bash
git config --global alias.lg "log --graph --pretty=format:'%C(yellow)%h%Creset -%C(red)%d%Creset %s %C(green)(%cr) %C(blue)<%an>%Creset' --abbrev-commit"

# Now: git lg
Removing an alias

Bash
git config --global --unset alias.st
Aliases are case-sensitive
`git ST` and `git st` are different. By convention, aliases are lowercase.
Tip
Start small. Add 3-5 aliases that match commands you already type often. After a week, see which feel natural and add more. Aliases you never use are clutter; aliases you use 50 times a day are pure gold.