Git Configuration
Sensible Git defaults and aliases save Rust teams hours: rebase pulls, helpful logs, hooks that mirror CI, and global ignores for target/ artifacts.
Recipe
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global fetch.prune true
git config --global rerere.enabled true
git config --global alias.st 'status -sb'
git config --global alias.lg "log --oneline --graph -20"
git config --global alias.fp 'push --force-with-lease'When to reach for this:
- New machine onboarding
- Standardizing team developer experience
- Adding pre-commit hooks for
cargo fmtandclippy
Working Example
# ~/.gitignore_global
target/
**/*.rs.bk
.env
.env.*
!.env.examplegit config --global core.excludesfile ~/.gitignore_global# .pre-commit-config.yaml (repo)
repos:
- repo: local
hooks:
- id: rustfmt
name: rustfmt
entry: cargo fmt --all --
language: system
pass_filenames: false
- id: clippy
name: clippy
entry: cargo clippy --all-targets -- -D warnings
language: system
pass_filenames: falseWhat this demonstrates:
- Global ignores prevent
target/accidents rererereuses recorded conflict resolutions- Pre-commit matches CI gates
Deep Dive
Useful Settings
| Setting | Effect |
|---|---|
merge.conflictstyle zdiff3 | Shows common ancestor in conflicts |
push.autoSetupRemote true | First push sets upstream |
core.autocrlf input | LF line endings on macOS/Linux |
Commit Template
# .gitmessage
# <type>(<scope>): <subject> [TICKET]
#
# body: why, not what
git config commit.template .gitmessage
Gotchas
- Hook bypass habit -
--no-verifyevery commit. Fix: fix slow hooks, do not disable. - Global alias conflicts - shell functions shadow git aliases. Fix: document team standard aliases.
- Signed commits not configured - blocked push on protected branch. Fix:
ssh-keygenand GitHub signing setup early. - Windows line endings -
core.autocrlfsurprises in CI. Fix:.gitattributes* text=auto eol=lf. - Secrets in git config - tokens in URLs logged. Fix: credential helper, not embedded passwords.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
lefthook | Cross-language monorepo hooks | Simple Rust-only repo |
husky | JS+Rust polyglot | Pure Rust |
| Manual CI only | Zero local hook friction | Style drift before push |
FAQs
Global vs local config?
Identity and aliases global; hook versions local per repo for reproducibility.
credential helper on macOS?
git config --global credential.helper osxkeychain.
Large file handling?
Use Git LFS for binaries; prefer building release artifacts in CI instead of committing them.
Related
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.