Maintaining a clean and concise commit history is important for readability and collaboration. One powerful technique to achieve this is Git squash. Squashing allows you to combine multiple commits into a single commit, making your project history more simpler and easier to follow. In this article, we'll see more about what Git squash is, its benefits, and how to use it effectively.
What is Git Squash?
Git squash is the process of taking a series of commits and merge them into a single commit. This technique is especially useful when a feature branch has numerous small, incremental commits that clutter the commit history. By squashing these commits, you can present a more polished and cohesive history to your collaborators.
Git Squash-Merge Command Line
By using the git squash-merge command you can combine multiple commits into a single commit. After using the git squash command your repository will be clean and organized manner.Â
git merge --squash feature-branch
To merge the changes in the feature branch to your current branch we can use the above command.
When To Squash Commits?
Now we have a repository called GFG_VIDEO, which is an open-source video conferencing tool. GFG_VIDEO has released the 1st version of their tool with basic features such as peer-to-peer video calling and messages with the tag R_V1 (Green colored). After the R_V1 release team GFG_VIDEO started to add new features such as creating groups, group video calls, and fixing minor bugs from R_V1 such as call drops, etc. Now GFG_VIDEO is ready for their new release R_V2.Â
Git SquashÂ
Git SquashIf you observe, we have three commits from our initial commit to R_V1 (1st Release). After our R_V1 we have 3 commits for our R_V2 (2nd Release), It kind of looks untidy and difficult to follow. Here we can use the Squash concept and merge all the commits after R_V1 till R_V2 to a single commit which makes our repository log more tidy and easy to follow.
How To Squash Commits?
The below image shows we have 3 commits: Initial commit, Commit 2, and Version 1 Release. We’ve successfully released the 1st version (R_V1) of the GFG_VIDEO tool. After R_V1 new features are added and minor bugs are fixed from the previous release and the tool is ready for its 2nd release R_V2.
Git SquashÂ
Git SquashThe above image of the GFG_VIDEO log is after the 2nd version release. It can be observed after the Version 1 Release (tag: R_V1) there are 3 commits for the Version 2 Release. This kind of looks untidy, to make it simpler to read we can do a squash operation.
Let's perform squash now
git rebase -i HEAD~3
Note: Rebase is an action to rewrite commits and their history "-i" is to enter into an interactive mode of rebase HEAD~n states to perform our operation on n commits from HEAD.
Squashing By Interactive Mode
Upon entering the above command we'll get an interactive editor with all our selected commits which is where we'll be performing squash.
Git - SquashWe can see we have selected 3 commits at the beginning of the interactive editor, below that we can see the commands list such as pick, reword, edit, squash, etc. To squash  2nd and 3rd commit with 1st commit, so we'll change the first word from pick to squash. whichever commits we want to squash we have to change it to squash from pick.
Git - SquashAfter changing commits from pick to squash save the file and close it, Immediately another editor will be opened where we have to enter the latest commit message. Enter the latest commit message and comment on the remaining old messages.
Git - SquashAfter adding the latest commit message save the file and exit the file. Now it shows rebasing is successful.
Git - SquashNow if we see our GFG_VIDEO log we can observe our 3 commits after version 1 release are squashed into 1 commit.
Git - SquashSquashing By Merging With The –Squash Option
Git can combine the different changes into a single commit. The "--squash" option helps you to can merge multiple commits into a single commit.
Git combines all the changes into a single commit and applies it on top of the target branch.
Following are the steps to use "--squash".
Step 1: Create and Switch to a new branch.
Create a branch that you want to merge into which is also called the target branch. After creating a branch change to that new branch by using the below command.
git checkout new_branch_name
Step 2: Merge the branch.
Know to merge the branch by using the "--squash" option including the git merge option. As shown below.
git merge --squash <Branch>
Step 3: Resolve and Commit the changes.
In between you may face some merge conflict issues which to be resolved manually. After resolving the issue commit the changes to the local repository where this commit will have a single message with all the commit history.Â
Step 4: Push the changes to the remote repository.
By using the git push command push the recent commit to the remote repository.
The commit history will be simplified by using the "--squash" option which makes it cleaner.Â
Git Squash Last 2 Commits
By using the interactive rebase feature you can squash the last two commits.
For that follow the below steps:Â
Step 1: Switch to the branch in which you want to squash the last two commits. Run the following command which if the interactive rebase command passes the argument Head~2 specifies you want two squash the last two commits.
git rebase -i HEAD~2
Step 2: After running the above command an interactive rebase file will open which contains the commits you are about to modify. "Pick" is used at starting of each line to squash the last commit into the second-to-last commit and changes the word from "pick" to "squash" or "s" for the last commit.Â
pick <commit_hash_1> <commit_message_1>
squash <commit_hash_2> <commit_message_2>
Save and close the rebase operation will start.
Step 3: New text editor will open where you can modify the commit messages for the new squashed commit. After changing the commit message save and closing the file the rebase operation will be completed.
Git Squash vs Rebase
Git Squash | Git Rebase |
---|
You can combine multiple commits into a single commit. | Applies commits on top of the different base commits. |
After using git squash your commit history will be more clean and organized. | You can form a new branch by using previous commits. |
Must be done on private branches. | Rebasing can be done on a feature branch or shared branch. |
By using squishing you can maintain clean and logical commit history. | By using rebase command you can keep your branch up to date. |
Similar Reads
Git Push
Version control and collaboration are vital elements of any Git project. Git push is one of the most important commands in the Git ecosystem. You can make your updates accessible to others by sending your committed changes to a remote repository. The git push command, all of its options, and recomme
5 min read
Git - Squash Commits
In Git, squashing refers to combining two or more commits into a single commit. This is typically done to improve the organization and clarity of the commit history, especially when working on a collaborative project with other developers. In this article, we will review the feature git squashing, w
2 min read
Git - Status
Git is a powerful version control system that helps you to manage code changes and collaborate efficiently. One of the fundamental commands in Git is git status, which provides important information about the current state of your working directory and staging area. This article will explain the git
3 min read
Git Tags
Git provides a robust tagging system that allows you to mark specific points in a repositoryâs history. Tags are commonly used to mark important milestones, such as software releases, making it easier to refer to specific versions of the code.What are Git Tags?Git tag is a reference used to label sp
5 min read
Git - SubGit
Version control is an important aspect of modern software development, enabling teams to collaborate efficiently and manage code changes effectively. While Git has become very popular many projects still use Subversion (SVN). Transitioning from SVN to Git can be challenging, but SubGit simplifies th
3 min read
What is Git?
Git is a tool used to keep track of changes to files, especially the code of the projects. It is termed a distributed version control system because of its behaviour to allow multiple people to work on the same project, even if they are not connected to a common server. It was created by a person na
6 min read
Git LFS Install
Git Large File Storage (LFS) is a Git extension that helps developers manage large files in a more efficient way. Git LFS is especially useful for repositories that deal with large media files, such as images, videos, datasets, or binaries, which can slow down regular Git operations.In this article,
4 min read
Git - Head
In Git, understanding the concept of HEAD in Git is important for managing your repository effectively. In this article, we'll learn more about the fundamentals of Git-Head, its significance, and how it impacts various Git operations. What is Git Head?In Git, HEAD is a reference to the current check
6 min read
Git Tutorial
Git is an essential tool for developers, enabling them to manage and track project changes. Whether you're working on a solo project or collaborating with a team, Git keeps everything organized and under control. This Git Tutorial, from beginner to advanced, will give you a complete understanding of
12 min read
What is Git Pull?
Git pull is a command which is used to fetch and integrate the changes which are present in the remote repository to the local repository. Git Pull UsageGit pull is basically combination of git merge and git fetch which is used to update the local branch with the changes available in the remote repo
6 min read