Git Configuration & Aliases
Reviewed & published by Brayan K
Five minutes of setup makes Git faster and friendlier forever. By the end of this lesson you'll have configured your identity, picked your editor, and built a set of short aliases that turn long commands into two-letter shortcuts.
Part of the free Git course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
What You'll Learn
- Set your commit identity with git config --global user.name / user.email
- Understand the three config scopes: system, global, and local
- Know which value wins when scopes overlap (most specific)
- Create time-saving aliases like st, co, br, and lg
- Write shell-command aliases with the ! prefix
- Set core.editor and init.defaultBranch, and inspect config origins
1️⃣ Set Your Identity
Git stamps every commit with a name and email. Set them once with --global so they apply to all your repos. Use the same email as your GitHub account so your commits link back to your profile.
# The very first thing to set on a new machine: your identity. Git stamps
# every commit with this name and email, so set it ONCE globally.
git config --global user.name "Ada Lovelace"
git config --global user.email "ada@example.com"
# Check what you set
git config --global user.name # -> Ada Lovelace
git config --global user.email # -> ada@example.com
# Use the same email as your GitHub account so commits link to your profile.2️⃣ The Three Scopes
Config exists at three levels: --system (all users), --global (all your repos), and --local (just this repo, the default). Git reads them in that order, so the most specific value wins — handy for using a work email in one repo while keeping your usual one everywhere else.
# Git config has three scopes, applied least- to most-specific:
# --system /etc/gitconfig (all users on the machine)
# --global ~/.gitconfig (all of YOUR repos)
# --local <repo>/.git/config (just this one repo) [the default]
#
# The most specific value wins, so a local setting overrides your global one.
# Example: use a work email in just this one repo, keeping your global one
git config --local user.email "ada@work.com"
# See every value AND which file it came from — great for debugging
git config --list --show-origin3️⃣ Aliases — Your Own Shortcuts
Aliases live under the [alias] section and turn long commands into short ones. Define st, co, lg and use them like real subcommands. Start an alias value with ! to run a full shell command.
# Aliases turn long commands into short ones. They live under [alias].
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"
# Now use them like real subcommands:
git st # == git status
git co main # == git checkout main
git lg # == git log --oneline --graph --all
# An alias starting with ! runs a SHELL command (great for combos):
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "reset HEAD --"Your turn. Set your name globally and add a one-letter status alias. Fill in the two blanks.
# 🎯 YOUR TURN — replace each ___ then run it in your terminal.
# Goal: set your identity and create a handy alias.
# 1) Set your name for ALL your repos
git config ___ user.name "Your Name" # 👉 the flag for machine-wide config
# 2) Make "git s" run "git status"
git config --global ___.s status # 👉 the section name for shortcuts
# 3) Prove it works
git s
# ✅ Expected: step 3 prints the same thing as "git status".4️⃣ Editor & Other Handy Settings
A few one-time settings smooth everything out: core.editor picks the editor Git opens, init.defaultBranch names the starting branch for new repos, and color.ui turns on colour. Remove any setting with --unset.
# A few quality-of-life settings worth doing once globally.
# Which editor opens for commit messages, rebases, etc.
git config --global core.editor "code --wait" # VS Code; or "nano", "vim"
# Name the starting branch for every new repo you create
git config --global init.defaultBranch main
# Colourful output, and remember resolved conflicts
git config --global color.ui auto
# Made a typo in a setting? Remove it cleanly:
git config --global --unset core.editorCommon Errors (and the fix)
- Commits show the wrong author: a local override is in play, or identity was unset. Run git config --list --show-origin and fix or --unset the offending value.
- Editor never opens / hangs: a GUI editor without --wait returns immediately, so Git thinks you wrote nothing. Use core.editor "code --wait".
- Alias does nothing or errors: you may have set it locally in a different repo, or the value needs quotes. Multi-word values must be quoted: alias.lg "log --oneline".
- "command not found" from a ! alias: shell aliases run from the repo root and need the tools on your PATH — test the raw command first.
- Forgetting --global: without it, git config writes to the current repo only, so the setting "disappears" in other repos.
Pro Tips
- 💡 Edit the file directly: git config --global --edit opens ~/.gitconfig for bulk changes.
- 💡 Great starter aliases: st=status, co=checkout, lg=log --oneline --graph --all, last=log -1 HEAD.
- 💡 Per-directory identity: use a conditional include (includeIf "gitdir:~/work/") to auto-switch your work email.
- 💡 Sync across machines: keep your ~/.gitconfig in a dotfiles repo.
📋 Quick Reference
| Command | Purpose |
|---|---|
| git config --global user.name "X" | Set your commit name everywhere |
| git config --global alias.st status | Create the alias git st |
| git config --list --show-origin | List all values and their files |
| git config --global core.editor "..." | Choose your editor |
| git config --global init.defaultBranch main | Name the starting branch |
| git config --unset key | Remove a setting |
Mini-Challenge: Build Your Starter Config
No commands given this time — just the plan. Configure a fresh machine the way a pro would, all in your global config.
# 🎯 MINI-CHALLENGE: build your starter config.
# No commands given — just the plan. Run each step in your terminal.
#
# 1. Set your global user.name and user.email (match your GitHub email)
# 2. Set core.editor to your editor of choice
# 3. Set init.defaultBranch to "main"
# 4. Add three aliases you'll actually use (e.g. st, co, lg)
# 5. Run "git config --global --list" and confirm everything is there
#
# ✅ Expected: git config --global --list shows your identity, editor,
# default branch, and all your aliases in one place.
# your commands here🎉 Lesson Complete!
- ✅ --global sets your identity and prefs for every repo
- ✅ Three scopes — system, global, local — with the most specific winning
- ✅ Aliases turn long commands into short ones; ! runs a shell command
- ✅ core.editor and init.defaultBranch are worth setting once
- ✅ --list --show-origin debugs surprising values
Practice quiz
Which command sets your name for ALL repos on your machine?
- git config user.name "You"
- git config --system user.name "You"
- git config --global user.name "You"
- git name "You"
Answer: git config --global user.name "You". --global writes to ~/.gitconfig, applying to every repo for your user.
Where does --global config live?
- In ~/.gitconfig (your home directory)
- In each repo's .git/config
- In /etc/gitconfig
- In the cloud
Answer: In ~/.gitconfig (your home directory). Global config is stored in ~/.gitconfig and applies to all of your repositories.
Which scope wins when the same key is set globally and locally?
- Global always wins
- Whichever was set first
- They are merged
- The local (repo) value overrides the global one
Answer: The local (repo) value overrides the global one. Git applies system, then global, then local — the most specific (local) value wins.
How do you create an alias 'st' for 'status'?
- git alias st status
- git config --global alias.st status
- git config st=status
- git st = status
Answer: git config --global alias.st status. git config --global alias.st status defines the alias under the [alias] section.
After 'git config --global alias.co checkout', what does 'git co main' do?
- Runs git checkout main
- Nothing
- Creates a branch
- Errors
Answer: Runs git checkout main. The alias expands 'co' to 'checkout', so git co main runs git checkout main.
How do you make an alias that runs a non-git shell command?
- Prefix with #
- Wrap it in []
- Prefix the value with !
- You can't
Answer: Prefix the value with !. An alias value starting with ! runs as a shell command, e.g. alias.cleanup = !git ...
Which setting controls the editor Git opens for commit messages?
- core.pager
- core.editor
- commit.editor
- user.editor
Answer: core.editor. core.editor sets the editor, e.g. git config --global core.editor "code --wait".
How do you list every config value and where it came from?
- git config --all
- git show config
- git config --dump
- git config --list --show-origin
Answer: git config --list --show-origin. --list --show-origin prints each value with the file it was read from.
What's a safe value for the default branch name of new repos?
- core.branch main
- init.defaultBranch main
- branch.default main
- git default main
Answer: init.defaultBranch main. git config --global init.defaultBranch main sets the initial branch name for git init.
How do you remove a config entry?
- git config --delete key
- git config --remove key
- git config --unset key
- git unset key
Answer: git config --unset key. git config --unset <key> removes a single value (use --unset-all for multiples).
Continue this course
- Previous: Hunting Bugs with git bisect
- Next: GitHub Pull Requests & Code Review — Open, review, and merge pull requests — the heart of team collaboration
- Quick reference: Git cheat sheet › Setup & Config
Frequently asked questions
What's the difference between --global and --local config?
--global writes to ~/.gitconfig and applies to every repository for your user — perfect for your name, email, editor, and aliases. --local (the default when you omit the flag) writes to the current repo's .git/config and applies only there. When the same key is set in both, the local value wins because Git applies scopes from least specific (system) to most specific (local).
Why do my commits show the wrong author?
Almost always because user.name / user.email weren't set, or a repo has a local override. Run git config --list --show-origin to see every value and the file it came from. Set the right identity globally with git config --global user.email "...", and remove any stray local override with git config --local --unset user.email. Note: changing config doesn't rewrite commits you already made.
How do aliases work, and can they run shell commands?
An alias maps a short word to a longer Git command: git config --global alias.st status makes git st run git status. If the alias value starts with an exclamation mark (!), Git runs it as a shell command instead, so you can chain tools — e.g. alias.cleanup = !git fetch -p && git branch --merged. Aliases live under the [alias] section of your config.
Which editor does Git use, and how do I change it?
Git uses core.editor for commit messages, interactive rebases, and similar prompts; if that's unset it falls back to the EDITOR/VISUAL environment variables, then a built-in default. Set it with git config --global core.editor "code --wait" for VS Code (the --wait makes Git pause until you close the file), or "nano" / "vim" for terminal editors.
Where exactly are these settings stored?
In plain-text INI files you can open and read: system config in /etc/gitconfig, your global config in ~/.gitconfig (or ~/.config/git/config), and per-repo config in that repo's .git/config. You can edit them directly with git config --global --edit, but using git config commands avoids syntax mistakes.