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
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
[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
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
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
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
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
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
# 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
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
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
git config --global --unset alias.st