How do I move a commit between branches in Git?
You can use git cherry-pick
to grab C, and put it on Y. Assuming Y exists as the tip a branch called branch-Y
:
$ git checkout branch-Y
$ git cherry-pick C
So now C is on top of Y. But D and E also still contain C (cherry picking doesn't move a commit, it just makes a copy of it). You'll have to rebase D and E on top of B. Assuming E is the tip of branch-E
and B is branch-B
, you can:
$ git checkout branch-E
$ git rebase --interactive branch-B
This will open up an interactive rebase session. Just remove commit C entirely, and leave D and E intact. You'll then have D and E rebased on top of B without C.
Related videos on Youtube
Chris Perkins
Updated on August 19, 2020Comments
-
Chris Perkins almost 4 years
I'm sure this is a simple thing that has been asked and answered, but I don't know what terms to search for. I have this:
/--master--X--Y A--B \--C--D--E
Where I commited C, D, and E (locally only) on a branch, but then I realized that D and E are really independent of C. I want to move C to its own branch, and keep D and E for later. That is, I want this:
/--C /--master--X--Y A--B \--D--E
How do I yank C out from under D and E?
-
Cascabel almost 14 yearsNote that
cherry-pick
, as of v1.7.1, can cherry-pick multiple commits, so you can use this to "yank" more than one commit out. You can also do that by creating a temporary branch at E and interactively rebasing it onto Y, keeping only the commits you want, then merging it (it'll be a fast-forward) into Y's branch. (That's always worked, and might be easier anyway, so you don't have to paste as many SHA1s.)