Git Cheatsheet

Configuration

Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Config Levels

Git configuration is read from three levels, each overriding the previous:

LevelFlagFile LocationScope
System--system/etc/gitconfigAll users on the machine
Global--global~/.gitconfig or ~/.config/git/configCurrent OS user
Local--local.git/configCurrent repository (default)
Worktree--worktree.git/config.worktreeCurrent worktree
git config --global user.name "Alice"        # Set at global level
git config --local core.autocrlf false       # Set at repo level
git config --system core.editor vim          # Set at system level (needs sudo)

Reading Config Values

git config user.name                         # Read a single key
git config --list                            # Show all effective settings
git config --list --show-origin              # Also show which file each came from
git config --list --show-scope               # Show scope (system/global/local)
git config --get user.email                  # Get specific key (same as above)
git config --get-all alias.co               # Get all values for a multi-value key
git config --edit                            # Open current level config in editor
git config --global --edit                   # Open global config in editor

Essential Identity Settings

git config --global user.name "Alice Smith"
git config --global user.email "alice@example.com"

Core Settings

git config --global core.editor "code --wait"    # VS Code
git config --global core.editor "vim"            # Vim
git config --global core.editor "nano"           # Nano

git config --global core.autocrlf true           # Windows: convert LF→CRLF on checkout
git config --global core.autocrlf input          # macOS/Linux: convert CRLF→LF on commit
git config --global core.autocrlf false          # No conversion

git config --global core.ignorecase false        # Case-sensitive filenames
git config --global core.whitespace trailing-space,space-before-tab
git config --global core.pager "less -FX"        # Pager for log/diff output
git config --global core.fileMode false          # Ignore executable bit changes

Diff and Merge Tool Settings

git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff \$LOCAL \$REMOTE"

git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait \$MERGED"

git config --global diff.colorMoved zebra        # Color moved lines differently
git config --global merge.conflictstyle diff3    # Show base in conflict markers

Alias Settings

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 --decorate --all"
git config --global alias.undo "reset HEAD~1 --mixed"
git config --global alias.unstage "restore --staged"
git config --global alias.last "log -1 HEAD"
git config --global alias.aliases "config --get-regexp ^alias\."

Use aliases:

git st        # runs: git status
git lg        # runs: git log --oneline --graph --decorate --all

Push and Pull Settings

git config --global push.default current          # Push to upstream branch of same name
git config --global push.default simple           # (Git ≥2.0 default) same as current but safer
git config --global push.autoSetupRemote true     # Auto set --set-upstream on first push
git config --global pull.rebase true              # Always rebase instead of merge on pull
git config --global pull.ff only                  # Only fast-forward; fail otherwise
git config --global fetch.prune true              # Auto-prune deleted remote branches

Credential Settings

git config --global credential.helper cache               # In-memory cache (Linux)
git config --global credential.helper "cache --timeout=3600"  # Cache for 1 hour
git config --global credential.helper store               # Plain-text file (~/.git-credentials)
git config --global credential.helper osxkeychain         # macOS Keychain
git config --global credential.helper manager             # Git Credential Manager (Windows/cross-platform)

Color Settings

git config --global color.ui auto                 # Colorize output when in terminal (default)
git config --global color.diff.old "red bold"
git config --global color.diff.new "green bold"
git config --global color.status.added green
git config --global color.status.modified yellow
git config --global color.status.untracked cyan

Unset and Remove Settings

git config --global --unset user.email            # Remove a key
git config --global --unset-all alias.co          # Remove all values for a key
git config --global --remove-section alias        # Remove an entire section

Conditional Includes

Useful for separating work vs. personal configs:

# In ~/.gitconfig
[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal
# ~/.gitconfig-work
[user]
    email = alice@company.com
    signingkey = WORKKEY123

GPG Signing

git config --global user.signingkey <GPG_KEY_ID>
git config --global commit.gpgsign true           # Sign all commits by default
git config --global tag.gpgsign true              # Sign all tags by default
git config --global gpg.program gpg2