Git & Command Line · Lesson 4 of 12
Config, .gitignore and Aliases
Set up git config at the right level, master .gitignore patterns and global ignores, and create Git aliases that make your everyday commands faster.
- Beginner
- 14 min read
- 4 objectives
Before this lessonLesson 3: Git Basics
What you will learn
- Understand system, global and local config
- Write .gitignore rules including negation
- Untrack a file that was committed by mistake
- Create time-saving aliases
Your Progress
0 of 12 lessons 0%
- Lessons0 / 12
- Completed0
- Est. time left~ 3 hours
Create a free account to keep your progress on every device.
Tip: pressing Next marks this lesson complete automatically.
Git has hundreds of settings, but a handful of them make a big difference every day: who you are, what your default branch is called, how line endings behave and which files Git should never touch. Setting them up once saves you from a lot of small annoyances, and from accidentally committing secrets.
Three levels of config
Git reads settings from three places, and the most specific one wins:
- System (
--system): every user on the machine. Rarely touched. - Global (
--global): you, in every repo. Stored in~/.gitconfig. - Local (
--local, the default inside a repo): just this repository, stored in.git/config.
So you can set your personal email globally and override it with your work email in work repositories only.
git config --global user.name "Ada Lovelace"
git config --global init.defaultBranch main
git config --global --listuser.name=Ada Lovelace init.defaultbranch=main
Notice Git prints keys in lower case; config keys are case-insensitive. To see which file a value came from, use git config --list --show-origin.
Settings worth setting
git config --global user.email "ada@stackcone.com"
git config --global core.editor "code --wait" # VS Code for commit messages
git config --global pull.rebase true # pull = fetch + rebase
git config --global push.autoSetupRemote true # first push sets upstream
git config --global fetch.prune true # drop deleted remote branches
git config --global rerere.enabled true # remember conflict fixesFor line endings, Windows users usually want core.autocrlf true and macOS/Linux users core.autocrlf input. Better still, commit a .gitattributes file with * text=auto so the whole team behaves the same regardless of personal settings.
.gitignore patterns
A .gitignore file lists paths Git should pretend do not exist when they are untracked. Commit it so everyone shares the same rules. Each line is a pattern:
# dependencies and build output
node_modules/
dist/
__pycache__/
# any .log file, anywhere
*.log
# ...except this one
!keep.log
# secrets
.env
.env.*
# only at the repo root
/config.local.json
# any depth under docs
docs/**/*.tmp- A trailing
/matches directories only. - A leading
/anchors the pattern to the folder containing the .gitignore. *matches anything except/;**matches across folders.!re-includes something an earlier rule excluded. You cannot re-include a file if its parent folder is ignored.
Here is a repo with node_modules/, app.log, keep.log, .env and index.js, using the first rules above. Only the files we want show up, and check-ignore -v tells you exactly which rule hides a file:
git status --short
git check-ignore -v app.log?? .gitignore ?? index.js ?? keep.log .gitignore:2:*.log app.log
GitHub keeps ready-made templates for every language at github.com/github/gitignore, and gh repo create can add one for you.
A global ignore for your machine's junk
Files like .DS_Store or your editor's .idea/ folder come from your setup, not the project. Instead of adding them to every repo, put them in a personal global ignore file:
printf '.DS_Store\n.idea/\n*.swp\n' >> ~/.gitignore_global
git config --global core.excludesFile ~/.gitignore_globalAlready committed? Untrack it
Adding a file to .gitignore does nothing if Git already tracks it. You have to remove it from the index while keeping it on disk:
echo ".env" >> .gitignore
git rm --cached .env
git commit -m "Stop tracking .env"Tracked but locally changed files
Sometimes a file must stay in the repo (a shared settings template, say) but you need a personal tweak that should never be committed. Ignoring will not help because the file is tracked. For a quick, local-only fix, tell Git to stop noticing changes:
git update-index --skip-worktree config/settings.json # hide my edits
git update-index --no-skip-worktree config/settings.json # undoA cleaner long-term pattern is to commit settings.example.json, ignore settings.json, and have each developer copy the example. That way nobody fights over a shared file, and a new teammate can see exactly which keys they need to fill in.
Aliases
Aliases are shortcuts stored in your config. They are great for long commands you type every day:
git config --global alias.st "status --short --branch"
git config --global alias.lg "log --oneline --graph --decorate"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.last "log -1 --stat"
git lg* 770e41a (HEAD -> main) init
An alias starting with ! runs a shell command, which lets you combine steps: git config --global alias.sync '!git switch main && git pull'. Keep aliases few and memorable; if you cannot remember what one does, you will not use it.
Recap
- Config levels are system, global and local; the most specific wins.
- Set name, email, default branch and a few quality-of-life options once.
- Commit a project
.gitignore; keep machine junk in a global ignore file. - Ignoring does not untrack: use
git rm --cached, and rotate any leaked secret. - Aliases turn long commands like a graph log into two letters.
# Write your solution here
Finished reading? Mark this lesson complete to track your progress.
