Home > database >  how can I git cherry-pick a commit from branch A to branch B but I am in branch master?
how can I git cherry-pick a commit from branch A to branch B but I am in branch master?

Time:04-15

I need cherry-pick a commit from DEV to master, INT(for internal test environment), PROD branches.

If I need switch to corresponding branch every time cherry-pick, then I need checkout INT, checkout PROD.

Can I just be in branch Master and cherry-pick a commit from DEV to INT, to PROD but without checkout to any other branch?

Thanks a lot.

CodePudding user response:

Not in general, no. Cherry-picking is implemented as a kind of merge internally, and merging—like most processes in Git—takes place on the current branch, so the branch must be checked out in a working tree, with an index in which the merge can happen.

You can, however, add a new working tree to any existing Git repository using git worktree add. This new working tree must be on some other branch: not the branch checked out in the main working tree, and not any branch checked out in any other working tree.1 But that is exactly what you want here. So just add one working tree that's on branch INT—this becomes your second working tree—and a third one that's on branch PROD, and do the two cherry-pick operations in the two added working trees.


1A working tree can also be in "detached HEAD" mode, but this doesn't help for your case.

  •  Tags:  
  • git
  • Related