Useful Git Settings

Updated · 11 min read
mediumGit 2.53gitgit-configinclude-ifrererecredentialsinterview

Git settings are named values, such as user.email or pull.rebase, that change how Git behaves. They are read with and written by the git config command, and stored in plain text files at three levels: for the whole computer, for your user account, and for one repository. "Git Config: Setting Up Git" covered the first steps. This lesson covers the settings experienced developers change, how to see where a value comes from, and how to use different settings automatically in different folders.

Think of a new phone. The manufacturer sets defaults, you set your own preferences, and some apps have settings of their own that override yours inside that app. Git settings work the same way: the most specific setting wins.

The Three Levels

LevelOptionFileApplies to
System--system/etc/gitconfig (or Git's install folder on Windows)Every user on the computer
Global--global~/.gitconfig (or ~/.config/git/config)Every repository of your user
Local--local (the default when writing inside a repository).git/configOne repository

When the same setting appears at several levels, local overrides global, and global overrides system. --show-origin and --show-scope reveal where each value comes from:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
git config --global pull.rebase true
git config pull.rebase false
git config --show-scope --show-origin --get-all pull.rebase
git config pull.rebase
Output
global	file:/home/navika/.gitconfig	true
local	file:.git/config	false
false

Explanation: pull.rebase is set twice: true globally and false in this repository. --get-all lists both, with their file and level. A plain git config pull.rebase prints the value Git actually uses: the local one wins.

Settings Worth Knowing

SettingRecommendedDescription
init.defaultBranchmainIt is used to name the first branch of new repositories.
pull.rebasetrueIt is used to rebase local commits on pull, instead of creating merge commits.
push.autoSetupRemotetrueIt is used to set the upstream on the first push of a branch automatically.
push.followTagstrueIt is used to push annotated tags together with the commits they point to.
fetch.prunetrueIt is used to remove copies of branches deleted on the server.
rebase.autoStashtrueIt is used to stash and restore uncommitted changes around a rebase.
rebase.updateRefstrueIt is used to move stacked branches along during a rebase.
merge.conflictStylezdiff3It is used to show the original lines in conflict markers.
rerere.enabledtrueIt is used to remember conflict resolutions and reuse them.
diff.algorithmhistogramIt is used to produce clearer diffs for moved or repeated code.
diff.colorMoveddefaultIt is used to show moved lines in a different color.
help.autocorrectpromptIt is used to suggest (and optionally run) the right command after a typo.
core.editoryour editorIt is used to choose the editor for messages.
branch.sort-committerdateIt is used to list the most recently used branches first in git branch.

Set the ones you want with --global once. The next sections show two of them in action.

rerere: Reuse Recorded Resolution

rerere ("reuse recorded resolution") makes Git remember how you resolved a conflict. If the same conflict appears again, for example when you rebase a branch a second time or redo a merge, Git applies your earlier resolution automatically.

Terminal
git config rerere.enabled true
echo 'Late fee: $250' > fees.txt
git add fees.txt
git commit --quiet -m "Add the late fee"
git switch --quiet -c feature/late-fee
echo 'Late fee: $300' > fees.txt
git commit --quiet -am 'Raise the late fee to $300'
git switch --quiet main
echo 'Late fee: $275' > fees.txt
git commit --quiet -am 'Raise the late fee to $275'
git merge feature/late-fee
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Recorded preimage for 'fees.txt'
Automatic merge failed; fix conflicts and then commit the result.

Recorded preimage for 'fees.txt': rerere saved the conflict. Navika resolves it, which rerere records too, and then, to test the feature, undoes the merge completely:

Terminal
echo 'Late fee: $300' > fees.txt
git add fees.txt
git commit --quiet --no-edit
Output
Recorded resolution for 'fees.txt'.

Recorded resolution means rerere saved how the conflict was resolved. Now the merge is undone and run again:

Terminal
git reset --quiet --hard HEAD~1
git merge feature/late-fee
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Resolved 'fees.txt' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
Terminal
cat fees.txt
Output
Late fee: $300

Explanation: the second merge hit the same conflict, and Git said Resolved 'fees.txt' using previous resolution. The file already has the resolved content. Git still stops, so that you can check the result and git add it, but the work is done. With rebases that replay many commits, or long-lived branches merged repeatedly, rerere saves a lot of time.

help.autocorrect: Typo Rescue

Terminal
git merge --abort
git -c help.autocorrect=immediate stauts -s
Output
WARNING: You called a Git command named 'stauts', which does not exist.
Continuing under the assumption that you meant 'status'.

With immediate, Git runs the only close match after a typo. prompt asks first, a number such as 10 waits that many tenths of a second, and never (or unset) only prints suggestions. git -c <name>=<value> sets a value for one command only, without changing any file.

Conditional Includes: Work and Personal Identities

Many developers use one computer for work and personal projects, with a different email for each. includeIf loads an extra config file only for repositories in certain folders. Navika keeps work repositories under ~/work/:

Terminal
cd ..
mkdir -p ~/work
cat > ~/.gitconfig-work <<'EOF'
[user]
    email = navika@school.example.com
EOF
git config --global includeIf."gitdir:~/work/".path ~/.gitconfig-work
git init --quiet ~/work/school-portal
git init --quiet personal-site
echo "Work:     $(git -C ~/work/school-portal config user.email)"
echo "Personal: $(git -C personal-site config user.email)"
Output
Work:     navika@school.example.com
Personal: navika@example.com

Explanation: inside ~/work/, Git also reads ~/.gitconfig-work, whose email overrides the global one. Everywhere else, the global email is used. The trailing / in gitdir:~/work/ means "this folder and everything below it". Other conditions are gitdir/i: (case-insensitive, useful on Windows and macOS), onbranch:<pattern> and hasconfig:remote.*.url:<pattern> (for example, every repository whose remote is on your company's server).

One computer, two identities

Pager, Colors and Prompt

Setting or toolDescription
core.pagerIt is used to choose the pager for long output, such as less -FRX (exits at once if the output fits on screen).
pager.<command> falseIt is used to turn the pager off for one command, for example pager.branch false.
color.uiIt is used to turn colors on (auto, the default) or off (never).
Shell promptShows the current branch and state. Git ships git-prompt.sh for Bash and Zsh, and tools like Starship or Oh My Posh do it for any shell.

A prompt that shows fee-tracker (main *) (a branch, and * for unstaged changes) prevents many "wrong branch" mistakes.

Credential Storage

Git can remember HTTPS tokens in several ways, with different security:

HelperStores the tokenSecurity
manager (Git Credential Manager)In the operating system's secure storeGood: encrypted, supports browser sign-in
osxkeychainIn the macOS KeychainGood: encrypted
libsecretIn the Linux desktop's keyringGood: encrypted
cache --timeout=<seconds>In memory, for a limited timeFair: never written to disk
storeIn a plain text file, ~/.git-credentialsPoor: anyone who can read the file has the token

Avoid store on shared or unencrypted computers. SSH keys with a passphrase and an agent ("Adding an SSH Key") avoid storing tokens at all.

Common Mistakes

Setting something with --global and wondering why it doesn't apply. A local value overrides it. Check with --show-origin.

A wrong email on work commits. Use includeIf so that the identity follows the folder.

Forgetting the trailing / in gitdir:. Without it, the condition matches only that exact repository folder.

credential.helper store on a shared computer. The token is in plain text. Use the system's secure store.

Copying a huge .gitconfig from the internet. Settings you don't understand can surprise you. Add them one at a time.

Under the Hood

Config files use a simple INI-like format: [section] or [section "subsection"] headers, then key = value lines. Git reads the system file, then the global files, then the local .git/config, then any worktree-specific file, applying include and includeIf where they appear, and the last value read for a single-valued key wins. That reading order is why local overrides global. GIT_CONFIG_COUNT, GIT_CONFIG_KEY_<n> and GIT_CONFIG_VALUE_<n> environment variables and -c on the command line are applied after all files, which is how a one-off git -c setting beats everything else.

Interview Corner

What are the levels of Git configuration?

System (/etc/gitconfig), global (~/.gitconfig) and local (.git/config), plus worktree-specific config and one-off values with git -c. More specific levels override less specific ones: local over global over system.

How do you find out where a Git setting comes from?

git config --show-origin --show-scope <name> (or --list --show-origin) prints each value with its file and level.

How do you use a different email for work repositories automatically?

Put the work email in a separate file and include it conditionally in ~/.gitconfig with [includeIf "gitdir:~/work/"] and path = ~/.gitconfig-work. It applies to every repository under that folder.

What is git rerere?

"Reuse recorded resolution": with rerere.enabled=true, Git records how you resolve each conflict and automatically reapplies the same resolution when the identical conflict appears again, such as during repeated rebases or merges.

Which credential helper should you use?

One that uses the operating system's secure storage: Git Credential Manager (manager), osxkeychain or libsecret. Avoid store, which saves tokens in plain text.

Practice

Challenge: a second work folder. Navika also works for a tutoring center, whose repositories are under ~/tutoring/, with the email navika@tutoring.example.com. Add a second conditional include, then print the email used in ~/tutoring/timetable, in ~/work/school-portal and in personal-site.

What does Git print? Before you open the solution, decide which file each repository reads.

Show solution
Terminal
cat > ~/.gitconfig-tutoring <<'EOF'
[user]
    email = navika@tutoring.example.com
EOF
git config --global includeIf."gitdir:~/tutoring/".path ~/.gitconfig-tutoring
git init --quiet ~/tutoring/timetable
echo "Tutoring: $(git -C ~/tutoring/timetable config user.email)"
echo "Work:     $(git -C ~/work/school-portal config user.email)"
echo "Personal: $(git -C personal-site config user.email)"
Output
Tutoring: navika@tutoring.example.com
Work:     navika@school.example.com
Personal: navika@example.com

Each folder gets its own identity: the tutoring and work repositories read their included files, and the personal one uses the global email. A third employer would just be one more includeIf line.

Checkpoint. Set up a work and a personal identity that switch automatically by folder, enable rerere, fetch.prune and pull.rebase globally, and check every value's origin with git config --list --show-origin --show-scope.

Quick check: Useful Git settings

4 questions
1A setting has different values in ~/.gitconfig and .git/config. Which one does Git use?
2Which option shows where a setting's value comes from?
3What does rerere.enabled=true do?
4Which credential helper stores tokens in plain text?

Key Takeaways

Quick reference

CommandIt is used to
git config --global <name> <value>Set a value for your user
git config <name> <value>Set a value for this repository
git config --show-origin --show-scope <name>See where a value comes from
git config --list --show-originList every setting with its file
git config --global --unset <name>Remove a setting
git -c <name>=<value> <command>Use a value for one command
git config --global includeIf."gitdir:~/work/".path <file>Include a file for one folder

Was this tutorial helpful?

Lesson 77 of 106