Git’s defaults are cautious, chosen decades ago so nothing surprises anyone. A few lines of configuration make it faster, clearer and safer to use. This lesson shows how configuration works, how to build aliases for the commands you type all day and which settings experienced users turn on.
Reading and writing configuration
Lesson 2 introduced the three levels: system, global (~/.gitconfig) and local (.git/config). Since Git 2.46 there are clear subcommands for working with them:
$ git config set --global core.editor "code --wait"
$ git config get core.editor
code --wait
$ git config list --show-origin --show-scope
$ git config unset --global core.editor
$ git config edit --global # open ~/.gitconfig in your editor
The older forms (git config --global core.editor ..., git config --get, git config --list, --unset) still work and appear in most guides. The configuration file itself is plain text in INI style, and editing it by hand is perfectly fine:
[user]
name = Ana Souza
email = ana@orbit.dev
[core]
editor = code --wait
Aliases
An alias is a short name for a command. Define it once, use it everywhere:
$ git config set --global alias.st "status -sb"
$ git config set --global alias.sw switch
$ git config set --global alias.lg "log --oneline --graph --decorate -20"
$ git config set --global alias.last "log -1 --stat"
$ git config set --global alias.unstage "restore --staged"
$ git st
## main...origin/main
Some aliases the Orbit team shares:
| Alias | Expands to | Use |
|---|---|---|
git st | status -sb | Compact status with branch and ahead/behind |
git lg | log --oneline --graph --decorate -20 | Recent history as a graph |
git last | log -1 --stat | What did I just commit? |
git unstage | restore --staged | Undo a git add |
git amend | commit --amend --no-edit | Add staged changes to the last commit |
git wip | commit -am "wip" --no-verify | Quick save before switching tasks |
Shell aliases
An alias that starts with ! runs a shell command, so it can chain several Git commands:
[alias]
# update main and come back
up = "!git switch main && git pull --ff-only && git switch -"
# list branches by most recent commit
recent = "!git for-each-ref --sort=-committerdate --count=10 --format='%(committerdate:relative)%09%(refname:short)' refs/heads"
Shell aliases run from the top of the repository. Keep them small; anything bigger belongs in a script on your PATH named git-something, which Git then runs as git something.
Conditional includes: work and personal identities
includeIf loads a file only for repositories in a certain folder. It is the clean way to commit with your company email at work and your own email everywhere else:
# ~/.gitconfig
[user]
name = Ana Souza
email = ana@orbit.dev
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# ~/.gitconfig-work
[user]
email = ana.souza@company.example
signingkey = ~/.ssh/id_ed25519_work.pub
Every repository under ~/work/ now uses the work identity, without any per-repository setup. The trailing slash in gitdir:~/work/ matters: it matches everything below that folder. hasconfig:remote.*.url: can match by remote URL instead of folder.
Settings worth turning on
| Setting | Value | Why |
|---|---|---|
init.defaultBranch | main | Matches hosting services and Git 3.0 |
pull.rebase | true | Linear history on pull, no merge commits for local work |
push.autoSetupRemote | true | First push of a new branch sets its upstream |
push.followTags | true | Push annotated tags along with commits |
fetch.prune | true | Forget remote branches deleted on the server |
rebase.autoSquash | true | Apply fixup! commits automatically (lesson 6) |
rebase.updateRefs | true | Move stacked branches during a rebase |
merge.conflictStyle | zdiff3 | Show the original lines inside conflicts |
rerere.enabled | true | Reuse recorded conflict resolutions |
diff.algorithm | histogram | Clearer diffs for moved code |
diff.colorMoved | default | Color lines that moved instead of changed |
branch.sort | -committerdate | Most recently used branches first |
help.autocorrect | prompt | Offer to run the command you probably meant |
Git 2.54 added status.compareBranches, which makes git status report ahead and behind for more than one branch, for example both the upstream and the branch you push to when they differ, as in a fork workflow:
$ git config set --global status.compareBranches "@{upstream} @{push}"
Faster status in large repositories
In a working tree with tens of thousands of files, git status spends most of its time checking every file for changes. The built-in file system monitor asks the operating system which files changed instead. It has existed for Windows and macOS since Git 2.37; Git 2.55 added Linux support, using inotify:
$ git config set core.fsmonitor true
$ git config set core.untrackedCache true
$ git fsmonitor--daemon status
On Linux, very large repositories may need a higher fs.inotify.max_user_watches limit, because the monitor uses one watch per directory. Repositories on network drives are not monitored unless you opt in. For a quick all-in-one setup of large repositories, scalar register turns on the recommended performance settings and background maintenance.
A starter .gitconfig
[user]
name = Ana Souza
email = ana@orbit.dev
[init]
defaultBranch = main
[core]
editor = code --wait
[pull]
rebase = true
[push]
autoSetupRemote = true
followTags = true
[fetch]
prune = true
[rebase]
autoSquash = true
updateRefs = true
[merge]
conflictStyle = zdiff3
[rerere]
enabled = true
[diff]
algorithm = histogram
colorMoved = default
[branch]
sort = -committerdate
[alias]
st = status -sb
lg = log --oneline --graph --decorate -20
last = log -1 --stat
unstage = restore --staged
amend = commit --amend --no-edit
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
Keep your .gitconfig in a dotfiles repository so every new machine is set up in a minute.
Quick reference
| Task | Command |
|---|---|
| Set a value | git config set [--global] <key> <value> |
| Read a value | git config get <key> |
| List with origins | git config list --show-origin --show-scope |
| Remove a value | git config unset [--global] <key> |
| Edit the file | git config edit --global |
| Create an alias | git config set --global alias.<name> "<command>" |
| Identity by folder | [includeIf "gitdir:~/work/"] in ~/.gitconfig |
| Faster status | core.fsmonitor true, core.untrackedCache true |
Exercise
You want every repository under ~/work/ to use your company email automatically, and your personal email elsewhere. What do you use?
Show answer
Answer: C. includeIf loads extra configuration only for repositories inside the given folder, so the work email applies there and your global default applies everywhere else.
Exercise
You define alias.up as “!git switch main && git pull –ff-only”. What does the leading ! mean?
Show answer
Answer: B. An alias starting with ! is executed as a shell command, which allows && chains, pipes and other programs. Without it, the alias text is appended to git as arguments of a single command.
Exercise
git status takes several seconds in a monorepo with 200,000 files on Linux, using Git 2.55. Which setting helps most?
Show answer
Answer: A. The file system monitor lets Git ask the operating system which files changed instead of scanning every file. Git 2.55 added it on Linux; it already existed on Windows and macOS.
Key takeaways
- Configuration lives at system, global and local level;
git config set,get,list,unsetandeditmanage it. - Aliases shorten everyday commands; a leading
!runs a shell command, andgit-<name>scripts becomegit <name>. includeIfswitches identity and settings by folder or remote.- A handful of settings (
zdiff3,rerere,autoSetupRemote,fetch.prune,histogram) make Git clearer and safer. core.fsmonitor, now on Linux too (2.55), andscalarspeed up large repositories.
FAQ
Can an alias have the same name as a Git command?
No. Built-in commands always win, so an alias named status is ignored. Choose a different name such as st.
How do I see what an alias does?
Run git config get alias.lg, or git help lg, which prints what the alias expands to.
Are the new git config subcommands required?
No. git config set and friends arrived in Git 2.46 and are clearer, but the old options keep working. Scripts that must run on older Git versions should use the classic forms.
Where should I put settings that only apply to one project?
In that repository’s .git/config, with git config set without --global. Settings the whole team needs, such as line endings, belong in committed files like .gitattributes instead, because .git/config is not shared.
Is it safe to copy someone’s .gitconfig?
Read it first. Shell aliases and some settings, such as credential helpers or core.sshCommand, run programs. Take the settings you understand, one by one.