Git: squashing many commits into only one

Git: squashing many commits into only one

Sometimes when you are doing some tests or trying some new configurations in your project you end up with many commits with generic messages or maybe you were working on a big feature and before creating your pull request you need to update your branch with the latest master branch.

– Attemp 1 (made by you)
– Attemp number 2 (made by you)
– Try 1 (made by you)
– Bug fix (made by someone else)
– Try 2 (made by you)
.
.
.

But in order to have a clean working directory you can do some squashing there following the directions bellow:

Important Note: The advantage of doing it this way is that the commits don’t need to be consecutive but in case you have consecutive commits as my friend @IvanChukitow said in facebook in that case you can do it like so:

git rebase -i HEAD~<number_of_commits_you_want_to_squash>

So let’s continue with the other way to do it in spite of the commits order.

Let’s say that your branch was created based on the master branch and it’s called features/super-duper and you have 5 or more commits there but you want only one so let’s create only one commit with all those changes then:

1. Create a new branch from master called features/super-duper-squash
2. switch to the new branch

git checkout features/super-duper-squash

3. Make the squashing

git merge features/super-duper --squash

Then you can check the status of your working directory just to confirm that all your changes are there:

git status

If everything looks fine then you can create only one commit:

git commit -m 'Implements super super feature'

And that’s it.

Let me know if you know a different way to do this process I bet there is more than one.

H.

No Comments

Post A Comment