1

I'm having a strange issue when trying to squash some remote commits. I made a series of small changes to a GitLab .yml file (22 to be exact) that I would like to squash into one commit.

What I have tried to do so far is to use the command 'git reset --soft HEAD~22', then committing the squashed commit as one commit, and then force pushing to squash the commit remotely (as is done in this answer). I know using a separate branch for this in the first place would have been wiser and much less painful by using the automatic squash in GitLab's merge requests, but I am relatively new to Git and have certainly learned my lesson.

What is happening is this. When I try to do all 22, it squashes the last 44, when I do 2 it squashes the last 24. I've tried a few other numbers and it seems random (11 nets me 33 squashed commits, 4 gives me 25, etc.). What is going on here? I haven't caused any damage to my remote repo because I have not pushed anything and I have made numerous local backups, but I am totally perplexed.

PaulD
  • 73
  • 1
  • 5
  • 1
    I think that the reason is because one of the 2 commits you reset is a merge commit corresponding to a sync with the 'origin' remote (i.e. you did a 'git pull'). That means that with this commit in your branch you are up to date with 'origin'. If you reset, you are no more up-to-date because the history of your remote branch is no more included in your local branch. – Philippe Aug 20 '21 at 15:21

1 Answers1

2

I figured it out thanks to Philippe's comment. One of the commits was a merge that consisted of 23 separate commits (the .yml changes). There was one commit after this merge commit, so doing 'git reset --soft HEAD~1' would just squash that one recent commit, but doing 'git reset --soft HEAD~2' would grab that most recent commit on the branch, plus the 23 commits that comprised the merge commit.

So, in summary, check the graph of your commits on whatever Git platform you are using (or use some kind of visualizer). It will help to lay out the commit history in a more human-readable way, and it will help explain any strange behavior you are seeing.

PaulD
  • 73
  • 1
  • 5