1

I have a branch named myBranch, which I'm working on for a complex feature for a long time. After I finished my work and wanted to open a merge request, gitlab showed that 47 commits was done in my branch.

I thought the MR would be easier to understand if there was a single commit combining all the changes, because most of the individual commits were to try different things until it worked.

I heard about the term "squashing" before to combine multiple commits into one, so I decided that I could try and learn that.

I started with:

git rebase -i HEAD~47

It showed me a text file with a list of commits and choices like pick, squash or reword.

There were some old commits unrelated to my work in the text file, so I left them as "pick". I typed "reword" for my first commit in the branch, and changed the commit message to the feature I'm adding with my branch. I typed "squash" for the rest of them, up to the latest commit.

Then the rebase process continued. It stopped a few times for merge conflicts and asked me to fix then add them. I fixed the conflicts, added them using git add -A then continued with git rebase --continue.

After a few merge conflict fixes, process completed and I run git push origin myBranch --force, and tried to open a merge request using my branch.

Now it shows a whopping 101 commits, way more than my initial 47!

Instead of reducing the number of commits into 1, I actually increased them.

What did I do wrong?

5
  • 1
    No need for a rebase. Easiest thing is just to do a git reset HEAD~47 which will keep the contents of your working directly but point you to the commit 47-ago. Then simply re-commit the work and you're done.
    – TooTone
    Commented Nov 30, 2023 at 21:05
  • 1
    What would the merge request have looked like if you hadn't rebased? I mean, the fact that you've been working on this branch "for a long time", and you have evidently not merged main into this branch so it has not kept up with it, is suspect. Really, though, we'd need a lot more info in order to answer the question in detail, but my point is, I suspect that this has very little to do with the rebase (though note, please, that every commit that you did pick or reword is going to be a new commit — you were never going to end up with just 1 commit on the merge request).
    – matt
    Commented Nov 30, 2023 at 23:44
  • 3
    If your branch is not linear, 47 commits do not mean that you can pick HAED~47 as the new base. The new base should be the merge-base of your branch and the target branch, which you can find by git merge-base <your-branch> <target-branch>.
    – ElpieKay
    Commented Dec 1, 2023 at 0:53
  • @TooTone does that also work if I already pushed my changes to the remote branch?
    – uylmz
    Commented Dec 1, 2023 at 14:15
  • 1
    @uylmz Yes, you'll need to do a git push --force-with-lease to have your single commit replace the 47, which you'd have done anyway with a rebase. Usual caveats about rewriting history apply, simply put only do it if you're sure you're the only person working on your branch.
    – TooTone
    Commented Dec 1, 2023 at 14:20

0

Browse other questions tagged or ask your own question.