Search code examples
gitgithubbranchcommit

How to move one specific commit to another branch?


so lets say I have 2 branches : Master , dev . And commits look like this:

Master : A-B-C

dev: A-B-C-D-E-F

I want to move just the E commit from dev branch to Master like this :

Master : A-B-C-E

dev: A-B-C-D-E-F

I know this can cause problems if there are some changes in commit D that are required for commit E, but in my case I am certain that commit E doesnt have anything to do with commit D.

And after some time when D and F commits are also ready to be moved to Master branch I want to merge them without any problems like this :

Master : A-B-C-D-E-F

dev: A-B-C-D-E-F

I can understand if there is no easy way to do this because this wouldnt make sense in most of the cases out there . Open to any solutions that can help my specific case where I know for sure that commit E doesnt have anything to do with commit D .


Solution

  • You can run cherry-pick on master:

    git checkout master
    git cherry-pick E
    

    Which will create a copy of commit E on master (as commit E' if you will). New history:

    Master : A-B-C-E'
    dev: A-B-C-D-E-F
    

    But note that it will not be easily possible to have master as A-B-C-D-E-F. Having master as A-B-C-E'-D'-F' is easy however. Simply cherry-pick D and F when you need them. Or merge branch dev fully. Git should be smart enough to detect that master already contains a copy of E.