Essential Git Commands
The daily-driver Git command set for Rust teams - clone to commit to push, plus inspection commands that make debugging history fast.
Recipe
git status -sb
git diff
git add Cargo.toml Cargo.lock src/
git commit -m "feat(api): add health route [API-42]"
git push -u origin feat/api-42-healthWhen to reach for this:
- Starting work on a ticket branch
- Inspecting uncommitted changes before review
- Pushing feature branches and opening PRs
- Quick history lookup when CI breaks on
main
Working Example
git clone git@github.com:acme/billing-api.git
cd billing-api
git config user.name "Ada Lovelace"
git config user.email "ada@acme.com"
git checkout main
git pull --rebase origin main
git checkout -b feat/api-42-health
# ... edit Rust files ...
cargo test -q
git add Cargo.toml Cargo.lock src/routes/health.rs
git commit -m "feat(api): add health route [API-42]"
git fetch origin
git rebase origin/main
git push -u origin feat/api-42-healthWhat this demonstrates:
- Short
status -sbfor branch and staged summary Cargo.lockcommitted with dependency changes for binaries- Rebase before push keeps linear history
- Ticket ID in commit message for traceability
Deep Dive
Command Reference
| Task | Command |
|---|---|
| What changed? | git status, git diff, git diff --staged |
| History | git log --oneline -20, git log -p src/lib.rs |
| Undo staging | git restore --staged file.rs |
| Discard edits | git restore file.rs |
| Stash WIP | git stash push -m "wip health" |
| Update branch | git pull --rebase origin main |
Rust Notes
git blame src/error.rs
git log -S "AppError" --oneline -- src/Gotchas
git add .catches secrets -.envslips into commits. Fix:.gitignoreplusgit add -p.- Committing without lockfile - CI
cargo build --lockedfails. Fix: commitCargo.lockfor applications. - Pull merge commits on features - noisy history. Fix:
pull --rebaseon private branches. - Force push shared branches - rewrites team history. Fix:
--force-with-leaseonly on yourfeat/*before review. - Huge accidental add -
target/directory. Fix: global gitignore for Rust build artifacts.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| GitHub Desktop | Visual diff preference | Scripting workflows |
jj (Jujutsu) | Experimenting with VCS | Team standard is Git CLI |
gh pr create | PR from terminal | GitHub CLI not installed |
FAQs
merge vs rebase on feature branches?
Rebase onto main for clean PR history on private branches; merge on shared release branches per team policy.
How do I uncommit but keep changes?
git reset --soft HEAD~1 moves HEAD back one commit keeping staged files.
Diff against main?
git diff origin/main...HEAD shows PR delta three-dot style.
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+.