Git Configuration
Set up Git for a productive workflow - identity, aliases, defaults, and .gitignore patterns for Next.js projects.
Search across all documentation pages
Set up Git for a productive workflow - identity, aliases, defaults, and .gitignore patterns for Next.js projects.
🤖 Read the SystemsArchitect.io Blog for over 100+ cloud architecture articles 🔥
Quick-reference recipe card - copy-paste ready.
# Set identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Use rebase on pull by default
git config pull.rebase true
# Enable rerere (reuse recorded resolution)
git config --global rerere.enabled true
# Set VS Code as editor
git config --global core.editor "code --wait"
# View all config
git config --list --show-originWhen to reach for this: When setting up a new machine, onboarding to a project, or streamlining repetitive Git operations.
.gitignore for Next.js# Dependencies
node_modules/
.pnp/
.pnp.js
# Build output
.next/
out/
build/
dist/
# Environment files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# IDE
.vscode/settings.json
.idea/
*.swp
*.swo
.DS_Store
# Debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# TypeScript
*.tsbuildinfo
next-env.d.ts
# Vercel
.vercel
# Testing
coverage/
playwright-report/
test-results/
# Misc
*.pem
.turboWhat this demonstrates:
.env files are always ignored - secrets should never be committednext-env.d.ts is auto-generated and should be ignored# Short status
git config --global alias.s "status -sb"
# Pretty log
git config --global alias.lg "log --oneline --graph --all --decorate"
# Undo last commit (keep changes staged)
git config --global alias.undo "reset --soft HEAD~1"
# Show what you did today
git config --global alias.today "log --since='midnight' --oneline --author='Your Name'"
# List aliases
git config --global alias.aliases "config --get-regexp ^alias"
# Amend without editing message
git config --global alias.amend "commit --amend --no-edit"
# Quick diff of staged changes
git config --global alias.staged "diff --cached"Usage after setup:
git s # short status
git lg # pretty log graph
git undo # undo last commit
git today # today's commits
git amend # amend last commit silently
git staged # diff of staged changes# Global (applies to all repos) - stored in ~/.gitconfig
git config --global user.name "Your Name"
# Local (applies to this repo only) - stored in .git/config
git config user.email "work@company.com"
# System (applies to all users) - rarely used
git config --system core.autocrlf true
# Check where a config value comes from
git config --show-origin user.email# Auto-correct typos (runs after 1 second)
git config --global help.autocorrect 10
# Colorize output
git config --global color.ui auto
# Set default merge strategy
git config --global merge.conflictstyle diff3
# Sign commits with GPG
git config --global commit.gpgsign true
git config --global user.signingkey YOUR_GPG_KEY_ID
# Cache credentials for 1 hour
git config --global credential.helper 'cache --timeout=3600'
# macOS keychain
git config --global credential.helper osxkeychain# Generate a new SSH key
ssh-keygen -t ed25519 -C "you@example.com"
# Start the SSH agent
eval "$(ssh-agent -s)"
# Add key to agent
ssh-add ~/.ssh/id_ed25519
# Copy public key to clipboard (macOS)
pbcopy < ~/.ssh/id_ed25519.pub
# Then add to GitHub: Settings > SSH and GPG keys > New SSH key
# Test the connection
ssh -T git@github.com
# "Hi username! You've successfully authenticated"Git hooks run scripts automatically at key points in the Git workflow.
# Common hooks (in .git/hooks/ or managed by Husky)
pre-commit # Runs before each commit - lint, format, type-check
commit-msg # Validate commit message format
pre-push # Runs before push - run tests
post-merge # Runs after merge - reinstall dependenciesWith Husky (recommended for team projects):
npx husky init
# Add a pre-commit hook
echo "npx lint-staged" > .husky/pre-commitThings that will bite you. Each gotcha includes what goes wrong, why it happens, and the fix.
Wrong email in commits - Using a personal email on a work repo (or vice versa). Fix: Set local config per repo: git config user.email "work@company.com".
.gitignore not working on tracked files - Adding a file to .gitignore after it's already committed doesn't stop tracking it. Fix: git rm --cached .env to untrack it, then commit.
Line ending issues (CRLF/LF) - Windows and macOS/Linux use different line endings, causing noisy diffs. Fix: Add a .gitattributes file: * text=auto and *.tsx text eol=lf.
Hooks not running - Git hooks need execute permissions. Fix: chmod +x .husky/pre-commit. With Husky v9+, ensure the .husky/ directory is set up correctly.
Other ways to solve the same problem - and when each is the better choice.
| Alternative | Use When | Don't Use When |
|---|---|---|
.gitattributes | Enforcing line endings and diff behavior per file type | Simple single-platform projects |
| Husky + lint-staged | Team projects needing consistent pre-commit checks | Solo projects where you trust your own discipline |
direnv | Per-directory environment config beyond Git | Git-only configuration needs |
--global applies to all repos for the current user (~/.gitconfig)--system applies to all users on the machine (rarely used)git config --show-origin user.email.gitignore only prevents untracked files from being stagedgit rm --cached .env
git commit -m "chore: untrack .env"git config --global alias.s "status -sb"
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.undo "reset --soft HEAD~1"git s, git lg, git undo as shortcutsgit pull automatically rebase instead of mergegit config --global pull.rebase truegit config user.email "work@company.com"--global)ssh-keygen -t ed25519 -C "you@example.com"ssh-add ~/.ssh/id_ed25519ssh -T git@github.comrerere stands for "reuse recorded resolution"git config --global rerere.enabled truenpx husky init
echo "npx lint-staged" > .husky/pre-commitlint-staged config in package.json to run checks only on staged fileschmod +x .husky/pre-commit.husky/ directory is properly initializedprepare script exists in package.json: "prepare": "husky"node_modules/, .next/, out/, dist/, build/ (dependencies and build output).env, .env.local, .env.*.local (secrets)*.tsbuildinfo, next-env.d.ts (auto-generated TypeScript files)coverage/, playwright-report/ (test output).gitattributes file at the repo root:* text=auto
*.tsx text eol=lf
*.ts text eol=lf
Reviewed by Chris St. John·Last updated Jul 19, 2026
🤖 Read the SystemsArchitect.io Blog for over 100+ cloud architecture articles 🔥