Git for Data Engineers cheat sheet
Branching for dbt and pipeline repos, rebase versus merge, recovering mistakes, and hygiene that keeps CI green.
Daily loop
git switch -c feature/add-orders-mart- Modern replacement for checkout -b. Branch names that describe the model or pipeline beat ticket numbers alone.
git add -p- Stage hunks interactively. The single best habit for keeping a dbt refactor separate from an unrelated fix.
git commit -m "Add fct_orders at order-line grain"- Say what changed and at what grain. Reviewers of data code care about grain more than about file names.
git pull --rebase- Keeps history linear instead of littering it with merge commits. Set pull.rebase=true globally and forget it.
git push -u origin HEAD- Pushes and sets upstream in one step without retyping the branch name.
Inspecting history
git log --oneline --graph --decorate -20- The orientation command when you inherit a repo or return after a week away.
git log -p -- models/marts/fct_orders.sql- Full change history for one model. The fastest answer to "when did this join change?"
git blame -L 40,60 models/marts/fct_orders.sql- Line-range blame for a suspicious CASE expression, without wading through the whole file.
git log -S "customer_segment"- Pickaxe search finds the commit that introduced or removed a string — better than grepping the current tree.
git diff main...HEAD --stat- Three-dot diff shows only your branch's changes, which is exactly what the reviewer will see.
Fixing mistakes
git restore --staged path- Unstage without losing edits. Replaces the confusing reset HEAD incantation.
git restore path- Discard working-tree changes to a file. Destructive — there is no undo.
git commit --amend --no-edit- Fold a forgotten fix into the previous commit. Safe only before pushing to a shared branch.
git reset --soft HEAD~3- Collapse three commits into staged changes so you can recommit them as one coherent change.
git revert <sha>- The right way to undo something already pushed. Creates a new commit instead of rewriting shared history.
git reflog- The safety net. Every HEAD movement for 90 days, which means a "lost" branch after a bad reset is recoverable.
Stashing and switching context
git stash push -m "half-done incremental logic"- Always message your stashes. An unlabeled stash list is write-only storage.
git stash pop- Applies and drops the top stash. Use apply instead when you want to keep it for another branch.
git worktree add ../hotfix main- A second checkout sharing one clone. Ideal when a dbt hotfix interrupts a long-running refactor.
Repo hygiene for data projects
# .gitignore target/ dbt_packages/ logs/ .env *.duckdb- dbt artifacts, local databases, and env files must never be committed. profiles.yml with passwords is the classic leak.
git rm --cached .env- Untrack a file already committed. Rotate the secret too — it stays in history until the history is rewritten.
.pre-commit-config.yaml with sqlfluff and detect-secrets- Formatting and secret scanning before the commit lands beats catching it in review.
git config --global core.autocrlf input- On Windows, prevents CRLF churn that makes every SQL file look fully rewritten.
Collaboration patterns
git rebase -i origin/main- Clean up a messy branch before review. Never rebase a branch that others have already pulled.
git cherry-pick <sha>- Move one fix to a release branch without dragging along unrelated commits.
git merge --squash feature/x- Squash-merge keeps main readable with one commit per change — a good default for dbt repos with noisy iteration.
CODEOWNERS- Route model directories to the owning team so warehouse changes cannot merge without a domain reviewer.
From DataLane — tutorials at/blog, practice SQL live in theplayground.