Configuration, aliases and attributes

Intermediate · 12 min

Learn Git's layered config files, write aliases for common commands, switch identity by directory with includeIf, and use .gitattributes for line endings.

Before thisInstalling and configuring GitIgnoring files with .gitignoreGit hooks

Cameron McKenzie · 12 min

Configuration, aliases and attributes

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Cameron McKenzieReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Git reads config from system, global (`~/.gitconfig`) and local (`.git/config`) files, and a more local value overrides a broader one.
  • `git config --show-origin --show-scope <key>` tells you which file a value came from, which is the first step in debugging a surprising setting.
  • `alias.<name>` defines a shortcut for a git command, and a value starting with `!` runs a shell command instead.
  • `includeIf "gitdir:<path>/"` loads extra config only for repositories under a directory, which suits using a different email for work projects.
  • `.gitattributes` sets per-path behaviour that travels with the repository: line-ending normalisation, `binary`, `merge=union`, and `export-ignore` for archives.

Three layers of settings

You’ve set user.name and user.email before. Where did Git put them, and why does one repository sometimes behave differently? Config lives in layers:

Scope File Applies to
system /etc/gitconfig or similar every user on the machine
global ~/.gitconfig you, in every repository
local .git/config this repository only

Later layers override earlier ones. To see exactly what applies and where each value comes from:

$ git config --local core.editor "vim"
$ git config --show-scope --show-origin core.editor
local	file:.git/config	vim
$ git config --list --show-scope | grep -E '^(global|local)' | head -4
global	user.name=Ada Example
global	user.email=ada@example.com
global	init.defaultbranch=main
local	core.repositoryformatversion=0

(I ran these with GIT_CONFIG_GLOBAL pointing at a scratch file, so my real global config was untouched, which is also how you test config safely.) When a setting “isn’t working”, --show-origin finds which file won.

Aliases

An alias is a config entry, alias.<name>, whose value Git expands into a command:

$ git config alias.st "status -sb"
$ git config alias.lg "log --oneline --graph --decorate"
$ git config alias.last "log -1 --stat"
$ git config alias.root '!pwd'
$ git st
## main
$ git lg
* e0112c6 (HEAD -> main) two
* c95a7c6 one

A value starting with ! runs as a shell command, which lets an alias do things Git alone can’t, like chaining commands. My git root printed the repository directory (I ran it from the top level, so I didn’t test it from a subfolder; the docs say shell aliases run from the top level). A bad alias fails clearly:

$ git config alias.nope "frobnicate"
$ git nope
expansion of alias 'nope' failed; 'frobnicate' is not a git command

Add --global to make an alias available everywhere. Keep them for commands you truly repeat. An alias hides the real command from teammates you’re pairing with.

A different identity per folder

You use one email for personal projects and another for work. includeIf loads extra config only when the repository’s path matches:

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

# in ~/work/.gitconfig
[user]
    email = ada@work.example

I tested it with absolute paths. A repository under the matching directory got the work email, and one outside it kept the default:

$ cd work/proj && git config --show-origin user.email
file:.../work.gitconfig	ada@work.example
$ cd ../../jot && git config user.email
ada@example.com

The trailing / in gitdir: matters, since it matches everything below. Verify it with --show-origin, as above.

Attributes: settings that travel with the repo

.git/config is private. .gitattributes is a file you commit, so the whole team gets the same per-path behaviour. I tried four common uses.

Line endings. A file with Windows endings was staged with no attributes: git ls-files --eol showed i/crlf, so Git stored the carriage returns. After adding the file below and git add --renormalize ., the stored copy became i/lf:

* text=auto
*.sh text eol=lf
*.png binary
CHANGELOG.md merge=union
docs/ export-ignore
.gitattributes export-ignore
$ git ls-files --eol
i/lf    w/crlf  attr/text eol=lf      	crlf.sh
$ git check-attr -a logo.png
logo.png: binary: set
logo.png: diff: unset

text=auto has Git detect text and normalise it. eol=lf forces LF in the working tree for shell scripts, which matters because a script with CRLF fails on Linux. binary stops Git trying to diff or merge images. Git also warned CRLF will be replaced by LF the next time Git touches it. It warns when the working file doesn’t match the rule.

Merge strategy. Two branches each appended a line to CHANGELOG.md, which normally conflicts. With merge=union, Git keeps both:

$ git merge a
Auto-merging CHANGELOG.md
Merge made by the 'ort' strategy.
$ cat CHANGELOG.md
# Changelog
- add export
- add search

Union suits append-only files like changelogs, and is dangerous for anything where the order or uniqueness of lines matters.

Archives. export-ignore excludes paths from git archive:

$ git archive HEAD --format=tar | tar t
CHANGELOG.md
code.txt
crlf.sh

docs/ and .gitattributes were left out.

What the video covers

Cameron McKenzie’s “Git Config File Tutorial: What, Where, Why and How?” is about eleven and a half minutes and was published around 2023. From its title it covers the config file and where settings live, which is the first section of this lesson (the three layers). I judge English from the title, description and channel only, and I could not read a transcript. No single video I found covered config, aliases, includeIf and attributes together, so the alias, includeIf and .gitattributes sections rest on my own runs. Philomatics’ alias video and a .gitattributes video are linked for those parts.

Where this fits

That completes the inside-Git group. Next, a group on repository features for larger projects, beginning with working on two branches at once: worktrees.

Check yourself

  1. 01A setting in `.git/config` and one in `~/.gitconfig` disagree. Which wins?

    The local one, `.git/config`. Order of precedence is system, then global, then local, with later (more specific) files overriding earlier ones.

  2. 02How do you find out where `user.email` is being set?

    Run `git config --show-origin --show-scope user.email`, which prints the scope and the file path for the value in effect.

  3. 03What is the difference between `alias.st "status -sb"` and `alias.root '!pwd'`?

    The first expands to a git subcommand. The leading `!` in the second runs the rest as a shell command from the repository root.

  4. 04What does `* text=auto` with `*.sh text eol=lf` in `.gitattributes` do?

    It lets Git detect text files and normalise their line endings in the repository, and forces shell scripts to use LF endings in the working tree.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark