You have been working hard on that feature for many days and now it's done.
Unfortunately, you have made a lot commit and you want to squash them into one commit and raise that pull request.
As said in this tutorial you can do it in one command :
git rebase -i HEAD~[NUMBER OF COMMITS]
But How to know the number of commits you have made since the moment you started working on this feature?
git log you can list all the commit you made after creating your branch.
Let suppose your first commit hash is
33b14c62b and the last commit hash is
You can do it the old way by counting manually the number of commits between the two hashes but it is not always easy.
Git has a powerful command called
rev-list that lists commit objects in reverse chronological order
git rev-list 33b14c62b...33ad6cecf
With that command, you can get the list of all commit between the start hash and the end hash.
To count the number of line in the output of that command use the pipe and the following command:
| wc -l.
The final command will look like this :
git rev-list 33b14c62b...33ad6cecf | wc -l
Once you get the number of commits you would like to squash you can do the command mentioned in the introduction and combine your commit by deciding what you would like to pick and what you would like to squash.
If you have found 10 commits the command will be :
git rebase -i HEAD~10
I am sure, it is possible to combine the 2 commands into one by doing something like this :
git rebase -i HEAD~"$(git rev-list 33b14c62b...33ad6cecf | wc -l)"
Currently, it's not working because of the second command is returning a number with spaces, the next problem is to remove the space before the output of the second command.
If you know how it can be done, please let us know.
Do not hesitate to let us know what command you use to count the number of commits or any improvement which can be made to this blog post.
Resources used :