I have read through the basics of the git book trying to linearize the branch red from with the branch blue got merged into. To easier review the history I try to have only one branch and no junction. What options do I have to do this and what is best?
This answer suggests to reset --soft
, but here demo assumes it grew a lot since the merge.
It is ok to have the branch blue changes be added to red before af
or preferably after 9c
commit.
* 5356e62 vimdiff solved
* c1ccf86 red forward
* 51fbd33 Merge branch 'blue' into red
|\
| * 96575dc new file
| * 1d7f531 some line and blue branch 3
* | 9cc3291 new file
* | 92569c6 another commit
* | f8d3ab2 blue branch 3
|/
* af8b651 init
Caveat -- ALL methods of doing what you ask move a ref "backward" (effectively removing some commits); if those commits have been shared with other developers, it creates a problem. See the git rebase
documentation regarding recovering from upstream rebase, and understand that while this was documented in terms of rebase it applies to all history editing.
To the extent that git reset --soft
was a reasonable answer to the previous question, it's equally reasonable here.
git checkout 5356e62
git reset --soft 9cc3291
and now all the changes from 1d7f531 onward appear as staged changes, so
git commit
creating a new commit that "squashes" the old commits into one.
At this point if you have a branch pointing to 96575dc, then it will appear to be unmerged. OTOH if the parent branch was previously the only branch that could reach 96575dc, then 96575dc is now unreachable; but that's ok if you don't want that detailed history, because the relevant changes are in your new commit.
A somewhat more general solution is merge --squash
, and still more general would be rebase --interactive