Skip to content

Instantly share code, notes, and snippets.

@iklobato
Created October 23, 2024 17:59
Show Gist options
  • Save iklobato/fa9887c051ff01acddf2a748c238744a to your computer and use it in GitHub Desktop.
Save iklobato/fa9887c051ff01acddf2a748c238744a to your computer and use it in GitHub Desktop.
my personal git configuration to avoid common git issues
# ~/.gitconfig
[user]
# User identity configuration
# Problems solved:
# - Wrong commit authorship when working with multiple git accounts
# - Accidental commits with personal email on work projects
# - Missing or inconsistent user information across repositories
# How it solves:
# - Forces explicit user config for each repository
# - Prevents automatic user info inheritance
# - Requires intentional identity configuration
# Examples:
# 1. Clone a new repo and try to commit (will fail):
# git clone [email protected]:org/repo.git
# cd repo
# git commit # Fails with "please tell me who you are"
# 2. Set repository-specific identity:
# git config user.name "John Doe"
# git config user.email "[email protected]"
useConfigOnly = true
[includeIf "gitdir:~/personal/"]
# Personal projects git configuration
# Problems solved:
# - Manual switching between different git identities
# - Accidental use of work email in personal projects
# - Inconsistent commit author information
# How it solves:
# - Automatically loads personal config based on directory
# - Separates personal and work configurations
# - Ensures correct identity without manual switching
# Examples:
# 1. Create a new personal project:
# cd ~/personal/
# git init my-project
# git commit -m "Initial" # Uses personal email automatically
# 2. Clone personal project:
# cd ~/personal/
# git clone [email protected]:username/repo.git
# 3. Verify current config:
# git config --show-origin --get user.email
path = ~/.gitconfig-personal
[includeIf "gitdir:~/wanna/"]
# WANNA projects git configuration
# Problems solved:
# - Risk of using personal git identity in work projects
# - Inconsistent commit history in work repositories
# - Manual identity management across multiple repositories
# How it solves:
# - Automatically applies work identity in work directories
# - Maintains consistent work identity across all WANNA repos
# - Eliminates need for per-repository configuration
# Examples:
# 1. Create work project:
# cd ~/wanna/
# git init project
# git commit # Uses work email automatically
# 2. Check active config:
# git config --list --show-origin | grep "user"
path = ~/.gitconfig-wanna
[push]
# Push behavior configuration
# Problems solved:
# - "no upstream branch" errors on first push
# - Forgotten tags during push operations
# - Inconsistent push behavior across git versions
# How it solves:
# - Automatically sets up remote tracking
# - Includes tags with pushes
# - Standardizes push behavior
# Examples:
# 1. Push new branch automatically:
# git checkout -b feature
# git push # Creates remote branch automatically
# 2. Push with tags:
# git tag v1.0.0
# git push # Tags are included automatically
# 3. Push current branch:
# git push # Pushes current branch without upstream
autoSetupRemote = true
default = current
followTags = true
[pull]
# Pull behavior configuration
# Problems solved:
# - Messy merge commits in git history
# - Confusing merge messages on simple pulls
# - Divergent branches causing conflicts
# How it solves:
# - Uses rebase instead of merge for cleaner history
# - Only allows fast-forward pulls
# - Prevents unexpected merge commits
# Examples:
# 1. Pull with rebase:
# git pull # Rebases automatically instead of merging
# 2. Update feature branch:
# git checkout feature
# git pull origin main # Rebases feature onto main
# 3. Prevent non-fast-forward merge:
# git pull # Fails if fast-forward not possible
rebase = true
ff = only
[init]
# Repository initialization settings
# Problems solved:
# - Inconsistent default branch names (master vs main)
# - Missing git hooks and templates
# - Inconsistent repository initialization
# How it solves:
# - Sets standardized default branch name
# - Applies consistent git templates
# - Ensures uniform repository setup
# Examples:
# 1. Create new repository:
# git init # Creates with 'main' as default branch
# 2. Use template hooks:
# git init project # Copies hooks from ~/.git-templates/hooks
# 3. Verify branch name:
# git branch --show-current # Shows 'main'
defaultBranch = main
templateDir = ~/.git-templates
[branch]
# Branch behavior configuration
# Problems solved:
# - Messy git history from frequent pulls
# - Merge bubbles cluttering commit graph
# - Inconsistent branch update strategies
# How it solves:
# - Forces rebase on pull operations
# - Maintains linear git history
# - Standardizes branch update behavior
# Examples:
# 1. Create tracking branch:
# git checkout -b feature origin/main # Sets up rebase
# 2. Pull updates:
# git pull # Automatically rebases
# 3. Check branch config:
# git config branch.feature.rebase # Shows 'true'
autoSetupRebase = always
[rebase]
# Rebase operation configuration
# Problems solved:
# - Failed rebases due to local changes
# - Lost work during rebase operations
# - Interrupted workflows during rebase
# How it solves:
# - Automatically stashes changes before rebase
# - Restores changes after rebase
# - Prevents rebase failures
# Examples:
# 1. Rebase with uncommitted changes:
# git rebase main # Auto-stashes changes
# 2. Interactive rebase:
# git rebase -i HEAD~3 # Stashes before interactive
# 3. Pull with uncommitted work:
# git pull # Stashes, rebases, then restores
autoStash = true
[fetch]
# Fetch operation configuration
# Problems solved:
# - Stale remote branches cluttering repository
# - Outdated remote references
# - Accumulation of obsolete tags
# How it solves:
# - Automatically removes deleted remote branches
# - Cleans up stale references
# - Maintains clean remote tracking
# Examples:
# 1. Clean up old branches:
# git fetch --all # Removes deleted remote branches
# 2. Update remote refs:
# git remote update # Prunes old references
# 3. Clean up tags:
# git fetch # Removes deleted remote tags
prune = true
pruneTags = true
[core]
# Core git behavior configuration
# Problems solved:
# - Permission changes triggering false modifications
# - Line ending inconsistencies between systems
# - Missing global ignores and attributes
# How it solves:
# - Ignores file mode changes
# - Standardizes line endings
# - Centralizes ignore and attribute rules
# Examples:
# 1. Handle line endings:
# git add . # Converts CRLF to LF automatically
# 2. Check ignores:
# git check-ignore -v file.log # Shows which rule matches
# 3. View effective config:
# git config --show-origin core.excludesFile
fileMode = false
editor = n
autocrlf = input
excludesFile = ~/.gitignore_global
attributesFile = ~/.gitattributes
[alias]
# Command aliases configuration
# Problems solved:
# - Common git commands requiring too much typing
# - Dangerous force push operations
# - Difficult to verify remote configurations
# How it solves:
# - Provides shortcuts for frequent commands
# - Adds safety check for force pushes
# - Enables easy remote verification
# Examples:
# 1. Quick status:
# git st # Same as git status
# 2. Pretty log:
# git lg # Shows formatted log graph
# 3. Force push safely:
# git pushf # Uses --force-with-lease
# 4. Show recent branches:
# git recent # Lists branches by date
# 5. Verify remote:
# git checkorigin # Shows origin URL
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
recent = for-each-ref --sort=-committerdate refs/heads/
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
pushf = push --force-with-lease
checkorigin = "!git remote -v | grep '^origin.*push)$' | cut -f2 | cut -d' ' -f1"
[diff]
# Diff operation configuration
# Problems solved:
# - Hard to understand code movement in diffs
# - Unclear whitespace changes
# - Difficult to read diff output
# How it solves:
# - Uses better diff algorithm for code movement
# - Highlights whitespace errors
# - Improves diff readability with mnemonics
# Examples:
# 1. Show moved code:
# git diff feature..main # Better detection of moved code
# 2. Show whitespace errors:
# git diff # Highlights problematic whitespace
# 3. Review changes:
# git diff --cached # Shows staged changes with mnemonics
algorithm = patience
renames = true
mnemonicPrefix = true
wsErrorHighlight = all
[merge]
# Merge operation configuration
# Problems solved:
# - Confusing merge conflicts
# - Lost context in merge conflicts
# - Difficult conflict resolution
# How it solves:
# - Shows original content in conflicts
# - Provides more context with diff3 style
# - Improves merge tool integration
# Examples:
# 1. Resolve conflict with context:
# git merge feature # Shows original in conflicts
# 2. Use merge tool:
# git mergetool # Opens configured editor
# 3. See merge commit info:
# git show # Includes merge history
log = true
tool = n
conflictStyle = diff3
[mergetool "n"]
# Neovim merge tool configuration
# Problems solved:
# - Cluttered merge resolution interface
# - Backup files littering repository
# - Unreliable merge tool exit codes
# How it solves:
# - Configures optimal Neovim merge layout
# - Prevents backup file creation
# - Trusts Neovim's exit status
# Examples:
# 1. Resolve merge conflict:
# git mergetool file.txt # Opens in Neovim
# 2. Check for backups:
# ls *.orig # Should show nothing
# 3. Complete merge:
# git merge --continue # Uses tool's exit code
cmd = n -d $LOCAL $REMOTE $MERGED -c 'wincmd J'
trustExitCode = true
keepBackup = false
[credential]
# Credential handling configuration
# Problems solved:
# - Frequent password prompts
# - Insecure credential storage
# - Session management issues
# How it solves:
# - Caches credentials temporarily
# - Limits cache duration for security
# - Balances convenience and security
# Examples:
# 1. First clone:
# git clone https://github.com/org/repo.git
# # Caches credentials for 1 hour
# 2. Check cached credentials:
# git credential fill
# 3. Clear cache:
# git credential reject
helper = cache --timeout=3600
[url "[email protected]:"]
# Personal GitHub SSH configuration
# Problems solved:
# - SSH key conflicts between accounts
# - Wrong authentication for repositories
# - HTTPS vs SSH URL inconsistencies
# How it solves:
# - Routes personal repos to correct SSH key
# - Automatically converts HTTPS to SSH
# - Ensures consistent authentication
# Examples:
# 1. Clone personal repo:
# git clone https://github.com/username/repo.git
# # Automatically uses personal SSH key
# 2. Add remote:
# git remote add origin [email protected]:username/repo.git
# # Routes through personal SSH config
insteadOf = [email protected]:
pushInsteadOf = https://github.com/
[url "[email protected]:"]
# WANNA GitHub SSH configuration
# Problems solved:
# - Mixed SSH keys between accounts
# - Authentication errors with multiple accounts
# - Incorrect remote URL formats
# How it solves:
# - Routes WANNA repos to correct SSH key
# - Prevents authentication confusion
# - Standardizes repository URLs
# Examples:
# 1. Clone work repo:
# git clone https://github.com/wanna/repo.git
# # Automatically uses work SSH key
# 2. Push to work repo:
# git push origin main
# # Uses correct SSH identity
insteadOf = [email protected]:
pushInsteadOf = https://github.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment