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 (
mainvsdevelop) - 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 developmenthotfix/*→ Emergency production fixes
Branch Flow
GitFlow vs Trunk-Based
| Feature | GitFlow | Trunk |
|---|---|---|
| Staging branch | Yes | No |
| Complexity | Medium | Low |
| Speed | Moderate | High |
| Best for | Enterprise apps | Fast SaaS |
Essential Git Commands (With When-To-Use Examples)
1. Git Stash
Temporarily save uncommitted changes.
git stash
git stash popWhen 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 developLater:
git checkout feature/auth
git stash popThink of stash as a temporary drawer for messy work.
2. Git Rebase
Replays your commits on top of another branch.
git checkout feature/auth
git rebase developWhen 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.
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 abc123This 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.
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 abc456Perfect for hotfixes.
5. Git Rebase (Interactive)
Clean up commit history before merging.
git rebase -i HEAD~3When 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.
git reset --soft HEAD~1When 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= productiondevelop= 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)