Configuration layers
Git reads config from multiple files in order: system, global (~/.gitconfig), local (.git/config), worktree (when enabled), and command-line (-c). Later layers override earlier. Inspect with git config --list --show-origin.
Conditional includes
Apply different settings per directory tree — different signing keys for work and personal, for example:
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig.work
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig.personal
[includeIf "onbranch:release/*"]
path = ~/.gitconfig.release
[includeIf "hasconfig:remote.*.url:[email protected]:work-org/**"]
path = ~/.gitconfig.work
onbranch (Git 2.23+) and hasconfig (Git 2.36+) extend the conditional vocabulary.
Aliases that compose
[alias]
co = checkout
cm = commit -v
st = status -sb
lg = log --graph --pretty=format:'%C(auto)%h %ad %s %d' --date=short
last = log -1 HEAD --stat
unstage = reset HEAD --
# Shell-out alias
sync = "!f() { git fetch --all --prune && git rebase --autostash @{u}; }; f"
publish = "!git push -u origin $(git symbolic-ref --short HEAD)"
Prefix with ! to run an arbitrary shell command, with the repo as cwd.
URL rewriting
Force HTTPS to SSH (or vice versa) for an organization:
[url "[email protected]:"]
insteadOf = https://github.com/
[url "https://github.com/"]
pushInsteadOf = [email protected]:
Per-worktree config
git config extensions.worktreeConfig true
git config --worktree user.email [email protected]
Sensible defaults
[core]
autocrlf = input
fsmonitor = true
untrackedCache = true
[merge]
conflictStyle = zdiff3
ff = false
[pull]
rebase = true
[rebase]
autoSquash = true
autoStash = true
updateRefs = true
[push]
autoSetupRemote = true
default = current
[fetch]
prune = true
parallel = 0
[diff]
algorithm = histogram
colorMoved = default
submodule = log
[init]
defaultBranch = main
[advice]
detachedHead = false
[feature]
manyFiles = true
Inspecting
git config --list --show-origin
git config --get-all alias.lg
git config --show-scope user.email
git -c user.email=other@x commit -m "ad-hoc identity"
Common mistakes
Setting credentials in committed files. Mixing pull.rebase=true with workflows that expect merges; align with your team. Forgetting that --global writes to ~/.gitconfig while --system writes machine-wide and may need root.
Related
See "Multiple remote workflows", "GPG signing commits and tags", and "SSH signing (Git 2.34+)".