Git Aliases and Config: Speeding Up Your Workflow
Aliases and config settings remove friction from daily Git use. Learn the useful ones and how to set them up.
By Velloreth
Aliases
An alias is a shortcut for a command.
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 runs git status.
With arguments:
git config --global alias.lg "log --oneline --graph --decorate --all"
git lg runs the full log command.
With a shell command:
An alias starting with ! runs as a shell command.
git config --global alias.root '!pwd'
git config --global alias.undo '!git reset --soft HEAD~1'
The shell form allows pipelines and arguments.
git config --global alias.branches '!git branch --sort=-committerdate | head -10'
Useful aliases
Status and log:
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.today "log --since=midnight --oneline"
Branching:
git config --global alias.br "branch -vv"
git config --global alias.recent "!git branch --sort=-committerdate | head -10"
git config --global alias.cleanup "!git branch --merged | grep -v '\\*\\|main\\|develop' | xargs -n 1 git branch -d"
Undo:
git config --global alias.unstage "restore --staged"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.amend "commit --amend --no-edit"
git config --global alias.wip "!git add -A && git commit -m 'wip'"
Diff:
git config --global alias.df "diff --stat"
git config --global alias.dfc "diff --cached"
git config --global alias.wdiff "diff --word-diff"
Stash:
git config --global alias.sl "stash list"
git config --global alias.sp "stash pop"
git config --global alias.ss "stash push -m"
Misc:
git config --global alias.aliases "config --get-regexp '^alias\\.'"
git config --global alias.ignore "!git check-ignore -v"
git config --global alias.contributors "shortlog -sn --all --no-merges"
Useful config settings
Pull with rebase:
git config --global pull.rebase true
Avoids a merge commit on every pull.
Rebase autosquash:
git config --global rebase.autosquash true
Automatically reorders fixup! and squash! commits in an interactive rebase.
Reuse recorded resolution:
git config --global rerere.enabled true
Records how you resolved a conflict and replays it when the same conflict appears again. Invaluable during a long rebase.
Better diff algorithm:
git config --global diff.algorithm histogram
Produces more readable diffs than the default.
Colour and pager:
git config --global color.ui auto
git config --global core.pager "less -FRX"
The -F exits if the output fits on one screen, and -X keeps the output on screen.
Default branch name:
git config --global init.defaultBranch main
Push only the current branch:
git config --global push.default current
Prune on fetch:
git config --global fetch.prune true
Removes remote-tracking branches that no longer exist on the remote.
Auto-stash on rebase:
git config --global rebase.autoStash true
Stashes uncommitted changes before a rebase and restores them after.
Sign commits:
git config --global commit.gpgsign true
git config --global user.signingkey <key-id>
Case sensitivity:
git config --global core.ignorecase false
Useful on macOS, where the filesystem is case-insensitive by default, to catch case-only renames.
Where config lives
| Scope | File | Applies to |
|---|---|---|
| System | /etc/gitconfig | All users |
| Global | ~/.gitconfig | One user |
| Local | .git/config | One repository |
| Worktree | .git/config.worktree | One worktree |
Later scopes override earlier ones. A local setting beats a global one.
Set a value:
git config --global user.name "Your Name"
git config --local user.email "work@example.com"
Read a value:
git config --get user.name
git config --list --show-origin
--show-origin prints which file each setting came from, which is the fastest way to find where a value is set.
Edit directly:
git config --global --edit
Conditional config
Different settings for different directories.
# ~/.gitconfig
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
# ~/.gitconfig-work
[user]
email = work@example.com
This lets you use a work email in work repositories and a personal email elsewhere, without changing anything manually.
Sharing aliases
Aliases are personal. They belong in a dotfiles repository, not the project.
# In your dotfiles repo
ln -s ~/dotfiles/.gitconfig ~/.gitconfig
For project-specific settings, use the repository config, but keep it to things the whole team needs, such as a merge driver or a diff setting.
A starter config
A reasonable starting point for a new machine.
[user]
name = Your Name
email = you@example.com
[init]
defaultBranch = main
[pull]
rebase = true
[push]
default = current
autoSetupRemote = true
[fetch]
prune = true
[rebase]
autosquash = true
autoStash = true
[rerere]
enabled = true
[diff]
algorithm = histogram
colorMoved = default
[merge]
conflictstyle = zdiff3
[core]
pager = less -FRX
editor = nvim
[color]
ui = auto
[alias]
st = status -sb
co = checkout
br = branch -vv
lg = log --oneline --graph --decorate --all
last = log -1 HEAD --stat
unstage = restore --staged
undo = reset --soft HEAD~1
amend = commit --amend --no-edit
df = diff --stat
sl = stash list
sp = stash pop
aliases = config --get-regexp ^alias\\.
A few of these deserve a note.
merge.conflictstyle = zdiff3 shows the common ancestor in a conflict, which makes it much easier to see what each side changed. The default shows only the two sides, which leaves you guessing at the original.
diff.colorMoved = default highlights moved lines differently from added and removed ones, which makes a refactor diff readable.
push.autoSetupRemote = true sets the upstream automatically on the first push of a new branch, which removes a common error message.
rebase.autoStash = true stashes uncommitted changes before a rebase and restores them after, so a rebase does not fail because of a dirty working tree.
Credentials and security
Git config is also where credentials live, and the choices matter.
Never put a token in a remote URL. A URL like https://token@github.com/user/repo.git is stored in .git/config in plain text, and it leaks the moment the file is shared or the repository is copied.
# Bad: the token is in .git/config
git remote add origin https://ghp_xxx@github.com/user/repo.git
Use a credential helper. It stores credentials in the OS keychain rather than a file.
git config --global credential.helper osxkeychain # macOS
git config --global credential.helper manager # Windows
git config --global credential.helper 'cache --timeout=3600' # in-memory
Use SSH keys for authentication. An SSH remote has no token in the URL at all.
git remote set-url origin git@github.com:user/repo.git
Check for leaked credentials. If a token was ever in a remote URL, it is in .git/config and possibly in the shell history. Rotate the token and scrub the remote.
git remote -v
git remote set-url origin https://github.com/user/repo.git
Use includeIf for work and personal. It keeps the right email on the right repository without manual switching, which prevents committing a work change with a personal address.
Config for teams
Some settings belong in the repository config, because the whole team needs them.
A merge driver. A custom driver for a file type that needs special merge handling.
# .gitattributes
*.lock merge=ours
A diff driver. A custom diff for a binary or generated file.
Line endings. A .gitattributes file normalises line endings across platforms.
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
This is the right place for cross-platform settings, because it is committed and applies to everyone.
What does not belong. Personal aliases, editor preferences, and credential helpers are per-developer. Putting them in the repository config forces them on everyone, and it causes conflicts when two people disagree.
The split: .gitattributes and a small repository config for things the code needs, and a global config for personal preferences.
Debugging config problems
When Git behaves unexpectedly, the config is a common cause.
Find where a setting comes from:
git config --list --show-origin
This prints every setting with the file it came from, which immediately shows whether a value is global, local, or from an included file.
Check a single value:
git config --get pull.rebase
git config --get --default "not set" pull.rebase
See the effective config for a repository:
git config --list --local
A setting that seems ignored. The most common cause is a more specific scope overriding it. A local setting beats a global one, so a value in .git/config wins over ~/.gitconfig.
An alias that does not work. Check the quoting. An alias with spaces needs quotes around the whole value, and an alias with a shell command needs the ! prefix.
git config --global alias.lg "log --oneline --graph"
A conditional include that does not apply. The gitdir pattern must match the repository path, and it needs a trailing slash to match a directory.
Practical guidance
- Alias the commands you type most.
- Use
!for shell commands in aliases. - Set
pull.rebase trueto avoid merge commits. - Enable
rererefor long rebases. - Use
diff.algorithm histogramfor readable diffs. - Use
includeIffor work and personal emails. - Keep aliases in dotfiles, not the project.
- Use
--show-originto find where a setting comes from.
For the related guides, see git-commit-messages-guide and git-undo-changes-guide. For the broader context, git-hooks-guide and git-branching-strategies.
Quotable summary
A Git alias is a shortcut for a command, defined with git config —global alias.