Git Merge in only one commit
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want all the changes from a branch to land as one commit, what you usually want is a squash merge. That keeps the final branch history compact by combining the net effect of many feature-branch commits into one new commit on the target branch.
What a Squash Merge Does
A regular merge preserves the original branch history. A squash merge takes all the changes introduced by the feature branch and stages them as one combined change on the current branch.
After this, main gets one new commit containing the combined result of feature-branch, but it does not get the original feature-branch commit structure.
Why It Is Not the Same as a Normal Merge
With a normal merge commit, Git records both parent histories. With --squash, Git applies the diff but does not create a real merge commit connecting the histories.
That means:
- the target branch history stays compact
- the individual branch commits are not preserved there
- future merges may not see the squash commit as an actual merge relationship
This last point matters if the same branch will continue evolving after the squash merge.
When Squash Merge Is a Good Fit
Squash merging is useful when a feature branch contains many small commits such as:
- fix typo
- address review comment
- try approach B
- remove debug output
Those commits may have been useful during development, but not valuable in the permanent history of main.
Interactive Rebase Is a Different Tool
Sometimes you do want a cleaner branch history, but you still want to preserve meaningful intermediate commits. In that case, use interactive rebase before merging:
That lets you combine messy commits into a few meaningful ones. After that, you can do a normal merge or fast-forward merge depending on your team's workflow.
So the choice is:
- '
git merge --squashfor one final commit' - '
git rebase -ifor a cleaned-up but still multi-commit history'
Pull Requests and Hosting Platforms
Most Git hosting platforms expose this as "Squash and merge" in the pull request UI. That produces the same general outcome as git merge --squash locally: one commit lands on the target branch, usually with a message derived from the PR title or combined commit messages.
The UI option is often safer for teams because it keeps the decision visible in the review workflow instead of depending on local command usage.
Be Aware of the Tradeoff
A squash merge creates cleaner target-branch history, but it also hides the intermediate reasoning captured by individual commits. That is not always bad, but it is a tradeoff.
If those intermediate commits matter for debugging, auditing, or understanding the evolution of a complex feature, preserving them may be more valuable than a single tidy commit.
Common Pitfalls
- Expecting
--squashto create a real merge commit in the graph. - Squash merging and then continuing work on the same branch without understanding the future merge implications.
- Using squash merge when the individual commits are already clean and meaningful.
- Confusing interactive rebase with squash merge even though they solve different history problems.
- Treating "clean history" as automatically better than traceable history.
Summary
- To land a branch as one commit, use a squash merge.
- '
git merge --squashstages the combined changes, then you create one commit.' - Squash merging keeps history compact but does not record a real merge relationship.
- Use interactive rebase instead if you want a cleaner multi-commit branch history.
- Choose based on whether readability or commit-by-commit traceability matters more.
Related reading
- Git merge reports Already up-to-date though there is a difference
- Git mergetool generates unwanted .orig files
- git mergetool reports No files need merging
- git mv and only change case of directory
- Git name and email address configuration
- Git Needed a single revision error
- Git number of commits per author on all branches
- Git on Windows How do you set up a mergetool?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.