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?
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.pick
orreword
is going to be a new commit — you were never going to end up with just 1 commit on the merge request).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 bygit merge-base <your-branch> <target-branch>
.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.