Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git skips merge commit when squashing using interactive rebase

I am working on a feature using git and I have the following steps completed so far.

  • Checkout new feature branch called feature1 based on master
  • Add couple of commits.
  • Checkout new branch test based on feature1.
  • Perform some changes in test.
  • Return to (checkout to) feature1 branch.
  • Merge the test branch to feature1
  • Delete test branch
  • Edit a file, and amend the last commit to reflect the change

Here is git log --oneline -n 5 output

de5d701 Merge branch 'test' into 'feature1' (amended)
a668f93 'feature1' commit3
ded7c00 'feature1' commit2
8731807 Initial 'feature1' commit
ff2f539 latest 'master' commit

I want to squash everything which comes after ff2f539 latest 'master' commit before merging to master. I tried to perform

git rebase --interactive  ff2f539

but the latest commit is not listed in the editor:

pick 8731807 Initial 'feature1' commit
pick ded7c00 'feature1' commit2
pick a668f93 'feature1' commit3

# Rebase ff2f539..de5d701 onto ff2f539
#
# Commands:
...
# Note that empty commits are commented out

I can not understand why the latest commit which is produced by the merging of test branch to feature1 branch is skipped. Saving and exiting the editor with the following content:

pick 8731807 Initial 'feature1' commit
squash ded7c00 'feature1' commit2
squash a668f93 'feature1' commit3

# Rebase ff2f539..de5d701 onto ff2f539
#
# Commands:
...
# Note that empty commits are commented out

produces new commit which does not provide the changes from the last commit

de5d701 Merge branch 'test' into feature1(amended)

They are lost. Any help how to squash all those commits into one single commit before merging to master ?

like image 959
egelev Avatar asked Oct 20 '25 14:10

egelev


1 Answers

Git does not show merge commits in the "interactive" list by design. It instead includes all commits that were brought into the branch by that merge. You can use -p (--preserve-merges) option to change that behavior, but it is not recommended in interactive mode, because the result may be not that obvious (see http://git-scm.com/docs/git-rebase#_bugs).

But there is a simple solution without rebasing:

# make sure your working copy is clean, do git stash if needed
git checkout feature1
git reset --soft ff2f539
git commit -m "..."

This will produce one commit including all changes after ff2f539.

like image 55
Roman Avatar answered Oct 22 '25 04:10

Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!