Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rebase to squash multiple commits on same branch

Tags:

git

Can we use rebase to squash multiple commits into one single commit on the same branch?

Taking an example, i have created two topic branches - issueA_1 and issueA_2 from master. I make a couple of commits in both branches, as can be seen in the diagram(i am using commit names here instead of commit hashes for brevity) :

           -->  (issueA_1) - commitX1
          /
(master)--
          \
           -->  (issueA_2)
                   |
                commitY1
                   |
                commitY2

Then i do :

git checkout issueA_2
git rebase -i issueA_1

I change the rebase file to :

pick commitY1
fixup commitY2

After this rebase, the commit history looks like this :

           -->  (issueA_1) - commitX1
          /
(master)--
          \
           -->  (issueA_2)
                   |
                commitX1
                   |
                commitY1
                   |
                commitY2

I don't need the branch issueA_1 anymore, so i do :

git branch -D issueA_1

The commit history of issueA_2 isn't pretty enough to be merged into master yet. I want commitX1, commitY1 and commitY2 of this branch to be squashed into 1 single commit before i merge into master. Is this possible?

like image 461
faizal Avatar asked Aug 15 '14 09:08

faizal


1 Answers

Yes, the simplest way I find is to do:

git rebase -i HEAD~3

This will let you review the last 3 commits and you can then squash them.

like image 120
Ilion Avatar answered Sep 22 '22 14:09

Ilion