fbpx

Mastering Git: How to Squash Commits and Streamline Your History

Close-up of computer code on a dark screen, featuring programming language and database commands for software development.

When you are working with version control. You may have multiple commits that you would like to combine into a single commit. Or perhaps you want to change their order, etc…

First you need to determine how far back you want to go. Open a command prompt or terminal window, navigate to your Git Repository.

next, type:

git log -10


The above command will show the last 10 commits. If that does not go far enough back, in increase the number 10

Once you have identified the commits you wish to squash run the git rebase command:

git rebase -i HEAD~10

The above command will open a git rebase window showing you the last 10 commits. Adjust this number as needed.

Each line shown will have the word pick here is an example of the rebase window:

Screenshot of a Git rebase interactive session showing a list of commit actions, including commands like pick, reword, edit, squash, fixup, exec, and drop, with explanatory notes.

Any commits you wish to squash, replace the word “pick” to the letter “s”

Be sure to leave the last commit in the list set to pick and the first commit in the list set to pick. As you squash the commits, they will be combined into the previous commits. You need to leave something for it to combine into. If you don’t do this, you may get an error.

In our example, we can really only squash the middle commit since we have 3. If you want to squash that first commit, add another commit before squashing.

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At Inventive HQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.