← Back to Blog

Git Best Practices for Modern Engineering Teams

A visual guide to Git branching: staging vs production setups, GitFlow vs Trunk-Based Development, and the commands that matter, with diagrams and examples.

Git does more than track file changes. It's how you coordinate with your team, recover from mistakes, and control what goes to production.

This guide covers:

  • Proper branch structure (main vs develop)
  • GitFlow vs Trunk comparison
  • Clean workflow discipline
  • The Git commands you'll actually use (with visuals and real-world examples)
  • Good Git habits

Branching Model: Production vs Staging

  • main → Production (real users)
  • develop → Staging (QA/testing)
  • feature/* → Active development
  • hotfix/* → Emergency production fixes

Branch Flow

main (Production)develop (Staging)feature/xfeature/yRelease

GitFlow vs Trunk-Based

FeatureGitFlowTrunk
Staging branchYesNo
ComplexityMediumLow
SpeedModerateHigh
Best forEnterprise appsFast SaaS

Git Commands You Should Know (With When-To-Use Examples)

1. Git Stash

Temporarily save uncommitted changes.

Working dir(uncommitted)stashStashpopWorking dir(changes back)

git stash
git stash pop

When to use: You are working on a feature, and suddenly you need to switch branches, a hotfix is urgent, or you accidentally started coding in the wrong branch. Instead of committing unfinished work:

git stash
git checkout develop

Later:

git checkout feature/auth
git stash pop

Think of stash as a temporary drawer for messy work.

2. Git Rebase

Replays your commits on top of another branch.

Before:develop: C1 C2 C3feature: A BAfter rebase:A' B' on top

git checkout feature/auth
git rebase develop

When to use: Your feature branch is behind develop. Instead of merging (git merge develop), you rebase to keep history clean: git rebase develop.

Example scenario: develop has 5 new commits, you want your branch on top of the latest code, and you want a linear, professional history. Use rebase before opening a PR.

⚠️ Do NOT rebase shared public branches.

3. Git Revert

Safely undo a commit without rewriting history.

okabc123(bad)okrevert abc123new(undo)

git revert <commit-hash>

When to use: Production bug introduced by a commit (e.g. commit abc123) that breaks login. Instead of git reset --hard, do:

git revert abc123

This creates a new commit that cancels the bad one. Safe for main.

4. Git Cherry-Pick

Apply a specific commit from one branch to another.

developabc456 (fix)maincherry-picksame fix

git cherry-pick <commit-hash>

When to use: A bug fix exists in develop, production needs it urgently, and you don't want to merge entire develop. Cherry-pick just that fix:

git checkout main
git cherry-pick abc456

Perfect for hotfixes.

5. Git Rebase (Interactive)

Clean up commit history before merging.

Before:fix typofix againreal fixAfter (squash + reword):fix(auth): correct validation logic

git rebase -i HEAD~3

When to use: Your commit history looks like "fix typo", "fix again", "real fix". Before opening PR, clean it up: squash commits, reword messages. Result: one professional commit like fix(auth): correct validation logic.

6. Git Reset

Move HEAD back; --soft keeps changes staged.

Before:HEAD (last commit)After reset --soft HEAD~1:HEAD (changes in staging)

git reset --soft HEAD~1

When to use: You committed too early and want to modify it. Soft reset keeps changes staged.

⚠️ Avoid hard reset on shared branches.

Wrapping Up

Good Git habits give you predictable releases. They make rollbacks easy and PRs clean. And they leave you with a readable commit history.

Remember:

  • main = production
  • develop = staging
  • Rebase keeps history clean
  • Revert rolls back safely without rewriting history
  • Stash: temporary storage for uncommitted work
  • Cherry-pick: pull a single fix across branches

How a team uses Git says a lot about how they ship software.

Share
X LinkedIn HN
UI

Umur Inan

Principal Software Engineer

Backend engineer focused on JVM systems, distributed architecture, and the failure modes that only show up in production. I write about what I learn building and breaking things at scale.

👁 0 3 min read

Comments (0)