0

I am sure this question is duplicate, but I am not able to find the right answer because of too many answers being returned and each one is not working for me.

I have branch A with commits 1 -> 2 -> 3 -> 4 -> 5 -> 6 (commit 6 is the most recent).

I have branch B with commits 1 -> 2 (2 is the most recent)

I just want to move commit 6 from branch A to branch B. So 3, 4, 5, should remain in branch A.... I just take commit 6 and move it to branch B.

The end result I want is

A = 1 -> 2 -> 3 -> 4 -> 5 -> 6 B = 1 -> 2 -> 6

is this possible? and sorry if this is FAQ, duplicate or just illogical.

I googled but all answers I found pointed me to conclusion that every commit before 6 will also move... so there is no way for me to take 6 without taking 3, 4, 5

I googling I found this tutorial

https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/

but here also, once the cherry pick is done both the commits 76cada - 62ecb3 move to master. I want only 62ecb3

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
  • Possible duplicate of [Move the most recent commit(s) to a new branch with Git](http://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git) – Ken White Feb 17 '17 at 18:15
  • the problem with the solutoin you point is that with the most recent commit, all commits before them also move. I don't want the commits before the most recent commit. only the most recent commit. I just want the E. I don't want C and D. – Knows Not Much Feb 17 '17 at 18:23
  • You mentioned: "The end result I want is A = 1 -> 2 -> 3 -> 4 -> 5 -> 6 B = 1 -> 2 -> 6". So, do you want to keep commit `6` in both `A` & `B` branches? – Sajib Khan Feb 17 '17 at 18:31
  • It's not possible, as asked. The reason is that Git's commit-connecting backwards arrows (all Git's pointers work backwards, from newest back to oldest) are *part of* the commit. Your commit 6 already exists. It points to your commit 5. It is literally impossible to change this: commit 6 will forever point to commit 5. The best you can do is, as in Sukima's answer, *copy* commit 6 to a new, slightly different copy, that points somewhere else, such as to commit 2. – torek Feb 17 '17 at 19:34

1 Answers1

3
$ git checkout branch-B
$ git cherry-pick commit-6
$ git checkout branch-A
$ git reset --hard HEAD^

It looks like this git checkout branch-B :

A--B            (branch-B) (HEAD)
    \
     C--D--E--F (branch-A)

git cherry-pick F:

A--B--F'         (branch-B) (HEAD)
    \
     C--D--E--F  (branch-A)

git checkout branch-A:

A--B--F'         (branch-B)
    \
     C--D--E--F  (branch-A) (HEAD)

git reset --hard HEAD^:

A--B--F'         (branch-B)
    \
     C--D--E     (branch-A) (HEAD)
            \
             F   (detached and will be garbage collected)
Sukima
  • 9,965
  • 3
  • 46
  • 60