1

Due to the workflow of my employer, I am in the situation where I have a parent commit with several child commits, with each child commit being the tip of a different branch:

      ┌─── b <- branch1
      ├─── c <- branch2
      ├─── d <- branch3
... ─ a ── e <- branch4

I want to end up in the following state:

      ┌─── b <- branch1
      ├─── c <- branch2
      ├─── d <- branch3
... ─ a ── e <- branch4
      └─── b'─ c'─ d'─ e' <- branch5

Where the primed commits are clones. The order of these commits is not important, and I can manually resolve commits as each as applied if necessary.

Is there a way I can do this automatically without just creating a new branch and manually cherry-picking each commit one at a time?

p0llard
  • 439
  • 6
  • 17
  • 2
    Aside from cherry-picking all necessary commits together (as in `git cherry-pick b c d e`), hard to come up with something quicker... – Romain Valeri Sep 04 '18 at 15:39
  • 1
    a [`git merge --squash` is another option](https://stackoverflow.com/questions/5308816/how-to-use-git-merge-squash). There is no way to "automatically" make this happen though. – Liam Sep 04 '18 at 15:46

1 Answers1

2

List branches that contains the parent commit (to find child commits):

git branch --contains $a | cut -c2-

(cut to remove asterisk from the current branch).

Then create a new branch and cherry-pick the child commits:

child_commits=`git branch --contains $a | cut -c2-`
git checkout -b branch5
git cherry-pick $child_commits
phd
  • 82,685
  • 13
  • 120
  • 165