我有两个分支,主人和论坛,我刚刚在论坛做了一些修改,我想樱桃进入主人。但不幸的是,我想要选择的提交也包含一些我不想要的修改。
解决方案可能是以某种方式删除错误的提交,并将其替换为两个独立的提交,一个带有我想在master中选择的更改,另一个带有剩余的更改。
我试过了
git reset --hard HEAD^
它删除了所有的更改,所以我必须回到
git reset ORIG_HEAD
因此,我的问题是,将最后一次提交拆分为两个单独的提交的最佳方法是什么?
我有两个分支,主人和论坛,我刚刚在论坛做了一些修改,我想樱桃进入主人。但不幸的是,我想要选择的提交也包含一些我不想要的修改。
解决方案可能是以某种方式删除错误的提交,并将其替换为两个独立的提交,一个带有我想在master中选择的更改,另一个带有剩余的更改。
我试过了
git reset --hard HEAD^
它删除了所有的更改,所以我必须回到
git reset ORIG_HEAD
因此,我的问题是,将最后一次提交拆分为两个单独的提交的最佳方法是什么?
当前回答
你可以使用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 --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只会对已经跟踪的文件进行更改。)
双重反向挤压方法
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 gui,选择“修改最后提交”单选按钮,并取消你不想第一次提交的更改(commit > unstage From commit,或Ctrl-U)。我认为这是最简单的方法。
你可以做的另一件事是在没有提交的情况下选择更改(git cherry-pick -n),然后在提交之前手动或使用git gui选择所需的更改。
这可能是另一种解决方案,适用于需要提交大量文件而需要将少量文件移动到新提交的情况。如果要从HEAD的最后一次提交中提取一组<path>文件,并将它们全部移动到新的提交中,那么这将有效。如果需要多次提交,则可以使用其他解决方案。
首先将补丁放入分段和非分段区域,这些区域将分别包含将代码恢复到修改前和修改后的更改:
git reset HEAD^ <path>
$ git status
On branch <your-branch>
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <path>
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <path>
为了理解将会发生什么(箭头和注释不是命令的一部分):
git diff --cached -> show staged changes to revert <path> to before HEAD
git diff -> show unstaged changes to add current <path> changes
回复<path>在上次提交时的变化:
git commit --amend -> reverts changes on HEAD by amending with staged changes
使用<path> changes创建新的提交:
git commit -a -m "New Commit" -> adds new commit with unstaged changes
这样做的效果是创建一个新的提交,其中包含从上次提交中提取的更改。
git reset HEAD^
困难是扼杀你改变的原因。