Git Rebase and Squash: A Step-by-Step Tutorial

14 Sept 2023

Depending on your work environment or how many people work in same repository you may come across needing to git rebase & git stash your commits.

What is Git Rebase?

Git rebase same as git add, commit etc is one of the commits but less known. The command is used to modify or clean up the commit history of a branch. Thus you can

Why is Git Rebase Used?

Cleaner History: Rebase can help create a cleaner and more linear commit history, which can make it easier to understand and review changes.
Avoiding Merge Commits: Rebase can be used to avoid creating additional merge commits, which can clutter the commit history.
Integration: It is commonly used to integrate changes from one branch (often a feature branch) into another (usually the main or development branch).

Let’s say your on your feature branch you run git log to see how much commit’s you have by running git log.
In my case I had 12 commits and had to merge it to one

Then I run this command

git rebase -i HEAD~11

If you have 12 commits and you want to squash them into one, you should use git rebase -i HEAD~11. The HEAD~11 specifies the commit range from the current HEAD (the latest commit) back to the 11th commit, which includes a total of 12 commits.

You should have something similar after in your terminal.
Now you pick and squash them usually latest on top is used.

pick abc123 Commit message 1
squash def456 Commit message 2
squash ghi789 Commit message 3
pick xyz987 Latest commit message 12

One’s saved and existed run a simple use git push --force

What is Git Squash?

Git squash is a Git technique used to combine multiple commits into a single, more meaningful commit. It helps streamline your commit history by reducing the number of individual commits.

Why is Git Squash Used?

Code Review: A squashed commit with a clear, concise message can make code reviews easier for both you and your collaborators.
History Clarity: It enhances the clarity and readability of your project’s commit history, making it easier to understand the evolution of the codebase.

Git squash is typically used in conjunction with Git rebase, especially during an interactive rebase.

Write & Read to Earn with BULB

Learn More

Enjoy this blog? Subscribe to Mozes721


Mastering Git rebase and squash is like tidying up your code history, streamlining it for a cleaner and more organized repository. It's a valuable skill that every developer should have in their toolkit.
Git is for developers and I ain't one
Curious and interesting article. Thank you
What's the difference between git or github
Good enough article for a specific type of audience