英文:
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
git checkout -b refactor
(and perhaps stash or WIP the uncommitted changes)git rebase -i main
and delete all the feature related commitsgit switch feature
Option 2 with cherry-picking
git switch main
(perhaps stash or WIP the uncommitted changes before)git checkout -b refactor
git cherry-pick <D hash>
git switch feature
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
git checkout -b refactor
(and perhaps stash or WIP the uncommitted changes)git rebase -i main
and delete all the feature related commitsgit switch feature
Option 2 with cherry-picking
git switch main
(perhaps stash or WIP the uncommitted changes before)git checkout -b refactor
git cherry-pick <D hash>
git switch feature
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论