Skip to main content

Git Configuration

Proper Git configuration ensures commits are correctly attributed and pushes work smoothly with GitHub.

User Configuration

Global Configuration

Set default user information for all repositories:

# Set global username
git config --global user.name "donnyaw"

# Set global email
git config --global user.email "donnyaw@gmail.com"

# Verify global settings
git config --global --list

Local Repository Configuration

Override global settings for a specific repository:

# Navigate to repository
cd /home/rezriz/github/01-production/vps-management

# Set local user (takes precedence over global)
git config user.email "donnyaw@gmail.com"
git config user.name "donnyaw"

# Verify local settings
git config --list --local

View All Configuration

# View all configuration (global + local)
git config --list

# View specific value
git config user.email
git config user.name

Configuration Files

Global Configuration File

Location: ~/.gitconfig

[user]
name = donnyaw
email = donnyaw@gmail.com

[core]
editor = vim
pager = cat

[init]
defaultBranch = main

[pull]
rebase = false

[push]
default = simple

Local Configuration File

Location: .git/config (inside repository)

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true

[remote "origin"]
url = git@github.com:donnyaw/vps-management.git
fetch = +refs/heads/*:refs/remotes/origin/*

[branch "main"]
remote = origin
merge = refs/heads/main

[user]
email = donnyaw@gmail.com
name = donnyaw

SSH Configuration for GitHub

SSH Config File

Location: ~/.ssh/config

Host github.com
HostName github.com
User git
IdentityFile /home/rezriz/.ssh/github/id_ed25519
IdentitiesOnly yes

Verify SSH Configuration

# Display GitHub SSH configuration
cat ~/.ssh/config | grep -A 5 "github"

# Test SSH connection
ssh -T git@github.com

# Verbose test
ssh -v -T git@github.com 2>&1 | grep "Authenticating"

Git Aliases

Useful Aliases

Add to ~/.gitconfig:

[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = log --oneline --graph --decorate --all
amend = commit --amend --no-edit
pushf = push --force-with-lease

Usage Examples

# Instead of: git status
git st

# Instead of: git checkout main
git co main

# Instead of: git commit
git ci

# View graph
git visual

Branch Configuration

Set Default Branch Name

# Set default branch for new repositories to 'main'
git config --global init.defaultBranch main

Rename Branch

# Rename current branch to 'main'
git branch -M main

# Push and set upstream
git push -u origin main

Remote Configuration

Add Remote Repository

# Add origin remote
git remote add origin git@github.com:donnyaw/vps-management.git

# Add multiple remotes
git remote add upstream git@github.com:original/repo.git

Update Remote URL

# Change remote URL
git remote set-url origin git@github.com:donnyaw/new-repo.git

# View all remotes
git remote -v

Remove Remote

# Remove remote
git remote remove origin

# Remove and re-add
git remote remove origin
git remote add origin git@github.com:donnyaw/vps-management.git

Credential Handling

SSH Key as Credential

Our preferred method - uses SSH key for authentication:

# HTTPS (requires token)
git clone https://github.com/donnyaw/vps-management.git

# SSH (uses SSH key - preferred)
git clone git@github.com:donnyaw/vps-management.git

Using Specific SSH Key

# Set GIT_SSH_COMMAND environment variable
GIT_SSH_COMMAND="ssh -i /home/rezriz/.ssh/github/id_ed25519 -o IdentitiesOnly=yes" git push origin main

# Or configure in repository
git config core.sshCommand "ssh -i /home/rezriz/.ssh/github/id_ed25519 -o IdentitiesOnly=yes"

Core Settings

Line Endings

# Linux/Mac - use LF
git config --global core.autocrlf input

# Verify setting
git config core.autocrlf

Default Editor

# Set vim as default editor
git config --global core.editor vim

# Set nano as default editor
git config --global core.editor nano

Pager Configuration

# Disable pager for short output
git config --global core.pager cat

# Use less with options
git config --global core.pager "less -RFX"

Performance Settings

File Mode Detection

# Ignore file mode changes (useful for permissions)
git config core.filemode false

Large File Settings

# Increase HTTP buffer
git config --global http.postBuffer 524288000

# Set pack size limits
git config --global pack.windowMemory "100m"
git config --global pack.packSizeLimit "100m"

Security Settings

Verify Commits

# Require signed commits
git config --global commit.gpgsign true

# Set GPG key
git config --global user.signingkey YOUR_GPG_KEY

SSL Verification

# Enable SSL verification (default, recommended)
git config --global http.sslVerify true

# Disable only if absolutely necessary (not recommended)
git config --global http.sslVerify false

Repository-Specific Configuration

Configure After Clone

# Clone repository
git clone git@github.com:donnyaw/vps-management.git

# Navigate to repository
cd vps-management

# Configure local user
git config user.email "donnyaw@gmail.com"
git config user.name "donnyaw"

# Set upstream tracking
git branch --set-upstream-to=origin/main main

Cleanup Configuration

Remove Configuration Options

# Remove global setting
git config --global --unset user.name

# Remove local setting
git config --unset user.email

# Remove entire section
git config --remove-section remote.origin

Troubleshooting

View Configuration Location

# Show where config value is set
git config --show-origin user.email

# Show all origins
git config --list --show-origin

Fix Configuration Issues

# Reset specific config
git config --unset core.filemode

# Edit configuration file directly
vim ~/.gitconfig

# Verify syntax
git config --list

Common Configuration Problems

Wrong Email in Commits

# Fix last commit
git commit --amend --author="donnyaw <donnyaw@gmail.com>" --no-edit

# Fix multiple commits (use interactive rebase)
git rebase -i HEAD~5

Push Authentication Failed

# Verify SSH is configured
cat ~/.ssh/config | grep -A 5 github

# Test SSH connection
ssh -T git@github.com

# Check remote URL is SSH (not HTTPS)
git remote -v

Best Practices

Initial Setup Checklist

  1. Set global user name and email
  2. Configure SSH for GitHub
  3. Set default branch to 'main'
  4. Configure editor preference
  5. Set up aliases for common commands
  6. Test SSH connection to GitHub

Setup Script

#!/bin/bash
# git-initial-setup.sh

# Global configuration
git config --global user.name "donnyaw"
git config --global user.email "donnyaw@gmail.com"
git config --global init.defaultBranch main
git config --global core.editor vim
git config --global core.pager cat
git config --global pull.rebase false

# Aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.last "log -1 HEAD"

# Test GitHub connection
ssh -T git@github.com

echo " Git configuration complete"

Configuration Templates

Minimal Configuration

git config --global user.name "donnyaw"
git config --global user.email "donnyaw@gmail.com"
git config --global init.defaultBranch main

Developer Configuration

git config --global user.name "donnyaw"
git config --global user.email "donnyaw@gmail.com"
git config --global init.defaultBranch main
git config --global core.editor vim
git config --global pull.rebase false
git config --global push.default simple
git config --global core.autocrlf input

Advanced Configuration

git config --global user.name "donnyaw"
git config --global user.email "donnyaw@gmail.com"
git config --global init.defaultBranch main
git config --global core.editor vim
git config --global core.pager "less -RFX"
git config --global pull.rebase false
git config --global push.default simple
git config --global core.autocrlf input
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.visual "log --oneline --graph --decorate --all"