GitGit Configuration (git config)

Git Configuration (git config)

git config is the command you use to tell Git about yourself, your preferences, and your aliases. Settings can apply at three levels: system-wide, per-user, or per-repository. Understanding these three levels and where they live on disk turns Git from a rigid tool into one tailored to how you actually work.

The three config levels

Level

Scope

File location

Flag

system

All users on this machine

/etc/gitconfig (Linux/macOS) or Program Files\Git\etc\gitconfig (Windows)

--system

global

All repos for the current user

~/.gitconfig or ~/.config/git/config

--global

local

The current repository only

.git/config inside the repo

--local (default)

Lower levels override higher ones — a setting in local overrides the same setting in global, which overrides system. So you can set a sensible default globally and override it for one specific project.

The essential first-time setup

Minimum config for a new machine

Bash
# Identity — every commit is tagged with these
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Default branch name for new repos
git config --global init.defaultBranch main

# Editor used for commit messages
git config --global core.editor "code --wait"   # VS Code
# Or: "nano" / "vim" / "subl -w" / "atom --wait"

# Default 'pull' behaviour — explicit merge
git config --global pull.rebase false
Inspecting your config

See what's set

Bash
# List every effective setting and where it comes from
git config --list --show-origin

# Just see a single value
git config user.email

# What is set at each level?
git config --system --list
git config --global --list
git config --local  --list   # must be inside a repo
Setting and unsetting

Set and remove settings

Bash
# Set a value
git config --global core.pager "less -FRX"

# Add another value to a multi-valued setting
git config --global --add safe.directory "/path/to/repo"

# Remove a setting
git config --global --unset core.pager

# Open the config file in your editor for bulk edits
git config --global --edit
Useful settings you probably want

Quality-of-life defaults

Bash
# Coloured output
git config --global color.ui auto

# Better diff algorithm
git config --global diff.algorithm histogram

# Push only the current branch
git config --global push.default current

# When pushing a new branch, also set it up to track upstream
git config --global push.autoSetupRemote true

# Reuse recorded merge resolutions (saves you on repeated rebases)
git config --global rerere.enabled true

# Prune deleted remote branches on fetch
git config --global fetch.prune true

# Sort branches by most recently used
git config --global branch.sort -committerdate

# Show file changes in 'git log -p' more cleanly
git config --global diff.renames true
Cross-platform: line endings

core.autocrlf

Bash
# Windows — convert CRLF in working tree, LF in repo
git config --global core.autocrlf true

# macOS / Linux — never write CRLF in repo
git config --global core.autocrlf input

# Don't convert at all (use .gitattributes instead)
git config --global core.autocrlf false
Credentials and authentication

Credential helpers

Bash
# Cache credentials in memory for 15 minutes
git config --global credential.helper cache

# Cache for a custom duration (here: 1 hour)
git config --global credential.helper "cache --timeout=3600"

# Use the OS keychain (recommended)
# macOS:
git config --global credential.helper osxkeychain
# Windows:
git config --global credential.helper manager
# Linux (GNOME):
git config --global credential.helper "/usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret"
Aliases (short for long commands)

Common aliases

Bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"

# Now you can type:
git st
git lg
git last
Per-project overrides

Sometimes a single repository needs different settings — for example, you use a work email at the office and a personal email for open source.

Override the global email for one repo

Bash
cd ~/work/big-corp-project
git config user.email "you@bigcorp.com"
# This writes to .git/config inside the repo
# Outside this repo, your global email is still used
Conditional includes

Git can include different config files based on path. Useful if all your work repos live under ~/work and personal under ~/personal:

~/.gitconfig

Text
[user]
    name = Your Name
    email = personal@example.com

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal

~/.gitconfig-work

Text
[user]
    email = you@bigcorp.com
    signingkey = ABCD1234EFGH5678
The raw file format

Git config files are plain INI:

Example ~/.gitconfig

Text
[user]
    name = Your Name
    email = you@example.com

[core]
    editor = code --wait
    autocrlf = input

[init]
    defaultBranch = main

[alias]
    st = status
    co = checkout
    lg = log --oneline --graph --decorate --all

You can edit this file directly in your favourite editor. git config is just a convenience wrapper that writes the same format.

Where is my global config?
Most systems use ~/.gitconfig. Some XDG-respecting systems use ~/.config/git/config. Run git config --global --edit — it opens whichever one Git is actually reading.
Warning
Never commit a `.gitconfig` with credentials, signing keys, or emails you do not want public. Keep that data in your home directory, not inside repositories.
Tip
After you set up a new machine, write your favourite settings into a `dotfiles` repository on GitHub. Then on every new computer you can pull and symlink your config in one minute.