0

I try to run

git rebase -i HEAD~N

to squash my older commits into one. But sometimes I got a Merge conflict. The problem is, even if I resolve the conflict and do a "git rebase --continue", I lost a lot of my other commit changes.

I also tried to solve the issue based on these answers: Git squash all commits in branch without conflicting

Paxi
  • 111
  • 11
  • 2 questions : 1. did you by any chance change the order in which the commits are applied, or delete any commit ? 2. is there a merge commit in the last `N` commits you are trying to squash together ? – LeGEC Nov 16 '20 at 11:49
  • 1. no, what I did is to make some pushes where I successfully squashed the first 35 commit (and I still have 11) 2. Yes. – Paxi Nov 16 '20 at 12:07

3 Answers3

2

You can avoid using rebase for squashing by using reset --soft. Say you want yo squash from HEAD~100 up to your current branch (HEAD~100 being the last surviving commit that won't be squashed):

git reset --soft HEAD~100
git commit -m "Squashed stuff"

And that's it. No conflicts, no hassles.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
1

If you need to rebase a range of commits which includes a merge commit, add the -m option :

git rebase -i -m HEAD~N

The sequencer script will allow you to describe how you include that merge in the rewriting.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Thanks for the answer. I have tried it, but unfortunately it behaved in the same way as I tried it at the beginning. – Paxi Nov 17 '20 at 08:58
0

Soft reset is worked, but I found a better solution. The problem was that there was already a merge commit within the commits which I wanted to squash. I used the following code to get the parent commit:

git show --pretty=%P COMMIT_HASH

Finally, I had to use the parent commit's hash to do the rebase.

Paxi
  • 111
  • 11