将与非特性相关的提交移到另一个分支

huangapple go评论60阅读模式
英文:

Git move non-feature-related commits to another branch

问题

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.)

英文:

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.)

答案1

得分: 1

第二种方法假设你的特性分支需要重构,因为你正在将 feature 分支重新基于 refactor 分支之上。

如果不是这种情况,你可以使用 cherry-pick 选项,通过 git reset --hard C 来从特性分支中移除该提交(在进行 git stash 之后,以防你有其他尚未提交的工作正在进行中)。

但事实上,cherry-pick 不会从源分支中删除提交,它只会复制它。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月3日 20:04:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定