Git Configuration and Aliases

Understand Git's configuration levels, choose your editor and line-ending settings, and save time with aliases.

Lesson 5 of 30Beginner17 min read

In the introduction you set your name and email, the two settings every Git user needs. Git has many more settings, though, and a few minutes spent on them makes every day with Git smoother: a friendly editor for commit messages, sensible line endings, clearer output and short commands for the things you type most.

All of these are stored by git config, in plain text files you can read and edit. Settings can apply to the whole computer, to your user account or to just one repository, and Git combines them so that the most specific one wins.

In this lesson you'll learn where settings live, check what's currently set, choose the most useful options, and create aliases: your own shortcuts such as git st for git status or a beautiful one-line history graph called git lg.

Three levels of settings

LevelFlagApplies toStored in
System--systemEvery user on the computerGit's install folder
Global--globalYour user account~/.gitconfig
Local--local (default)One repository.git/config

When the same setting appears at several levels, the most specific wins: local beats global, which beats system. That lets you use your work email in work repositories and your personal email everywhere else, for example.

See every setting and where it comes from with:

Terminal
git config --list --show-origin

Useful settings

Terminal
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --global pull.rebase false
git config --global color.ui auto
  • core.editor: the editor Git opens for commit messages and merges. code --wait uses VS Code; without it Git often opens Vim, which confuses many beginners.
  • init.defaultBranch: names the first branch of new repositories.
  • pull.rebase: tells git pull whether to merge (false) or rebase (true) when your branch and the remote have both changed. Setting it removes a warning and makes behaviour predictable.
  • color.ui auto: coloured output in terminals that support it.

Line endings

Windows ends lines with two invisible characters (CRLF), while macOS and Linux use one (LF). When people on different systems work on the same files, Git can see every line as "changed". The core.autocrlf setting fixes this:

  • On Windows: git config --global core.autocrlf true (store LF, check out CRLF).
  • On macOS and Linux: git config --global core.autocrlf input (convert any CRLF to LF when committing).

Teams can also add a .gitattributes file to the repository with * text=auto, which applies the same rules for everyone regardless of their personal settings.

Aliases

An alias is your own name for a Git command:

Terminal
git config --global alias.st status
git config --global alias.co switch
git config --global alias.last "log -1 HEAD"
git config --global alias.lg "log --oneline --graph --decorate --all"

Now git st runs git status, and git lg draws the whole history as a compact graph. Aliases are stored in your global config file, so they work in every repository. Keep them few and memorable; their job is to save typing on commands you use dozens of times a day.

Git Config Syntax

Config commands

git config --global key value
git config --local key value
git config --get key
git config --list --show-origin
git config --global --unset key
git config --global --edit
git config --global alias.lg "log --oneline --graph --all"

Git Config Summary

  • Settings exist at system, global and local levels; the most specific wins.
  • git config --list --show-origin shows every setting and its source.
  • Set core.editor, init.defaultBranch and pull.rebase to avoid surprises.
  • Use core.autocrlf (or .gitattributes) to handle Windows and Unix line endings.
  • git config --global alias.name "command" creates shortcuts such as git lg.

Git Config Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Checking your settings

Terminal
git config --global --list
git config --get user.email
git config --list --show-origin
Output
user.name=Sam Lee
user.email=sam@example.com
init.defaultbranch=main
sam@example.com
file:C:/Program Files/Git/etc/gitconfig    core.autocrlf=true
file:C:/Users/sam/.gitconfig               user.name=Sam Lee
file:C:/Users/sam/.gitconfig               user.email=sam@example.com
file:C:/Users/sam/.gitconfig               init.defaultbranch=main
Line-by-line explanation
  1. Line 1Only the global settings from your user account.
  2. Line 2Read a single value.
  3. Line 3Every setting with the file it comes from: the system file first, then the global one.
Example 2

A different email for one repository

Terminal
cd work-project
git config user.email "sam.lee@company.com"
git config --get user.email
cd ../hobby-project
git config --get user.email
Output
sam.lee@company.com
sam@example.com
Line-by-line explanation
  1. Line 2Without a level flag, git config writes a local setting for this repository only.
  2. Line 3Inside the work project, the local value wins.
  3. Line 5Elsewhere, the global email is used.
Example 3

Creating and using aliases

Terminal
git config --global alias.st "status --short"
git config --global alias.lg "log --oneline --graph --decorate --all"
git st
git lg
Output
 M index.html
?? notes.txt
* 52ac9e1 (HEAD -> main) Add dark colour scheme
* 9d41e07 Add contact form
* 3f9c2ab Add home page
Line-by-line explanation
  1. Line 1git st now shows a short, two-letter status for each file: M for modified, ?? for untracked.
  2. Line 2git lg prints the history as a compact graph, including all branches.

Common Git Config Mistakes

Getting stuck in Vim

Don't
git commit
# Vim opens; you can't figure out how to save and quit
Do
git config --global core.editor "code --wait"
git commit
# VS Code opens; save and close the tab to finish

If you're already in Vim, press Esc, type :wq and press Enter to save and quit.

Creating an alias with the "git" prefix

Don't
git config --global alias.st "git status"   # runs "git git status"
Do
git config --global alias.st status

Ignoring line-ending warnings

Don't
# "LF will be replaced by CRLF" warnings, and every line shows as changed
Do
git config --global core.autocrlf true   # on Windows
# or add a .gitattributes file with: * text=auto

Git Config in Real Projects

Configuration in teams

Companies often ask new developers to set pull.rebase, their work email and a commit signing key on their first day. Shared rules, such as line endings, go into .gitattributes and .editorconfig files committed to the repository, so every machine behaves the same way. Personal aliases are a matter of taste, and many developers proudly share theirs.

Git Config FAQ

How do I see which file a Git setting comes from?

Run git config --list --show-origin to list every setting with the file it was read from. For a single setting, use git config --show-origin user.email. This is the quickest way to find out why a repository isn't using the value you expected.

How do I remove a Git config setting?

Use --unset with the same level you set it at, for example git config --global --unset user.signingkey. Aliases are removed the same way: git config --global --unset alias.co. You can also open the file with git config --global --edit and delete the line.

How do I use a different email for work repositories?

Inside the work repository, run git config user.email "sam@company.com" without --global. A local setting overrides the global one for that repository only. If all your work projects live in one folder, an [includeIf "gitdir:~/work/"] section in your global config can load a separate config file for them automatically.

Can a Git alias run a shell command?

Yes. Start the alias value with ! and Git runs it in a shell: git config --global alias.root '!pwd' prints the repository's top folder. Shell aliases run from the top of the repository, not the folder you're in, so keep that in mind for commands that use relative paths.

How do I list all my Git aliases?

Run git config --get-regexp '^alias\.'. Each line shows an alias name followed by the command it expands to. Adding --global limits the list to aliases in your user-level config.

Git Config Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the command that sets your global editor to VS Code.

git config  core.editor "code --wait"
Multiple choiceeasy

Where are --global settings stored?

Predict the outputmedium

Global user.name is "Sam" and the repository's local user.name is "Sam (Work)". What does git config --get user.name print inside that repository?

Fill in the blanksmedium

Complete the command that creates an alias git co for git switch.

git config --global  switch
Multiple choicehard

Which setting helps when Windows and Mac users edit the same files?

Git Config Quiz

5 questions to check what you learned.

  1. Which level of Git setting wins when the same key is set in several places?

    • Local (the repository)
    • System
    • Global
    • The first one set
  2. What does git config --list --show-origin show?

    • Only your aliases
    • The remote repositories
    • Every setting and the file it comes from
    • The commit history
  3. What's the purpose of a Git alias?

    • To rename a branch
    • To hide a commit
    • To share settings with GitHub
    • To create your own shorter name for a Git command
  4. What does --wait do in code --wait as the editor?

    • Makes Git wait until you close the file in VS Code
    • Delays the commit by a minute
    • Opens VS Code in the background
    • Saves automatically
  5. Which file lets a team share line-ending rules inside the repository?

    • .gitignore
    • .gitattributes
    • .gitconfig
    • README.md