Git squash all commits on a branch
This article provides guidance for squashing all completed commits into a single commit.
Suppose we have made a bunch of commits on a topic branch, and now we need to clean it up by grouping all the commits on this branch into one commit. Git calls this squashing.
The idea behind squashing all commits is as follows.
- Use a soft reset to reset all changes.
- Re-add the changes.
- Submit a new message.
Git soft reset changes
A soft reset will undo all commits without removing newly added files.
To do this, we have to find the original commit where the feature branch was, usually the name of the original branch, main
e.g.
$ git log
commit a856ee456967a942ab379b27a4839962f88b92ce (HEAD -> feature/long-features)
Author: Cuong Nguyen
Date: Mon Dec 27 20:53:18 2021 +0700
Feature 2.3
commit 6f1599a18691906ed148dc40d2d290aaeceeaa5c
Author: Cuong Nguyen
Date: Mon Dec 27 20:53:03 2021 +0700
Subfeature 2
commit 94e35bae85f395c62fdaaa1aeaedbb11d2c94375
Author: Cuong Nguyen
Date: Mon Dec 27 20:52:39 2021 +0700
Subfeature 1
commit 9265e3bd97863fde0a13084f04163ceceff9a9d0 (grafted, tag: v1.0.0, branch-off-from-tag-v1.0.0)
Author: Cuong Nguyen
Date: Sun Dec 19 19:33:07 2021 +0700
Merge pull request #1 from stwarts/feature/shared-branch
In this example, we found the branch that was checked out feature/long-features
from the commit SHA 9265e3bd97863fde0a13084f04163ceceff9a9d0
(or from the branch name ) .branch-off-from-tag-v1.0.0
To reset all modifications, use git reset --soft 9265e3bd97863fde0a13084f04163ceceff9a9d0
or git reset --soft branch-off-from-tag-v1.0.0
.
$ git reset --soft 9265e3bd97863fde0a13084f04163ceceff9a9d0
$ git status
On branch feature/long-features
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: sub_feature_1.txt
new file: sub_feature_2.txt
--soft
The -p option instructs git to reset the commit and keep the new files.
Add back changes
git add -A
is the command to use.
-A
option specifies that all changes will be added.
submit
The final step is git commit -m <message>
to generate a new commit using .
$ git commit -m 'Squash 3 commits into 1'
[feature/long-features 8cc336c] Squash 3 commits into 1
2 files changed, 2 insertions(+)
create mode 100644 sub_feature_1.txt
create mode 100644 sub_feature_2.txt
$ git log
commit 8cc336c6d1b2e6ed55470f99b040d6835ec655e5 (HEAD -> feature/long-features)
Author: Cuong Nguyen <cuong.nguyen@oivan.com>
Date: Mon Dec 27 21:07:54 2021 +0700
Squash 3 commits into 1
commit 9265e3bd97863fde0a13084f04163ceceff9a9d0 (grafted, tag: v1.0.0, branch-off-from-tag-v1.0.0)
Author: Nguyễn Phú Cường <npcuong.011308@gmail.com>
Date: Sun Dec 19 19:33:07 2021 +0700
Merge pull request #1 from stwarts/feature/shared-branch
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git force pull
Publish Date:2025/03/29 Views:165 Category:Git
-
In this tutorial, we will learn how to force pull changes from a remote repository in Git. Sometimes, we may need to discard local modifications and replace them with updates from a remote repository in a collaborative development environme
Git pulls Master into the branch
Publish Date:2025/03/29 Views:193 Category:Git
-
When developing software using the Git tool, you can create different branches for different features. When you make changes to master, these changes are not automatically added to other branches. This article will explain how to pull all t
Installing Git in Cygwin
Publish Date:2025/03/29 Views:116 Category:Git
-
Git is considered an active, innovative and highly recommended distributed version control system with a fantastic standalone command line while providing us with advanced features and complete internal methods. What is Cygwin Cygwin is con
Displaying remote repository information in Git
Publish Date:2025/03/29 Views:139 Category:Git
-
This tutorial is about displaying information about remote repositories in Git. We use Git, a version control system, to track changes made to files in our project directories through Git repositories. Usually, local repositories are tracke
Deleting a remote repository in Git
Publish Date:2025/03/29 Views:130 Category:Git
-
When we commit the wrong data to the origin, push it to the origin and merge it to the current branch. But later we realize that we don't need to do the merge in that repo, so the question here is how to undo or revert the merge commit that
Setting up a Git remote repository
Publish Date:2025/03/29 Views:78 Category:Git
-
This article will explain how to add or delete remote repositories. A remote repository is a project hosted somewhere, such as Github/Gitlab. Adding a remote repository allows us to use a short name (alias) to execute commands instead of ty
.git Directory Explanation
Publish Date:2025/03/29 Views:65 Category:Git
-
In this article, we'll introduce Git folders .git . We'll cover why Git creates folders and what they contain. .git What are folders in Git ? Git is a widely used version control system. Git repositories store the changes you make in your p
Cherry-Pick Merge Commits in Git
Publish Date:2025/03/29 Views:105 Category:Git
-
When multiple developers from the same team are working on a project, regardless of the complexity of the project, handling and managing changes between Git branches becomes very difficult. Sometimes, we need to merge some specific commits
Fatal: Refusing to Merge Unrelated Histories error in Git
Publish Date:2025/03/29 Views:123 Category:Git
-
This article outlines the steps required to resolve the fatal: refusing to merge unrelated histories error in Git. We usually encounter such errors when trying to merge two unrelated Git projects into one branch. It pops up when the receivi