-1

I'm rebasing a branch. I want to squash all commits into one.

git rebase -i --root

All commits besides the first one are set to squash.

Along the way, some conflicts pop up, as if I were merging. Why does it happen? I thought git simply applies one commit after the other.

John Smith
  • 3,863
  • 3
  • 24
  • 42
  • 1
    `rebase`s can have conflicts just like `merge`s do. In both cases, some sets of lines have been changed on both branches in such a way that Git cannot resolve the changes without human intervention. – 0x5453 May 15 '20 at 21:17
  • 1
    So the question really is: what is rebase really? You could have searched on that. Here's a particularly good answer (ignore the first part and just read after the heading "Long"): https://stackoverflow.com/a/51565608/341994 – matt May 15 '20 at 21:19

2 Answers2

1

It is like merging because it is merging. Consider by way of example:

A <- B <- C
      \
       \<-D

Suppose you ask to rebase D on top of C. Git asks itself what is the difference between B and D: what would you have to do to B in order to get to D? It then "replays" that difference onto C: that is, it tries to generate a new commit that does that same thing with respect to B, but is attached to C.

Well, depending on what C did to B, it may not be obvious how to do that. A change made by C with respect to B can be "in the way" of making a change to B. If C changed line 1 of a file in B to "hello" and D changed line 1 of the "same" file in B to "goodbye", what now should this new commit do in relation to C?

If you think the answer is obvious — that is, if you think B or D is "right" in this regard — then just resolve the conflict. Conflicts are not bad! They just answer questions where git fears that its answer might go against your wishes.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

By far, the easiest way to do this is:

git merge base-branch # merge your base branch
git reset--soft base-branch # move branch pointer, do not change files
# at this point, all differences between base branch and your branch are on index
# ready to be committed
git commit -m "my squashed feature"

And you are done.

eftshift0
  • 26,375
  • 3
  • 36
  • 60