Home > database >  How to squash several commits to another branch?
How to squash several commits to another branch?

Time:09-21

I will develop my app in branch dev and provide the "best so far" (a release on GitHub) on a branch public.

The branch dev will have commits c964dea, 06cf8ee, 396b4a3 and 9be7fdb. I would like to squash them into a single commit on branch public (leaving them as-it on the branch dev).

Is this possible?

I saw many questions and answers about squashing commits, but none of tehem seemed to address the problem of "squashing to another branch".

CodePudding user response:

Create and switch to a new branch from dev.

git switch -c <new-branch>

It'll be identical to dev at this point but now you can squash the commits without changing dev. Start an interactive rebase.

git rebase -i <first-commit>

Then set all the commits to squash.

CodePudding user response:

you probably want first to cherry pick individual commits, if they are not in sequence by running :

git cherry-pick <commitSHA>

You will have a sequence of cherry-picked commits on your feature branch. after that you want to do interactive rebase on your branch from all the commits you cherry-picked.

git rebase -i HEAD~<n 1>

where n is going to be the number of cherry-picked commits. mark all the commits with s except the last one. s, squash = use commit, but meld into previous commit

if all your commits that you want to cherry-pick are in sequence, then it is easier :

git cherry-pick A^..B

where A and B are first and last commits to be cherry-picked. After that rebase them.

  • Related