Essential Git Commands
The daily-driver Git command set for Python teams - clone to commit to push, plus the inspection commands that make debugging history fast.
Recipe
git status -sb
git diff
git add pyproject.toml uv.lock src/
git commit -m "feat(billing): add tax line item [BILL-42]"
git push -u origin feat/bill-42-taxWhen 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
# Clone and configure identity (once per machine)
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"
# Daily loop
git checkout main
git pull --rebase origin main
git checkout -b feat/bill-42-tax
# ... edit Python files ...
uv lock # if dependencies changed
git add pyproject.toml uv.lock src/billing/tax.py tests/test_tax.py
git commit -m "feat(billing): add tax line item [BILL-42]"
git fetch origin
git rebase origin/main
git push -u origin feat/bill-42-taxWhat this demonstrates:
- Short
status -sbfor branch and staged summary - Lockfile committed with dependency changes
- Rebase before push keeps linear history
- Ticket ID in commit message for traceability
Deep Dive
How It Works
- Working tree - Files on disk;
git addstages snapshots to index. - Commits - Immutable snapshots referenced by SHA.
- Remotes -
origintypically GitHub/GitLab;fetchdownloads refs without merging. - Branches - Named pointers to commits;
HEADis current checkout.
Command Reference
| Task | Command |
|---|---|
| What changed? | git status, git diff, git diff --staged |
| History | git log --oneline -20, git log -p file.py |
| Undo staging | git restore --staged file.py |
| Discard edits | git restore file.py |
| Stash WIP | git stash push -m "wip tax" |
| Update branch | git pull --rebase origin main |
Python Notes
# See who last touched a failing test
git blame tests/test_tax.py
# Find commit that changed default VAT rate
git log -S "DEFAULT_VAT" --oneline -- src/Gotchas
git add .catches secrets -.envslips into commits. Fix:.gitignore+git add -pfor sensitive repos.- Committing without lockfile - CI
uv sync --frozenfails. Fix: alwaysuv lockwithpyproject.tomledits. - 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 -
.venv/or__pycache__/. Fix: global gitignore for Python artifacts. - Empty commit noise - "WIP" commits before push. Fix:
git commit --amendor squash before PR.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| GitHub Desktop / GitKraken | Visual diff preference | Scripting and server SSH workflows |
jj (Jujutsu) | Experimenting with VCS | Team standard is Git CLI |
| IDE git panel | Quick staging | Learning rebase conflict resolution |
gh pr create | PR from terminal | You need GitHub CLI 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.
What goes in commit messages?
Imperative subject, ticket id, body explaining why - reference Conventions & Style Guide.
Should I commit .python-version?
Yes when team uses pyenv/mise - documents interpreter pin alongside requires-python.
How do I see remote branches?
git branch -r after git fetch --prune removes stale remote-tracking branches.
Recover deleted branch?
git reflog then git checkout -b feat/x <sha> within default 90-day window.
Diff against main?
git diff origin/main...HEAD shows PR delta three-dot style.
Tag releases from CLI?
git tag -a v1.2.0 -m "release 1.2.0" then git push origin v1.2.0 when tag triggers deploy.
Clone with submodules?
git clone --recurse-submodules for repos vendoring native deps - rare in pure Python services.
Signed commits?
Configure SSH or GPG signing when org policy requires - git log --show-signature verifies.
Related
- Branching & Merging - branch strategies
- Git Configuration - aliases and hooks
- GitHub CLI - PR automation
- Environment Setup Checklist - first-time git config
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.