victordobry
victordobry

Reputation: 3408

Is it possible to easily move all commits from one branch onto another as one commit?

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?

Upvotes: 2

Views: 1940

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176532

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.

Upvotes: 5

Leon
Leon

Reputation: 32504

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.

Upvotes: 4

Related Questions