我有两个分支,主人和论坛,我刚刚在论坛做了一些修改,我想樱桃进入主人。但不幸的是,我想要选择的提交也包含一些我不想要的修改。

解决方案可能是以某种方式删除错误的提交,并将其替换为两个独立的提交,一个带有我想在master中选择的更改,另一个带有剩余的更改。

我试过了

git reset --hard HEAD^

它删除了所有的更改,所以我必须回到

git reset ORIG_HEAD

因此,我的问题是,将最后一次提交拆分为两个单独的提交的最佳方法是什么?


当前回答

既然你在挑选,你可以:

选择它,添加-no-commit选项。 重置并使用add -patch, add -edit或只是添加到舞台上您想保留的内容。 提交分阶段的更改。 如果要重用原始提交消息,可以在commit命令中添加——reuse-message=<old-commit-ref>或——reedit-message=<old-commit-ref>选项。 用reset消除未分阶段的变化——很难。

另一种方法,保存或编辑原始的提交消息:

像往常一样选择原始提交。 反转您不想要的更改,并使用add来进行反转。 如果您要删除添加的内容,这一步会很简单,但如果您要添加已删除的内容或反转更改,这一步就有点棘手了。 Commit——modify对选定的提交执行反转。 您将再次收到相同的提交消息,您可以根据需要保留或修改该消息。

其他回答

双重反向挤压方法

Make another commit that removes the unwanted changes. (If it's per file, this is really easy: git checkout HEAD~1 -- files with unwanted changes and git commit. If not, files with mixed changes can be partially staged git reset file and git add -p file as an intermediate step.) Call this the revert. git revert HEAD – Make yet another commit, that adds back the unwanted changes. This is the double-revert Of the 2 commits you now made, squash the first onto the commit to split (git rebase -i HEAD~3). This commit now becomes free of the unwanted changes, for those are in the second commit.

好处

保存提交消息 即使提交的分裂不是最后一个也有效。它只要求不需要的更改与以后的提交不冲突

要将当前提交更改为两次提交,可以执行以下操作。

:

git reset --soft HEAD^

这将取消上一次提交,但将所有内容都保留在舞台上。然后你可以取消某些文件:

git reset -- file.file

可选地重新部署这些文件的部分:

git add -p file.file

做一个新的第一次提交:

git commit

阶段,并在第二次commit中提交其余的更改:

git commit -a

Or:

撤销并取消上次提交的所有更改:

git reset HEAD^

有选择地进行第一轮更改:

git add -p

提交:

git commit

提交其余的更改:

git commit -a

(在任何一个步骤中,如果你撤销了一个添加了全新文件的提交,并想要将其添加到第二次提交中,你必须手动将其添加为commit -a只会对已经跟踪的文件进行更改。)

运行git gui,选择“修改最后提交”单选按钮,并取消你不想第一次提交的更改(commit > unstage From commit,或Ctrl-U)。我认为这是最简单的方法。

你可以做的另一件事是在没有提交的情况下选择更改(git cherry-pick -n),然后在提交之前手动或使用git gui选择所需的更改。

你可以使用git rebase -i <commit>,其中<commit>是你想保持原样的最新提交。在您想要插入新的分离提交的每个点上添加一个断点。然后在每次休息时,使用git checkout -p <commit,其中包含您想要>提取的部分,并提交它们。然后git rebase——继续从以后的提交中删除这些部分。

对于拆分最近一次提交的简单情况,如下所示:

$ git rebase -i HEAD^
# add 'break' at the start

$ git checkout -p master # or whatever your branch is called
# choose the parts you want to split out

$ git commit
# commit the newly-split-out parts

$ git rebase --continue
# rebase the remaining parts of the change onto the split-out parts

这假设您希望稍后的提交保留原始的提交消息;这就是我通常发现自己所处的情况(排除一些预备变化)。

git reset HEAD^

困难是扼杀你改变的原因。