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?

2
  • 1
    git checkout -b <another-branch> && git reset --soft <first-commit-sha> && git commit?
    – raina77ow
    Commented Jul 23, 2016 at 9:32
  • If you have a text editor with line numbers, then it should not so hard to identifies the rows which need to be changed and then do a mass replace. Commented Jul 23, 2016 at 9:35

2 Answers 2

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.

1
  • You will still need to do a git commit after the squash merge. Commented Oct 20, 2022 at 6:27
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.

2
  • 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? Commented Jul 28, 2016 at 5:07
  • @VictorDombrovsky That sentence can be rephrased as "This is useful when merging changes from multiple commits into the index".
    – Leon
    Commented Jul 28, 2016 at 5:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.