I have a commit that includes things that should be considered in another branch. So what I would like to do is cut out some portions of the commit and 'move' them into another branch. I looked at git cherry-pick and git revert but these commands seem to deal with the whole commit. Is it possible to pull out portions of a commit and move them to another branch as staged changes?
CodePudding user response:
git cherry-pick -n
will do the cherry pick but not commit it. Then you can alter the change as normal before committing.
Once that's done, remove the commit from the other branch as normal using git reset --hard
or git rebase -i
depending on whether it's the latest commit or if it's buried.
CodePudding user response:
Assuming it's a single commit that you want to split up, this is what you can do:
git checkout -b temp the-feature # start a new branch from the feature you want to break up
git reset --soft HEAD~
# this will set the branch pointer on the parent revision
# all changes from the feature will be in the index
# take out all the changes that you would like to separate from the-feature
# when you are ready, add all files, commit
git add .
git commit -m "this is what I want to save apart from the-feature"
# here is where the magic happens:
git checkout the-feature # checkout the original feature
git reset --soft temp # make it point to the revision you created before
# now, all you should have in the index is all changes that are only relevant to the-feature
git commit -m "Changes for the-feature"
# and you can separate the branches:
git rebase --onto HEAD~2 the-feature~ the-feature
# now the two branches are separate