Context: Why Choosing Between Git Merge and Git Rebase Matters
Six months ago, our production project entered a crunch phase with 4–5 features being developed in parallel. At the time, everyone was in the habit of running git pull and git merge ad hoc. After just one two-week sprint, our git log --graph had turned into a spiderweb with over 40 “Merge branch ‘main’ into feature/…” commits. Whenever production hit a bug and we needed to track down the culprit, the team spent nearly an hour just untangling the commit history.
Our 8-person team decided to sit down and standardize our Git workflow. Instead of picking one over the other, we intentionally combined Git Merge and Git Rebase. The results were clear: merge conflicts dropped by about 60%, and Pull Request (PR) review times fell from 45 minutes to around 15 minutes because diffs were no longer cluttered with sync commits.
Understanding how these two commands actually work will save you from plenty of stressful moments when you accidentally tangle up a shared repository’s history.
Setting Up Git for the Team
Before rolling out the new workflow, everyone should be on Git 2.23 or newer for clearer error messages and full flag support. You can check your version quickly in the terminal:
git --version
# Recommended: git version 2.34.0 or newer
By default, git pull creates a noisy merge commit whenever you pull new changes from the remote into a branch you’re actively working on. To eliminate this, enable rebase on pull along with automatic stashing:
# Apply to all repositories on your machine
git config --global pull.rebase true
git config --global rebase.autoStash true
# Set default editor for interactive rebase (rebase -i)
git config --global core.editor "nano"
With pull.rebase and rebase.autoStash configured, pulling code becomes much cleaner. Git automatically stashes your uncommitted work, fetches and applies the latest commits, replays your local commits on top, and then restores your working changes.
How They Work and Our Hybrid Strategy
To combine these two commands effectively, you first need to picture how each one manipulates the underlying commit tree.
1. How Does Git Merge Work?
Git Merge preserves the entire history of both branches and ties them together. Depending on the state of the branches, Git handles this in one of two ways:
- Fast-forward merge: Happens when the target branch (such as
main) has no new commits since you branched offfeature. Git simply moves themainpointer forward to the latest commit onfeature. - 3-way merge: Happens when both
mainandfeaturehave new commits. Git finds their nearest common ancestor, reconciles the differences, and creates a new merge commit with two parent commits.
# Switch to main and merge the feature branch, forcing a merge commit to preserve the milestone
git checkout main
git merge --no-ff feature/thanh-toan
The biggest upside I’ve noticed over the past six months is that the history reflects real-world chronology 100% accurately. The SHA-1 hashes of older commits remain completely intact. Suppose a payment feature deployed at 10 PM breaks production—you just run git revert -m 1 <merge-commit-hash> to safely roll back the entire feature.
On the flip side, if you repeatedly use merge to pull updates from main into your personal branch, the commit graph balloons and becomes very hard to read.
2. How Does Git Rebase Work?
Git Rebase works on an “uproot and replant” principle. Suppose you create feature/gio-hang from commit A on main. While you write two commits (C1, C2), a teammate pushes commit B to main. When you rebase, Git temporarily lifts C1 and C2 off, moves the base to commit B, and reapplies them as two brand-new commits, C1′ and C2′, with completely different commit hashes.
# On your personal branch, update it on top of the latest main
git checkout feature/gio-hang
git fetch origin
git rebase origin/main
If feature/gio-hang has already been pushed to the remote, you’ll need to use --force-with-lease after rebasing to push your updated history. Strictly avoid plain --force, as it can overwrite someone else’s work if they pushed to the branch in the meantime:
git push origin feature/gio-hang --force-with-lease
Thanks to rebase, the commit history stays strictly linear and remarkably clean. Before opening a PR, I often run git rebase -i HEAD~3 to squash three minor commits like “fix typo”, “wip”, and “test again” into a single clean commit that’s easy for reviewers to follow.
However, rebase carries a critical risk: it rewrites history. If you run rebase on a shared branch that others are actively working on, like main or develop, your teammates’ local histories will immediately diverge.
3. Our Production Hybrid Strategy
Instead of arguing over which command is better, our team locked in three simple rules and stuck to them for six months:
- Rebase on personal branches: When working on
feature/*orfix/*, always usegit rebase origin/mainto pull in new changes. Small conflicts are resolved cleanly step by step on your local machine. - Merge (–no-ff) into shared branches: When merging a PR into
mainordevelop, always create a Merge Commit (or use Squash & Merge if the PR has too many granular commits). This keeps clear boundaries around each feature, making auditing and rollbacks fast. - Never rebase shared branches: Never run
git rebaseon public branches that other team members have already pulled.
Monitoring Commit History and Recovering from Mistakes
Having a workflow in place still requires periodic checks to make sure no one drifts off track—and knowing how to recover data when someone makes a mistake.
At the end of each week, I usually run this command in the terminal for a quick look at the branch structure:
git log --oneline --graph --decorate --all -n 20
A healthy graph has a straight main trunk, with feature branches splitting off cleanly and closing back into main with a single merge commit.
What if a newer teammate accidentally botches a rebase and “loses” local code? Stay calm—Git keeps a log of HEAD pointer movements for at least 30 days for unreachable commits (before garbage collection). You can simply open reflog to turn back the clock:
# View the history of local operations
git reflog
# Restore to the state before the rebase (e.g., HEAD@{3})
git reset --hard HEAD@{3}
On the server side in GitHub or GitLab, we also enabled Branch Protection for main and develop. These rules block force pushes and require every change to go through a PR and pass CI checks before merging. With these technical guardrails in place, our repo history stayed spotless even as our PR volume doubled over six months.

