Git Squash Commits

Git Squash Commits

What is this Git Squash

First, let's understand what Git squash is. When we commit code and raise a pull request (PR), we often make multiple changes to it. When it comes time for final deployment or merging, we need to squash all of our commits into one. This is why we need to perform squashing - to combine all of the commits into one.

Let's get started!

Step 1:

First, we need to set up our terminal, any terminal that you use for your Github actions, whether it is WSL or PowerShell. First, you will open your terminal and navigate to the project you want to work on.

Step2:

Now, we need to select our branch because PRs cannot be squashed or updated without a branch. If our PR is in the same branch, then it's fine, but if not, we need to check the branch using the command git branch. If we are not in the same branch, then we need to change the branch by using the command git checkout <branch name> For example, my current branch is Keyboard_accessibility_fix.

Step3:

Now, To check the commits in the branch and decide which ones to squash, we can use the command git log --oneline to check the all commits in your branch.

Step 4:

Now we need to check how many commits we have in our branch and which ones we want to squash. To check the commits, we need to run git log --oneline command. Once we know which commits we want to squash, we can run the command git rebase -i HEAD~3, where 3 is the number of commits. Running this command will take us to an edit form where we can choose which commits we want to squash. The first commit, where we want to keep all the changes, should have pick written in front of it as it is the last commit where we are squashing all the previous ones. For the rest of the commits, we need to replace pick with either squash or s. Once we are done with the changes, we need to press ctrl+x and then choose whether we want to save the changes or not we have an option of Y and N (yes and no). Finally, press enter and the updates will be saved.

Step 5:

And here finally our all commits have been successfully squashed.

Importance of squash commits

Commit squashing is important in development because it allows developers to clean up their commit history and present a more organized and concise view of the work they have done. It helps to reduce clutter in the commit history and makes it easier for others to understand the changes made in the code.

Squashing commits also makes it easier to revert changes if needed, as all related changes are grouped together into a single commit. This makes it easier to track changes over time and makes the commit history more manageable.

Another important benefit of commit squashing is that it helps to maintain a clean and organized codebase. This is especially important when working with large teams or complex projects, as it can be difficult to keep track of all the changes made over time. By squashing commits, developers can ensure that the codebase is organized and easy to navigate.

Overall, commit squashing is an important practice that can help to streamline the development process, reduce clutter, and maintain a clean and organized codebase.