Git Aliases & Config
Git aliases let you create custom shortcuts for commands you use often. Instead of typing git status every time, you can type git st.
Creating Aliases
Use git config to define aliases:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
Now git st is the same as git status.
Aliases for Complex Commands
Aliases can wrap longer commands:
# Pretty log with graph
git config --global alias.lg "log --oneline --graph --all --decorate"
# Show last commit
git config --global alias.last "log -1 HEAD"
# Unstage a file
git config --global alias.unstage "reset HEAD --"
# Amend without changing the message
git config --global alias.amend "commit --amend --no-edit"
# Show branches sorted by last commit
git config --global alias.recent "branch --sort=-committerdate"
Where Aliases Live
Aliases are stored in your Git config file:
git config --global --list # see all global settings
git config --local --list # see repo-specific settings
The global config is at ~/.gitconfig:
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --all --decorate
Try it!
Set up some useful aliases:
- Initialize a repo:
git init && echo "hello" > file.txt && git add file.txt && git commit -m "Init" - Create a status alias:
git config alias.st status - Test it:
git st - Create a log alias:
git config alias.lg "log --oneline --graph --all --decorate" - Add more commits:
echo "update" >> file.txt && git add file.txt && git commit -m "Update file" - Test the log alias:
git lg - Create an unstage alias:
git config alias.unstage "reset HEAD --" - Stage a change and unstage it:
echo "test" >> file.txt && git add file.txt && git unstage file.txt && git st
Shell Aliases vs Git Aliases
You can also create shell aliases for even shorter commands:
# In ~/.bashrc or ~/.zshrc
alias g="git"
alias gs="git status"
alias gc="git commit"
alias gp="git push"
With both, gs becomes a two-character git status.
Useful Config Settings
Beyond aliases, these config options improve your daily workflow:
# Set default branch name
git config --global init.defaultBranch main
# Enable color output
git config --global color.ui auto
# Set your editor
git config --global core.editor "code --wait"
# Auto-correct typos (with 1.5s delay)
git config --global help.autocorrect 15
# Reuse recorded conflict resolutions
git config --global rerere.enabled true
Checking Your Config
git config --list --show-origin # show all settings and where they come from
git config alias.st # show a specific setting