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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

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

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Start Coding With Google Cloud Workstations
  • Introduction to Retrieval Augmented Generation (RAG)
  • System Coexistence: Bridging Legacy and Modern Architecture
  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 user avatar
Amanuel G. Shiferaw
DZone Core CORE ·
May. 30, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.7K 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

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

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!