GIT cherry pick all my commits that exists in one

2019-08-30 09:37发布

问题:

I have 2 branches A and B. Branch B was branched from branch A at some time and multiple users are committing to both branches. I want to cherry pick all my commits (that doesn't already exist in branch A) from branch B to branch A without the need to manually search for every single one. Is it somehow possible?

Thanks

回答1:

A more straight foreward way would be to use rebase --onto:

git rebase --onto target-branch [LAST_COMMON_COMMIT_BEFORE_BRANCH] branch-to-move

if your repo looks like this:

a - B - c - d - e -> master
      \      \
       \      f - g -> some-feature
        \
         h - i - j -> branch-to-move

the command

git rebase --onto some-feature B branch-to-move

would result in

a - B - c - d - e -> master
             \
              f - g -> some-feature
                   \
                    h' - i' - j' -> branch-to-move


回答2:

Figured it out and made this PowerShell script:

$Source = Read-Host -Prompt 'Enter Source branch: '
$Target = Read-Host -Prompt 'Enter Target branch: '
cd C:\project
git log $Source --not $Target --cherry --author=my@ema.il --author-date-order --reverse | 
Select-String -Pattern "commit" |
ForEach-Object { git cherry-pick -x $_.ToString().split("+")[-1].Trim(' ') }