2

I know, I can do an interactive rebase, reword first commit and fixup all other. But if a branch contains hundreds of commits it becomes very tedious.

Is there a simpler way?

Victor Dombrovsky
  • 2,955
  • 3
  • 21
  • 33

2 Answers2

5

You can use git merge --squash to squash the commits into a single one while merging into the branch.

Switch to the target branch

$ git checkout target-branch

then use

$ git merge --squash original-branch

All the commits in original-branch will be merged into a single one, and applied to the target-branch.

Graham
  • 7,431
  • 18
  • 59
  • 84
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
4

You can do that with git cherry-pick -n. That approach is more flexible compared to git merge --squash since it allows you to specify an arbitrary range of commits:

git cherry-pick -n OTHER_BRANCH~100..OTHER_BRANCH
git commit -m "Merged 100 commits from OTHER_BRANCH"

git cherry-pick

-n|--no-commit

Usually the git cherry-pick command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.

This is useful when cherry-picking more than one commits' effect to your index in a row.

Leon
  • 31,443
  • 4
  • 72
  • 97
  • Thanks for this useful option! It's really interesting. But I can't understand the last part of the last sentence: 'effect to your index in a row.' What it means? – Victor Dombrovsky Jul 28 '16 at 05:07
  • @VictorDombrovsky That sentence can be rephrased as "This is useful when merging changes from multiple commits into the index". – Leon Jul 28 '16 at 05:18