Sub topic 5
Merge, squash, or rebase: what your history will look like
This chapter assumes you know basic Git commands (add, commit, push, pull), can read a commit log, and have used pull requests on a host such as GitHub. You have seen how merge and rebase work separately. Here you compare what each integration method leaves behind on main.
Three ways to integrate a branch
When a feature branch is ready, you can bring it into main in three ways. Each one produces a different history, and that history is what your team will read for years when it runs git log, git bisect, or git revert.
Merge commit
A merge commit keeps every feature commit and adds one commit with two parents. The branch stays visible in the graph.
git switch -c feature/login-form
# edit files
git add .
git commit -m "Add login form"
git switch main
git merge --no-ff feature/login-formWithout --no-ff, Git fast-forwards when main has not moved, and no merge commit is created. The flag forces one, so the history records that a branch existed.
Squash merge
A squash merge combines all branch changes into one new commit on main. The individual commits do not appear in main, and the new commit has only one parent.
git switch main
git merge --squash feature/login-form
git commit -m "Add login form"git merge --squash stages the changes but does not commit. You write the message yourself. Git also records no link to the branch, so git branch --merged will not report it as merged. Delete it with git branch -D.
Rebase
Rebasing replays your commits on top of the latest main. Each commit is copied with a new hash, so the result is a straight line.
git switch feature/login-form
git fetch origin
git rebase origin/main
git push --force-with-leaseThe push needs --force-with-lease because the branch's commit hashes changed. This option refuses to overwrite commits on the remote that you have not fetched, which makes it safer than --force.
Comparing the results
| Method | Commits on main | Graph shape | Per-commit detail kept | Reverting the feature |
|---|---|---|---|---|
| Merge commit | All, plus a merge commit | Branches and joins | Yes | Revert the merge commit |
| Squash | One | Linear | No | Revert one commit |
| Rebase | All, copied | Linear | Yes | Revert each commit |
Common errors
- Squashing a long branch. One commit with 40 files is hard to review later and hard to bisect.
- Reusing a squashed branch. After a squash, Git does not know the branch was merged. If you keep committing on it, the old changes appear again and cause conflicts. Delete the branch and start a new one.
- Rebasing shared commits. Rewriting commits that others have pulled forces them to repair their copies.
- Reverting a merge commit blindly.
git reverton a merge needs-m 1to choose the parent to keep.
Exercise
Your team wants a linear main where each pull request is one commit, so that reverting a feature is one git revert. Which method fits, and what must you do with the branch afterward?
Show answer
Use a squash merge. Afterward, delete the feature branch, because Git does not record it as merged and reusing it would bring back the already-squashed changes.