This guide shows you how to add custom Git aliases to your dotfiles configuration. Use this approach when you want to create shortcuts for frequently used Git commands.
Ensure you have:
~/.dotfiles
stow git
Use this approach for aliases you want available everywhere.
cd ~/.dotfiles
$EDITOR git/.gitconfig
Find the [alias]
section (around line 13)
[alias]
# Existing aliases...
# Your new alias
myalias = log --oneline --graph --all
Use this for quick additions without editing files.
git config --global alias.myalias "log --oneline --graph --all"
The alias is added to ~/.gitconfig
(which is symlinked to your dotfiles)
cd ~/.dotfiles
git add git/.gitconfig
git commit -m "feat: add 'myalias' git alias"
For aliases that need shell features, use a function:
[alias]
section:
[alias]
# Complex alias using shell
branch-clean = "!f() { \
git branch --merged | \
grep -v '\*\|main\|master' | \
xargs -n 1 git branch -d; \
}; f"
!
prefix indicates a shell commandTest your new alias:
# Simple alias
git myalias
# List all aliases
git config --get-regexp alias
# Check specific alias
git config alias.myalias
# Status shortcuts
st = status --short --branch
s = status
# Commit shortcuts
ci = commit
cm = commit -m
amend = commit --amend --no-edit
# Branch management
br = branch
co = checkout
cob = checkout -b
# Log variations
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
last = log -1 HEAD
# Workflow helpers
unstage = reset HEAD --
discard = checkout --
undo = reset --soft HEAD~1
# Information
aliases = config --get-regexp '^alias\.'
contribs = shortlog --summary --numbered
Problem: Alias doesn’t work
Solution: Check for typos and ensure there are no spaces around the =
sign
Problem: Shell features not working
Solution: Add !
prefix for shell commands: myalias = !echo hello
Problem: Alias conflicts with git command
Solution: Git aliases cannot override built-in commands. Choose a different name.
Problem: Changes not persisting
Solution: Ensure you’re editing ~/.dotfiles/git/.gitconfig
, not ~/.gitconfig
directly
graph
better than gr