Skip to main content

Git squash merge explained

There are many ways to integrate changes in git: regular/normal git merge, git squash merge, git rebase etc. This article explains git squash merge by comparing it to regular merge.

Let’s use below example:
In the repository with default main branch, after two commits, a new feature branch is created.
Some work happened in feature branch. feature branch now has 2 commits that it shares with main branch, and three exclusive commits (exists only in feature branch).
In the meantime, others worked on main branch and added two new commits (exists only in main branch).

git branches

git log output of the main branch:

c72d4a9 (HEAD -> main) fourth commit on main
2c3dd61 third commit on main
0c2eec3 second commit on main
9b968e8 first commit on main

git log output of the feature branch:

786650f (HEAD -> feature) third commit on feature
21cbaf1 second commit on feature
677bc7f first commit on feature
0c2eec3 second commit on main
9b968e8 first commit on main

We want to merge the changes that exist on the feature branch into the main branch.
First let’s review regular merge then squash merge

Regular merge

# switch to main branch
git switch main

# merge two branches (commits on the top of these two branches)
git merge feature

If there are conflicting changes, merge conflict will arise.

Merge conflict example:

git merge feature
Auto-merging main.txt
CONFLICT (content): Merge conflict in main.txt
Automatic merge failed; fix conflicts and then commit the result.

We should resolve the merge conflict, stage the resolved state and commit.

git add

git commit -m <commit message>

If there are no conflicts, git merge command opens the editor and asks us to enter the commit message. After we close the editor, it is committed.

git merge feature
Merge made by the 'ort' strategy.

Alternatively, we can provide commit message while executing git merge command.

git merge feature -m 'merge feature into main'
Merge made by the 'ort' strategy.

Result:

It merges two branches. We can see it on the below graph.

git merge

merge commit has two parent commits:

Commit: a76ce17e8e36915d50edda2c42406e52cbdc876f
Parents: c72d4a92dbe381eb7bfed1551df3d449d25e7ceb, 786650f08684a2779d0e5c96cec54bdc59f10325
Author: Hujaakbar <username@xxx.com>
Committer: Hujaakbar <username@xxx.com>
Date: Thu Sat 23 2024

regular merge feature into main

git log history includes commits of both main and feature branches.

git log --oneline
a76ce17 (HEAD -> main) regular merge feature into main
c72d4a9 fourth commit on main
786650f (feature) third commit on feature
2c3dd61 third commit on main
21cbaf1 second commit on feature
677bc7f first commit on feature
0c2eec3 second commit on main
9b968e8 first commit on main

Commit history of the main branch changed. Now 21cbaf1 second commit on feature and 677bc7f first commit on feature commits (of feature branch) are inserted between 0c2eec3 second commit on main and 2c3dd61 third commit on main commits (of the main branch).

There is nothing wrong with regular merge. But some people find it confusing or unnecessary to include all the commits from feature branch, especially when multiple branches are merged into main branch on a regular bases. They prefer other ways of merging changes such as squash merge or rebase.

Squash merge

# switch to main branch
git switch main

# squash all the changes of the feature branch and merge them into main branch
git merge --squash feature

When you run above commands, if conflict arises, we should resolve it and
run git add command.

Conflict example:

git merge --squash feature
Auto-merging main.txt
CONFLICT (content): Merge conflict in main.txt
Squash commit -- not updating HEAD
Automatic merge failed; fix conflicts and then commit the result.

If there is no conflict, git automatically combines the changes and stages them. But git does not make a commit, even if we provide commit message.

Git squash merge example with no conflict

git merge --squash feature
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested

What does squash merge do differently?

squash merge operation takes all the changes made on the feature branch, and puts them onto the main branch. It does not merge commits. We can think like we never had a feature branch, rather we continuously worked on main branch and made lots of changes without committing.
After we run the squash merge command, we should save the combination of the changes by committing.

git commit -m <commit message>

The resulting commit will have a single parent. It won’t have any reference to feature branch.

Resulting commit:

Commit: d7f2115e36cc380dc63afc89238e90d83223d602
Parents: c72d4a92dbe381eb7bfed1551df3d449d25e7ceb
Author: Hujaakbar <username@xxx.com>
Committer: Hujaakbar <username@xxx.com>
Date: Thu Sat 23 2024


squash merge feature into main

The result looks like this:

squash merge result

As we can see on the above graph, two branches are not merging.

Important points to notice:

  • feature branch is untouched (it is not changed in any way).
  • git adds the changes to the stage but do not commit them, we should commit
  • resulting commit has single parent commit and it has no reference to feature branch.
  • git log command run on the main branch shows only commits made in the main branch. (clean looking linear history)
git log --oneline
d7f2115 (HEAD -> main) squash merge feature into main
c72d4a9 fourth commit on main
2c3dd61 third commit on main
0c2eec3 second commit on main
9b968e8 first commit on main

clean_git_log

Conclusion

Using squash merge is just a preference, not a right or a wrong way of merging. It can make the version history linear and easy to understand but it doesn’t mean it is always better. Keeping commit history is useful at times especially for undoing certain changes. So, you don’t have to use only one type of merge, rather you can choose when to use squash merge based on the content and context.

Comments

Popular posts from this blog

脱初心者! Git ワークフローを理解して開発効率アップ

Git – チーム開発に必須のバージョン管理システムですが、その真価を発揮するにはワークフローの理解が欠かせません。 色々な人は Git の使い方を良く知っていますが、Git を仕事やワークフローに統合する方法を余り良く知らない人もいます。本記事では、Git をワークフローに組み込むことで、開発プロセスがどのように効率化され、チーム全体のパフォーマンスが向上するのかを解説します。Centralized Workflow から Forking Workflow まで、代表的な 9 つのワークフローの特徴を分かりやすく紹介します。それぞれのメリット・デメリット、そして最適なユースケースを理解することで、あなたのプロジェクトに最適なワークフローを選択し、開発をスムーズに進めましょう! Centralized Workflow Feature branching/GitHub Flow Trunk Based Flow Git Feature Flow Git Flow Enhanced Git Flow One Flow GitLab Flow Forking Workflow 分かりやすくするために、同じコンセプトを説明するに一つ以上の図を使った場合があります。 Centralized Workflow 説明: 集中化ワークフローではプロジェクトにおけるすべての変更の単一の入力箇所として中央リポジトリを使用します。デフォルトの開発用ブランチは main と呼ばれ、すべての変更がこのブランチにコミットされます。 集中化ワークフローでは main 以外のブランチは不要です。チームメンバー全員がひとつのブランチで作業し、変更を直接中央リポジトリにプッシュします。 メリット: SVN のような集中型バージョン管理システムから移行する小規模チームに最適。 デメリット: お互いのコードが邪魔になり (お互いの変更を上書きするように)、プロダクション環境にバグをい入れる可能性が高くて、複数のメンバいるチームでこのフローを使いにくい。 地図: graph TD; A[Central Repository] -->|Clone| B1[Developer A's Local Repo] A --...

From Generic to Genius: Fine-tuning LLMs for Superior Accuracy in Snowflake

TL;DR: Cortex Fine-tuning is a fully managed service that lets you fine-tune popular LLMs using your data, all within Snowflake. While large language models (LLMs) are revolutionizing various fields, their "out-of-the-box" capabilities might not always align perfectly with your specific needs. This is where the power of fine-tuning comes into play. As it will be explained in this article, this feature empowers you to take a base LLM and customize it to excel in your particular domain. Here's the brief summary of why you might want to leverage Snowflake's fine-tuning capabilities: Unlocking Domain Expertise : Pre-trained LLMs are trained on massive, general datasets. Fine-tuning allows you to build upon this foundation and train the LLM further using data specific to your field, such as legal documents, medical records, or financial data. This empowers the LLM to understand complex terminology and patterns unique to your domain, leading to more accurate a...

How Wendy’s Successfully Penetrated the Japanese Market After Long Struggles

Wendy’s had long struggled to penetrate the Japanese market. Initially the Daiei Group tried to bring Wendy’s to Japan but failed. The next owner of Wendy’s’ Japanese franchise, Zensho Holdings Co. also failed miserably. However, Japanese-American entrepreneur Ernest M. Higa seems to have managed to do the task. This article will discuss the challenges Wendy’s faced when entering the Japanese market, how Ernie Higa addressed those challenges, macro environmental factors that impacted the success of the brand in Japan, future threats the Japanese fast food market is facing , and potential solutions. The prior challenges that Wendy’s faced when they entered the Japanese market There is no one-size-fits-all formula in business, especially when Japan is involved in the conversation. According to Japanese-American entrepreneur Ernie Higa, even if a company has a good product and good pricing, penetrating the Japanese market is more difficult compared to the US’s market. Foreign e...