25 Oct 2017

Squash “N” Git Commits into single commit

2:55 PM git 0 Comment

Is it possible to squash previous commits into one commit with GIT?
Yes, It is possible
This is a great way to group certain changes together before sharing them with others.
Lets starts how to squash some commits into one.

Step1: enter the git log inside the project folder

purnaraot@nbmaclap24 savingstar-java (SAV-3681) $ git log
commit de6a275055709d9c86d38634561a755588e9aeec
Author: purnaraot Date: Fri Oct 6 15:09:32 2017 +0530

SAV-3681: member issue resolved

commit 0b1082c1a38ddcfb3c5542530da2f7824e6b1fdb
Author: purnaraot Date: Thu Oct 5 21:07:19 2017 +0530

SAV-3861: member is not in db issue changes to reject the member

commit 7101136bc7f06a236e8eb62f8a03e06518198ec3
Author: purnaraot Date: Thu Oct 5 16:13:35 2017 +0530

SAV-3681: adding find user method in casbah

You have a branch SAV-3681 here. You’ve already pushed 8198ec3 with the proof of concept of the “adding find user method in casbah”. After that you’ve been working to “member is not in db issue changes to reject the member”, including some changes in member issue. Now, you want to squash your last three commits in one to make your history look pretty.

Step2: After that you can enter the command and see the below output:


purnaraot@nbmaclap24 savingstar-java (SAV-3681) $ git rebase -i HEAD~3

This will open up your editor with the following:


pick 8e9aeec SAV-3681: member issue resolved
pick e6b1fdb SAV-3681: member is not in db issue changes to reject the member
pick 8198ec3 SAV-3681: adding find user method in casbah

# Rebase 0b1082c..8198ec3 onto 0b1082c (3 commands)

Now you can tell git what to do with each commit. Let’s keep the commit 8e9aeec, the one were we added our feature. We’ll squash the following two commits into the first one – leaving us with one clean commit with adding find user method, including the member is not in db and member issue resolved.

Step3: Change your file to this:


pick 8e9aeec SAV-3681: member issue resolved
squash e6b1fdb SAV-3681: member is not in db issue changes to reject the member
squash 8198ec3 SAV-3681: adding find user method in casbah

# Rebase 0b1082c..8198ec3 onto 0b1082c (3 commands)

# This is a combination of 3 commits.
# This is the 1st commit message:
SAV-3681: member issue resolved

# This is the commit message #2:

SAV-3681: member is not in db issue changes to reject the member

# This is the commit message #3:

SAV-3681: adding find user method in casbah

Step4: When done, save and quit your editor. Git will now squash the commits into one. All done!


purnaraot@nbmaclap24 savingstar-java (SAV-3681) $ git rebase -i HEAD~3
[detached HEAD 2c3b31b] build error
Author: purnaraot Date: Thu Oct 5 21:33:52 2017 +0530
1 file changed, 10 insertions(+), 8 deletions(-)
Successfully rebased and updated refs/heads/SAV-3681.
purnaraot@nbmaclap24 savingstar-java (SAV-3681) $

Step5: After save and quit your editor, you can enter git log you can see one commit including with 3 commits.


purnaraot@nbmaclap24 savingstar-java (SAV-3681) $ git log
commit 2c3b31b38ffe0647c29165139569cb53e1b6f9ef
Author: purnaraot Date: Thu Oct 5 21:33:52 2017 +0530

SAV-3681: member issue resolved

SAV-3681: member is not in db issue changes to reject the member

SAV-3681: adding find user method in casbah

Step6: After that you can push changes to remote with below command


purnaraot@nbmaclap24 savingstar-java (SAV-3681) $ git push origin -f SAV-3681

Leave a Reply