简体   繁体   English

在git中,如何从分支中删除提交但是将该提交保留在单独的分支中?

[英]In git, how do I remove a commit from a branch but keep that commit in a separate branch?

I have a DEV branch with a history looking like this (with each list item being a separate commit): 我有一个DEV分支,其历史记录看起来像这样(每个列表项都是一个单独的提交):

  • feature 5 特色5
  • feature 4 特色4
  • feature 3 特色3
  • feature 2 特色2
  • feature 1 特色1

Suppose I want to "delete" feature 3 from this branch, but I don't want to lose the code in this feature so I want to put it in it's own separate branch. 假设我想从这个分支“删除” 功能3 ,但我不想丢失此功能中的代码,所以我想把它放在它自己的独立分支中。

Basically, from the dev branch above, I want to end up with two branches looking like this: 基本上,从上面的开发分支,我想最终有两个分支看起来像这样:

DEV BRANCH : DEV分支

  • feature 5 特色5
  • feature 4 特色4
  • feature 2 特色2
  • feature 1 特色1

NEW BRANCH : 新分支

  • feature 3 特色3
  • feature 2 特色2
  • feature 1 特色1

Any ideas how I can achieve this in git? 我有什么想法可以在git中实现这一点吗?

Create a new branch from feature3 . feature3创建一个新分支。 Then reset feature3 from the dev branch. 然后从dev分支reset feature3 However, if you have pushed these commits you will need to use revert 但是,如果您已经推送了这些提交,则需要使用revert

First create newbranch from feature3 首先创建newbranchfeature3

git checkout dev
git branch newbranch feature3
git reset --hard feature3

Now if you haven't pushed to remote 现在,如果你没有推到远程

git reset --hard feature3

Or if you have pushed 或者如果你推了

git revert feature3

Also, if you want to add specific commits to newbranch , like feature7 you can use 此外,如果您要向newbranch添加特定提交,例如feature7您可以使用

git checkout newbranch
git cherry-pick feature7
#create NEW
git branch NEW commit3

#rewrite DEV
git rebase -i commit2 DEV

#in the editor remove the line "pick commit3", save and exit.

To create a new branch till feature3 : 要创建一个新的分支,直到feature3

git checkout <feature3_commit_id>
git checkout -b new_branch

To remove feature3 from dev branch 从dev分支中删除feature3

git checkout dev_branch
git rebase --onto <feature2_commit_id> <feature3_commit_id> <feature5_commit_id>

Just FYI, you can use git log --oneline to find commit ids. 仅供参考,您可以使用git log --oneline查找提交ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM