-5

I am looking for a way to cherry pick a branch and merge it into another branch. The default behavior of cherry pick is to pick a particular commit, however, I am looking for picking changes that were part of the branch.

Please have a look at the below details for a better explanation.

Let suppose we have 5 branches I-e B1, B2, B3, B4, and B5. Step 1: B1 is merged into B2, and then deleted B1. Step 2: B3 is merged into B4, and then deleted B3. Step 3: B2 is merged into B4, and then deleted B2. Step 4: Now I have B4 and B5, where B4 contains all the changes done in B1, B2, and B3. Now I want to cherry Pick B2 changes (I-e Branch B2) from B4 and merge them into B5.

Do we have any way to achieve this?

Kind Regards

Can anybody help me

  • Does this answer your question? [How to git cherrypick all changes introduced in specific branch](https://stackoverflow.com/questions/35437253/how-to-git-cherrypick-all-changes-introduced-in-specific-branch) – Joe Jan 07 '22 at 13:28
  • Please may you share a visual representation of the git tree and the branch labels? Right now you're using git terminology in ways that is confusing. – evolutionxbox Jan 07 '22 at 13:28
  • Does [this image](https://imgur.com/a/QogXYfF) represent your git commit tree properly (simplified)? Ignoring master and the dotted `977ad83` commit. Consider using https://git-school.github.io/visualizing-git/#free-remote to properly visualise the commit tree and git commands – evolutionxbox Jan 07 '22 at 13:33
  • Why not create the branch at the commit before B2 was merged into B4, and then merge that into B5? – evolutionxbox Jan 07 '22 at 13:46

2 Answers2

1

cherry-pick is only for singular commits and branches point to a singular commit.

You can take all the commits within a branch and merge them into another with the following approaches.

Note: before you choose which approach, I'd suggest creating a secondary branch at the same point where your current branch that you want to merge is, so that you can throw this away and test.

Use git checkout -b new-branch current-branch. Replace current-branch with the existing branch and new-branch with newly named branch.

rebase

This is harder, but is much cleaner. It also gives you more control over which commits to apply to the new branch.

This approach rewinds to the base of your current branch and applies the commits in order, one by one.

You can also use -i to interactivly rebase your commits to select which commits you want to "apply" or merge into said branch.

git rebase -i branch-to-merge-with

merge

This is much simpler, and merges one branch with another. Bear in mind, this "smashes" commits together, so you'll need to resolve all conflicts in one go once before the merge commit is created.

git merge branch-to-merge-with
steadweb
  • 13,154
  • 3
  • 28
  • 39
1

Cherry picking means that you choose a commit from one branch and apply it onto another.

So you should take a look at the log and take the B2 commit hash you are interested from B4.

Make sure you are on the branch you want to apply the commit to.

Execute the following:

 git cherry-pick <commit-hash>

If you want multiple commits:

To cherry-pick all the commits from commit A to commit B (where A is older than B), run:

git cherry-pick A^..B

If you want to ignore A itself, run:

git cherry-pick A..B

How to cherry-pick multiple commits