Lesson 15 intermediate

Git Aliases and Configuration

Make Git yours: the git config commands, aliases for everyday commands and shell aliases, conditional includes for work and personal identities, settings that improve diffs, merges and pushes, status.compareBranches (Git 2.54), the file system monitor on Linux (Git 2.55) and a starter .gitconfig.

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:

AliasExpands toUse
git ststatus -sbCompact status with branch and ahead/behind
git lglog --oneline --graph --decorate -20Recent history as a graph
git lastlog -1 --statWhat did I just commit?
git unstagerestore --stagedUndo a git add
git amendcommit --amend --no-editAdd staged changes to the last commit
git wipcommit -am "wip" --no-verifyQuick 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

SettingValueWhy
init.defaultBranchmainMatches hosting services and Git 3.0
pull.rebasetrueLinear history on pull, no merge commits for local work
push.autoSetupRemotetrueFirst push of a new branch sets its upstream
push.followTagstruePush annotated tags along with commits
fetch.prunetrueForget remote branches deleted on the server
rebase.autoSquashtrueApply fixup! commits automatically (lesson 6)
rebase.updateRefstrueMove stacked branches during a rebase
merge.conflictStylezdiff3Show the original lines inside conflicts
rerere.enabledtrueReuse recorded conflict resolutions
diff.algorithmhistogramClearer diffs for moved code
diff.colorMoveddefaultColor lines that moved instead of changed
branch.sort-committerdateMost recently used branches first
help.autocorrectpromptOffer 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

TaskCommand
Set a valuegit config set [--global] <key> <value>
Read a valuegit config get <key>
List with originsgit config list --show-origin --show-scope
Remove a valuegit config unset [--global] <key>
Edit the filegit config edit --global
Create an aliasgit config set --global alias.<name> "<command>"
Identity by folder[includeIf "gitdir:~/work/"] in ~/.gitconfig
Faster statuscore.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?

  1. An alias that runs git config user.email in each repository
  2. The system-level config
  3. An includeIf “gitdir:~/work/” section in ~/.gitconfig that loads a file with the work email
  4. A .gitignore file in ~/work/
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?

  1. The alias overrides a built-in Git command
  2. The alias is run by the shell, so it can chain several commands
  3. The alias runs with administrator rights
  4. The alias only works in the current repository
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?

  1. core.fsmonitor true
  2. diff.algorithm histogram
  3. help.autocorrect prompt
  4. pull.rebase true
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, unset and edit manage it.
  • Aliases shorten everyday commands; a leading ! runs a shell command, and git-<name> scripts become git <name>.
  • includeIf switches 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), and scalar speed 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.

↑ ↓ navigate Enter open Esc close