1

Working on feature branch and doing some non-feature-related work in commit D. Now I want to move (not just cherrypick) that commit to another refactor branch.

    C - D (HEAD w. uncommitted changes) [feature branch]
   /
A-B [main branch]
   \
    D [refactor branch]

Currently I know of two ways of doing this:

Option 1 with interactive rebasing

  1. git checkout -b refactor (and perhaps stash or WIP the uncommitted changes)
  2. git rebase -i main and delete all the feature related commits
  3. git switch feature

Option 2 with cherry-picking

  1. git switch main (perhaps stash or WIP the uncommitted changes before)
  2. git checkout -b refactor
  3. git cherry-pick <D hash>
  4. git switch feature
  5. git rebase refactor

Are there any other good ways for doing it?

It would be handy if there was a to commit to another branch than the current checkout (HEAD).

Or if there would a way to have the cherry-pick also remove the commit from the feature branch.


(I'm wondering if git-filter-repo could be used to automate step 2 (git rebase -i main and delete all the feature related commits) in option 1 by saying that it should remove all similar commits that can be found on the refactor branch.)

Norfeldt
  • 8,272
  • 23
  • 96
  • 152
  • Does this answer your question? [How do I delete a commit from a branch?](https://stackoverflow.com/questions/1338728/how-do-i-delete-a-commit-from-a-branch) – Paolo Mar 03 '23 at 13:15
  • Wouldn't that reset the all commits down to the commit I want to remove - instead of specifically removing only that single commit. Remember I might have a WIP commit that I don't wanna lose. – Norfeldt Mar 03 '23 at 13:59

1 Answers1

1

Your second way supposes your feature branch would need/require refactor, since you are rebasing feature on top of refactor.

If that is not the case, using the cheery-pick option, you can remove the commit from the feature branch by git reset --hard C (after a git stash, in case you have other work in progress not yet committed)

But it is true a cherry-pick does not remove a commit from its source branch. It only duplicates it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250