1

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

user3414980
  • 29
  • 1
  • 9
  • This sounds like something that could potentially be done with an interactive rebase? – OliverRadini Jul 20 '18 at 07:53
  • If you want all the commits, you're better off merging B into A. Or am I missing something? – Matt Jul 20 '18 at 07:55
  • I only want commits committed by me, not other users. And I can't merge or rebase those branches as I'm not their owner. – user3414980 Jul 20 '18 at 07:57
  • 1
    Ok, then doing an interactive rebase as Oliver suggested might do the work. First, there is no "owner" of a branch and second, as long as you don't push your local branch to remote repo, it will not impact anyone else. Just do a `git rebase -i A` and keep the commits from you (delete or comment the other lines). Then you can merge B into A. Without pushing B – Matt Jul 20 '18 at 08:05

2 Answers2

3

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
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
0

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(' ') }
user3414980
  • 29
  • 1
  • 9