Sub topic 13
Keeping your configuration portable
This chapter assumes you already have a working ~/.gitconfig with aliases and settings, and that you know the scopes (system, global, local) and how includeIf works. The goal is to carry that setup to a new laptop or server without copying files by hand and without leaking anything that belongs to one machine.
Put the global file under version control
Your global config is a plain text file. Store it in a repository, usually called a dotfiles repository, and link it into place.
mkdir -p ~/dotfiles/git
mv ~/.gitconfig ~/dotfiles/git/gitconfig
ln -s ~/dotfiles/git/gitconfig ~/.gitconfig
cd ~/dotfiles
git init
git add git/gitconfig
git commit -m "Add Git configuration"On a new machine, clone the repository and run the ln -s line again. Git also reads ~/.config/git/config when it exists. Some people keep that directory in the repository instead, which avoids a dotfile at the top of the home directory.
flowchart TDSplit shared settings from machine-specific ones
Some values should differ per machine: credential helpers, paths to editors or signing keys, and the work email. Keep them out of the shared file. Use an [include] section at the end that points to an untracked local file.
# ~/dotfiles/git/gitconfig (shared, tracked)
[user]
name = Ada Lovelace
email = [email protected]
[alias]
st = status -sb
lg = log --graph --oneline --decorate --all
[pull]
rebase = true
# Keep this last so machine-specific values win
[include]
path = ~/.gitconfig.local# ~/.gitconfig.local (not tracked)
[credential]
helper = osxkeychain
[core]
editor = code --waitGit reads the file from top to bottom, and a later value replaces an earlier one. An include behaves as if the included text sat at that line. That is why the include goes at the bottom. A missing include file is silently skipped, so the shared file works on a machine where no local file exists yet.
Relative include paths are resolved against the directory of the file that contains the include, not against your current directory.
What not to commit
- Access tokens, passwords and anything inside a
credentialURL. Use a credential helper instead. - Signing key IDs and SSH key paths that only exist on one machine.
- Absolute paths such as
/Users/ada/.... Use~or put the setting in the local file.
If a dotfiles repository is public, assume everyone can read your email address and your alias history.
Account for differences between machines
A config that works on your laptop can misbehave elsewhere.
- Git version. Settings such as
merge.conflictStyle = zdiff3andpush.autoSetupRemoteneed fairly recent Git releases (2.35 and 2.37). Checkgit --versionon servers. An older Git may ignore a key it does not know, so a missing effect is not proof the file was read. - Operating system. Credential helpers and line-ending options such as
core.autocrlfdiffer between Windows, macOS and Linux. Keep them in the local file. - Shell tools in aliases. An alias like
cleanup, which pipes throughgrepandxargs, assumes those tools exist. On Windows it runs in Git's bundled shell, which usually has them. Test shell aliases on each platform you use.
Verify a new machine
After linking the file, confirm that Git reads what you expect.
git --version
git config --list --show-origin
git config --get-regexp '^alias\.'The --show-origin output should show your symlinked file and, if present, the local file. If an alias is missing, the origin column tells you which file Git actually loaded.
Exercise
Your shared config sets user.email = [email protected], and its last lines are [include] path = ~/.gitconfig.local. On a work laptop the local file contains [user] email = [email protected]. Which address does Git use outside any includeIf directory, and what changes if you move the [include] section to the top of the shared file?
Show answer
Git uses [email protected], because the include comes last and the later value wins. If the include moves to the top, the shared file's user.email = [email protected] is read afterwards and overrides the local one, so Git would use the personal address.