0

this is my git log history, I want to squash my last two commits into "merge release into feature" commit (which is a merge commit)

I tried with git rebase -i HEAD~3 and keep getting this error "cannot squash without a previous commit error"

commit 6a4fc (HEAD -> tasl-sq1)
Author: john
Date:   date

   show logo on landing page if they have one

commit dfa4fc
Author: James 
Date:   date

   update ui

commit 398ec03
Merge: efd12143ab c57bf64b81
Author: Tom
Date:   date

    merge release into feature 

commit dddfc 
Author: KAle
Date:   date

    API update
Sam
  • 6,215
  • 9
  • 71
  • 90
  • [j6t's answer](https://stackoverflow.com/a/68347928/1256452) is *how to achieve this*, but I will add that the result is generally what people call an [evil merge](https://stackoverflow.com/q/1461909/1256452), which (correctly) suggests that you should be cautious about using this. – torek Jul 13 '21 at 05:20

2 Answers2

3

In this situation it is a very simple matter of

git reset --soft 398ec03
git commit --amend

i.e., you go back to the merge commit without wiping the staged files nor the working tree, and then you amend the merge commit.

j6t
  • 9,150
  • 1
  • 15
  • 35
0

I know of a hacky way to do this :

  • reset (with reset --soft) to the first parent of the merge commit
  • create a file .git/MERGE_HEAD with the hash of the other commit to merge
  • run git commit
# move back to "before the merge", while keeping all the current content as staged
git reset --soft efd12143ab

# indicate to git 'I want to merge c57bf64b81'
echo c57bf64b81 >> .git/MERGE_HEAD

# commit
git commit
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • @j6t's answer is simpler than this one. I will leave my answer as I think the `MERGE_HEAD` indication may help in other situations. – LeGEC Jul 12 '21 at 13:58