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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Understanding Git
  • Top 35 Git Commands With Examples
  • Git Reset HEAD
  • Git: Basic Terms and Commands Explained

Trending

  • Running Unit Tests in GitHub Actions
  • Five Tools for Data Scientists to 10X their Productivity
  • Creating a Deep vs. Shallow Copy of an Object in Java
  • TDD With FastAPI Is Easy
  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.

Amanuel G. Shiferaw user avatar by
Amanuel G. Shiferaw
CORE ·
May. 30, 19 · Tutorial
Like (3)
Save
Tweet
Share
11.26K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Understanding Git
  • Top 35 Git Commands With Examples
  • Git Reset HEAD
  • Git: Basic Terms and Commands Explained

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: