DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Developer Git Commit Hygiene
  • Understanding Git
  • Top 35 Git Commands With Examples
  • Git Reset HEAD

Trending

  • Persisting Cookies Across JAX-WS Service Calls Using a Custom SOAP Handler
  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy
  • Building a Windows App for a Resilient SSH SOCKS Proxy and Selective Browser Routing
  • OwlVigil Open-Sources Its Go SDK for Enterprise AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to git squash in 6 Steps

How to git squash in 6 Steps

Learn how to keep your commit history neat and tidy with the squash git command.

By 
Amanuel G. Shiferaw
·
May. 30, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

When using a source code management system, we mostly intend to create a new branch to implement functionalities rather than always pushing everything to the main branch. In my opinion, this is the right way to do it — spoiling the branch history with every small commit might make things very hard, especially when there are a number of developers working together.

But how can we keep the main branch history tidy?

Let's see how to to force compress (squash) your git history in 6 steps.

Step 1: git branch –avv: This command checks remote and local branches verbosely.

Step 2: git shortlog (shortlog -10): This command lists 10 commits starting from the HEAD. Before making any changes, it's smart to create a copy of the branch we are on. This not only gives us a backup in case things go wrong, but it also helps us check whether our operation was successful. To do that, we'll use the following command:

  • git branch -b current new_branch: This command's output will be empty if the operation ended as intended.git squash 1Image title

Step 3: git rebase -i HEAD~2: Rebase to the 2nd commit from the HEAD so that the ones in the middle will be merged into the previous commit.

A file will pop up showing commits from the HEAD to the selected commit — and a list of commands that can be applied to these commits. In our case, you should change “pick” to “s” on the specific commit you want to merge to a predecessor commit as shown on the following figure.

todo popup

Step 4: Select operations: Once you are done, save and exit from the text editor.

Image title

Step 5: Add commit message: A second window will appear, prompting you for a commit message. Put the message string in and save and exit again. If the window stays the same after closing, press enter and it will continue.

Step 6: Validating the operation. This step will help us to make sure that we have completed the operation (squashing) correctly.  Check the diff between the two branches. git diff feature01 squash-feature01. Image title

Note: The above command should return an empty output. The two branches should be equal except the commit history

We saw that everything has gone as intended (since there is no code difference between the original and the squashed branch). Let’s check the commit history:

check commits

Now it is clear that we have merged the last two commits together and gave it a new commit message.

Step 7: Merge to main branch: Check out the main branch and merge

merge

Git Commit (data management) Branch (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Developer Git Commit Hygiene
  • Understanding Git
  • Top 35 Git Commands With Examples
  • Git Reset HEAD

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook