author avatar

nived.hari

Fri Oct 25 2024

Combining Commits with Git Squash
Squashing commits allows us to combine multiple related commits into a single one, helping to keep the commit history clean.

Let's say if we want to combine 3 separate commits which are related to same thing into one clean commit,

1. Checkout the branch


git checkout branch-name


2. Run the following command


git rebase -i HEAD~3


3. Modify the rebase file. Git will open a text window with last 3 commits


pick 7f9d4bf first commit
pick 3f8e810 second commit
pick ec48d74 third commit


pick means to keep the commit as is.
• To squash the second and third commits into the first one, change pick to squash / s for those commits.
4. After we save and exit, another text editor will pop-up with commit messages


# This is a combination of 3 commits.
# The first commit message:

fix for bug

# Commit message for #2:
Updated this

# Commit message for #3:
Added comments & updated README


Simply saving this will result in a single commit with a commit message that is a concatination of all 3 messages.
We can choose which one we want, or we can create a new message entirely.
5. Complete the Rebase
After saving, we now have a single commit representing the previous three.

#git #rebase