Git

How do I Squash my Last N Git Commits Together?

Developers can add changes to the GitHub hosting service through the local repository. They can push or pull changes according to the requirements. However, while making changes, you may need to save them in the repository by committing.

Git enables developers to merge the commits by squashing, which is the process of combining more than one commit into a single commit. You can perform this operation at any time using the Git Rebase feature, such as the “$ git rebase -i HEAD~1” command.

This post will explain the method to squash the last N Git commits together.

How do I Squash my Last N Git Commits Together?

To squash the last N number of Git commits together, first, move to the Git local repository and initialize it. Then, create and track the file to the staging area. Commit the added changes to the Git repository. Next, view the Git log history and reset the HEAD pointer position. Merge the commits by executing the “$ git merge –squash <head-index>” command. Lastly, commit changes and squash the most recently merged commits by executing the “$ git rebase -i HEAD~1” command.

Now, let’s check out the procedure of the above-discussed scenario!

Step 1: Navigate to Git Repository
First, execute the “cd” command to move to the desired Git local repository:

$ cd "C:\Users\nazma\Git\Demo10"

Step 2: Create File
Create a new file in the Git local repository using the following command:

$ touch file2.txt

Step 3: Track File
Run the “$ git add” command with the file name to track into the Git local repository:

$ git add file2.txt

Step 4: Save Changes
To save and update the Git local repository, execute the “$ git commit” command with the “-m” option and add the desired commit message:

$ git commit -m "2nd file added"

Step 5: Create New File
Create a new file using the “touch” command in the Git repository:

$ touch file3.txt

Step 6: Track File
Next, track a newly created file into the staging area using the “git add” command along with the file name:

$ git add file3.txt

Step 7: Commit Changes
Execute the “git commit” command to save the added changes to the Git local repository:

$ git commit -m "3nd file added"

Step 8: Check Git log History
Check the Git log history by utilizing the following command:

$ git log .

Here, it can be observed that the two most recent commits are added to the repository:

Step 9: Reset HEAD
Now, execute the “git reset” command with the “–hard” option and specify the HEAD position where you want to reset it:

$ git reset --hard HEAD~2

As a result, the HEAD pointer position will be reset back to the recent two commits:

Step 10: Merge Commits
Now, execute the “git merge” command with “–squash” option to merge the commit on the current HEAD index:

$ git merge --squash HEAD@{1}

As you can see, the most recent commits are merged successfully:

Step 11: Update Repository
Commit the added changes to the repository and update it:

$ git commit -m "Two files added"

Step 12: Git Log History
Now, execute the “git log .” command to check the Git log history for the added changes:

$ git log .

According to the below output, the last most recent commits are squashed together successfully:

Step 13: Squash N Commits
Lastly, execute the “git rebase” command along with the “-i” option to squash the “N” number of commits together. For instance, we have specified the “HEAD~1” to squash the last commit together:

$ git rebase -i HEAD~1

When the above command is executed, the editor will open with some instructions. Add the required comment and save it to rebase and update the squashed commits:

That’s it! We have compiled the easiest method to squash the last N commits together.

Conclusion

To squash the last N number of commits together, first, navigate to the Git repository and initialize it. Next, create and track the file to the staging area. Commit the added changes to the Git repository. Then, reset the HEAD pointer position and merge the commits by executing the “$ git merge –squash <head-index>” command. Lastly, commit changes and execute the “$ git rebase -i HEAD~1” command to squash the most recently merged commits. This post provided the method to squash the last N commits together.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.