Suppose we have the following scenario:
- main branch
- feature branch for new improvements
In the feature branch the commit history looks like this (main branch updates during the development process of feature branch and we want to keep feature branch up to date with main):
- E <- (HEAD, feature branch)
- Merge main into feature
- D
- C
- Merge main into feature
- B
- A <- first commit on feature branch
Now we want to squash all of these commits into a single commit. When I tried to use git rebase -i HEAD~7 => a list of 9 lines which contains the new commits from feature branch (A, B, C, D, E) and also the commits that were merged from main (not the merge commits the actual commits).
- pick A
- pick B
- pick New_commit_from_main_1
- pick C
- pick D
- pick New_commit_from_main_2
- pick New_commit_from_main_3
- pick E
When I tried to use git rebase -i main => a list of 5 commits that does not contain the merge commits or the commits taken from main as in the above example
- pick A
- pick B
- pick C
- pick D
- pick E
I don't understand why is this happening. I would expect the following list of commits:
- pick A
- pick B
- pick Merge main into feature
- pick C
- pick D
- pick Merge main into feature
- pick E
git rebase -i main git rebase -i HEAD~7
The default behavior of
git rebaseis to ignore merges (as in: completely drop them from the list of rebased commits, be it with or without the--interactiveflag).This behavior is pretty surprising when you are first confronted to it :)
If you want to handle merges in your rebase history, you can use the explicit
-r|--rebase-mergesoption :Another option could be to try to squash all the feature commits into 1, then add the merge result on top of it.
If the merges from master didn't result in a lot of conflicts, then this should be doable :
Ecommit,git rebase -i HEAD~7, remove the commits coming from master from that list, mark commitsB .. Etosquash,git rebasedo its job-> if you don't have too many conflicts, you should have the "content of feature" condensed in one single commit
E(the one you wrote down at step 1.)