AAlechin
AAlechin

Reputation: 31

Squash commits with commits from merged branch in between

Have situation like this on branch BRANCH_SOMETHING:

FFF - 6th commit
EEE - 5th commit
DDD - 4th commit
Y commit from master
X commit from master
CCC - 3rd commit
BBB - 2nd commit
AAA - 1st commit

How to squash only my commits?

Upvotes: 0

Views: 126

Answers (2)

unixia
unixia

Reputation: 4320

There are two things you can do. (Taking into consideration that you do not want to squash the master commits into yours)

  • Two commits

One commit will squash 1,2,3 commits, the other one will squash 4,5,6.

  • Apply some branch magic

You can

  1. Undo the rebase or merge (see reflog ) which led to the current structure of your branch (commits from master interleaved between yours).
  2. Squash all your commits together.
  3. Do a rebase or merge again with the master.

If the current branch structure is already there on the remote then you will have to force push after doing the changes in any of the above mentioned ways.

Upvotes: 0

Romain Warnan
Romain Warnan

Reputation: 1042

The command you have to run is git rebase -i AAA~ (note the ~) It will open your default text editor with the following rebase programm :

pick AAA 1st commit
pick BBB 2nd commit
pick CCC 3rd commit
pick X commit from master
pick Y commit from master
pick DDD 4th commit
pick EEE 5th commit
pick FFF 6th commit

You must edit the text to look like that :

pick X commit from master
pick Y commit from master
pick AAA 1st commit
squash BBB 2nd commit
squash CCC 3rd commit
squash DDD 4th commit
squash EEE 5th commit
squash FFF 6th commit

Save and exit the text editor.

Git will run and then open the text editor again giving you the opportunity to edit the commit message :

# This is a combination of 6 commits.
# This is the 1st commit message:

1st commit 

# This is the commit message #2:

2nd commit

...

Edit the commit message, save and exit.

Finally the history will look like that :

GGG squashed commit
Y commit from master
X commit from master

Note that you can also place commit X and Y after the squashed commit, jut edit the rebase programm accordingly.

Upvotes: 2

Related Questions