Back to Blog

Git Best Practices for Modern Engineering Teams

Git is more than version control. It's your collaboration engine, safety net, and deployment switch.

This guide covers:

  • Proper branch structure (main vs develop)
  • GitFlow vs Trunk comparison
  • Clean workflow discipline
  • Most important Git commands (with visuals and real-world examples)
  • Professional 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

Essential Git Commands (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.

Final Thoughts

Strong Git habits mean: predictable releases, easy rollbacks, clean PRs, and clear commit history.

Remember:

  • main = production
  • develop = staging
  • Use rebase for clean history
  • Use revert for safe rollback
  • Use stash for temporary storage
  • Use cherry-pick for selective fixes

Git discipline reflects engineering maturity.

Comments (0)