0

I've got a dirty working tree with changes that are destined for two different branches:

  • The student branch will get documentation with lab problems and some infrastructure.

  • The solutions branch will get working code.

My usual workflow is to go through files hunk by hunk and try to commit things that go together. But in this case, I want to send some commits to the student branch and other commits to the solutions branch. I feel like there must be a nice way to do this, but I'm not thinking of it. Here are two ideas that might work:

  • Commit everything to one branch, then use cherry picking to move the ones that don't belong. I'll have to label each commit with what goes where, and then once the commits are moved, I'll have to edit all the labels.

  • Stash everything, then separately pop the stashes each into its own branch. But then I'll have to retype every commit message.

Suggestions warmly appreciated!

2 Answers 2

1

Magit can "donate" commits, with magit-cherry-donate. A simple way to do what you want is to make all your commits on a fresh branch, then select commits and A d to donate the selection to a chosen branch. I'm not sure what you mean by "labeling" the commits, or having to re-label them, it seems to me that the commit summary should be sufficiently sensible to know which branch it belongs in. The nice thing about Magit's donate command is that it automates dropping the commit from the source branch, which cherry-picking by itself does not do as far as I'm aware.

1

Magit lets you easily switch between branches using the b command. Commit what you want on the student branch, then switch to the solutions branch using b b solutions RET and commit there (although personally I tend to use worktrees for this kind of stuff).

1
  • Can't safely change branches with a very dirty working tree :-( Commented Apr 11, 2020 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.