Rebase all my commits

Hi every one. I am working with rebase tool and I wanna know how this one works exactly. I have two branches - master and develop ones. So my task is to use rebase tool that all my commits will be combined into one.
My commits:

git hist
* [2019-01-09] [d658f11] | Fix #5 Final!!! {{Vasiliy I Ozerov}}  (HEAD -> develop, origin/develop)
* [2019-01-09] [c8957cc] | Fix #4 {{Vasiliy I Ozerov}} 
* [2019-01-09] [1346f7f] | Fix #3 {{Vasiliy I Ozerov}} 
* [2019-01-09] [ab12f7e] | Fix #2 {{Vasiliy I Ozerov}} 
* [2019-01-09] [847d11d] | Fix #1 {{Vasiliy I Ozerov}} 
* [2019-01-09] [a452cc0] | Simplified sum function {{Vasiliy I Ozerov}} 
* [2019-01-09] [721bbd4] | Added main server file {{Vasiliy I Ozerov}}  (origin/master, origin/HEAD, master)
* [2019-01-09] [d2594d0] | Initial commit {{Vasiliy I Ozerov}}

I have to rebase all of them, using pick/squash param that I shall get only one commit “Initial commit” at the end. Can someone explain me how I can do that?

1 Like

Hey @ntp-cloud, welcome to the community!

Rebasing is really useful for cleaning up history: dropping commits, squashing them, or rewording the message. It’s useful to have in your workflow toolbox. I don’t believe this is a complete solution, but hopefully it points you in the right direction:

I find the -i flag really helpful, it provides an intermediate prompt to clean up your history.

git rebase -i {HASH}

If you want to end up with a single commit, we will provide the hash for your first commit:

git rebase -i d2594d0

… which should put you into a prompt where you can keep/squash/etc. individual commits (similar to the following):

s d658f11 | Fix #5 Final!!! {{Vasiliy I Ozerov}}  (HEAD -> develop, origin/develop)
s c8957cc | Fix #4 {{Vasiliy I Ozerov}} 
s 1346f7f | Fix #3 {{Vasiliy I Ozerov}} 
... (clipped for brevity)
s 721bbd4 | Added main server file {{Vasiliy I Ozerov}}  (origin/master, origin/HEAD, master)
p d2594d0 | Initial commit {{Vasiliy I Ozerov}}

Here, I’ve labeled commits with s for squash and p for pick (the full set of options should be shown). Since everything is executed top-to-bottom, all squished commits will be melded into the previous, and the “Initial commit” will be picked, so everything will be squashed into a single initial commit.

There might be one additional complication. Since your origin/master is at 721bbd4 this might cause some issues if you try to rebase back to the master branch. Pushing with --force should technically resolve this, but I would generally recommend against it (it effectively rewrites history, which is problematic if you have collaborators).

1 Like