我有两个分支,主人和论坛,我刚刚在论坛做了一些修改,我想樱桃进入主人。但不幸的是,我想要选择的提交也包含一些我不想要的修改。
解决方案可能是以某种方式删除错误的提交,并将其替换为两个独立的提交,一个带有我想在master中选择的更改,另一个带有剩余的更改。
我试过了
git reset --hard HEAD^
它删除了所有的更改,所以我必须回到
git reset ORIG_HEAD
因此,我的问题是,将最后一次提交拆分为两个单独的提交的最佳方法是什么?
我有两个分支,主人和论坛,我刚刚在论坛做了一些修改,我想樱桃进入主人。但不幸的是,我想要选择的提交也包含一些我不想要的修改。
解决方案可能是以某种方式删除错误的提交,并将其替换为两个独立的提交,一个带有我想在master中选择的更改,另一个带有剩余的更改。
我试过了
git reset --hard HEAD^
它删除了所有的更改,所以我必须回到
git reset ORIG_HEAD
因此,我的问题是,将最后一次提交拆分为两个单独的提交的最佳方法是什么?
当前回答
你应该使用索引。混合重置后(“git reset HEAD^”),添加 将第一组更改放入索引,然后提交它们。然后提交 休息。
您可以使用“git add”将文件中所做的所有更改放到索引中。如果你 不希望在文件中执行所有修改,只需要其中的一部分即可 可以使用“git add -p”。
让我们看一个例子。假设我有一个名为myfile的文件,其中包含 以下文本:
something
something else
something again
我在上次提交时修改了它,现在它看起来是这样的:
1
something
something else
something again
2
现在我决定把它一分为二,我想插入 第一行是第一次提交,最后一行的插入是 在第二次提交中。
首先我回到HEAD的父目录,但是我想把修改保存在文件系统中, 所以我使用“git reset”不带参数(这将做所谓的“混合” 重置):
$ git reset HEAD^
myfile: locally modified
$ cat myfile
1
something
something else
something again
2
现在我使用“git add -p”来添加我想要提交到索引的更改(=I 阶段)。"git add -p"是一个交互式工具,它会询问你什么 对文件的更改应该添加到索引中。
$ git add -p myfile
diff --git a/myfile b/myfile
index 93db4cb..2f113ce 100644
--- a/myfile
+++ b/myfile
@@ -1,3 +1,5 @@
+1
something
something else
something again
+2
Stage this hunk [y,n,a,d,/,s,e,?]? s # split this section into two!
Split into 2 hunks.
@@ -1,3 +1,4 @@
+1
something
something else
something again
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? y # yes, I want to stage this
@@ -1,3 +2,4 @@
something
something else
something again
+2
Stage this hunk [y,n,a,d,/,K,g,e,?]? n # no, I don't want to stage this
然后我提交第一个更改:
$ git commit -m "Added first line"
[master cef3d4e] Added first line
1 files changed, 1 insertions(+), 0 deletions(-)
现在我可以提交所有其他更改(即数字“2”放在最后一行):
$ git commit -am "Added last line"
[master 5e284e6] Added last line
1 files changed, 1 insertions(+), 0 deletions(-)
让我们检查一下日志,看看我们有哪些提交:
$ git log -p -n2 | cat
Commit 5e284e652f5e05a47ad8883d9f59ed9817be59d8
Author: ...
Date: ...
Added last line
Diff --git a/myfile b/myfile
Index f9e1a67..2f113ce 100644
--- a/myfile
+++ b/myfile
@@ -2,3 +2,4 @@
something
something else
something again
+2
Commit cef3d4e0298dd5d279a911440bb72d39410e7898
Author: ...
Date: ...
Added first line
Diff --git a/myfile b/myfile
Index 93db4cb..f9e1a67 100644
--- a/myfile
+++ b/myfile
@@ -1,3 +1,4 @@
+1
something
something else
something again
其他回答
我很惊讶没有人建议git选择-n论坛。这将显示最新论坛提交的更改,但不提交它们-然后你可以重置你不需要的更改,并提交你想要保留的更改。
双重反向挤压方法
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 reset HEAD^”),添加 将第一组更改放入索引,然后提交它们。然后提交 休息。
您可以使用“git add”将文件中所做的所有更改放到索引中。如果你 不希望在文件中执行所有修改,只需要其中的一部分即可 可以使用“git add -p”。
让我们看一个例子。假设我有一个名为myfile的文件,其中包含 以下文本:
something
something else
something again
我在上次提交时修改了它,现在它看起来是这样的:
1
something
something else
something again
2
现在我决定把它一分为二,我想插入 第一行是第一次提交,最后一行的插入是 在第二次提交中。
首先我回到HEAD的父目录,但是我想把修改保存在文件系统中, 所以我使用“git reset”不带参数(这将做所谓的“混合” 重置):
$ git reset HEAD^
myfile: locally modified
$ cat myfile
1
something
something else
something again
2
现在我使用“git add -p”来添加我想要提交到索引的更改(=I 阶段)。"git add -p"是一个交互式工具,它会询问你什么 对文件的更改应该添加到索引中。
$ git add -p myfile
diff --git a/myfile b/myfile
index 93db4cb..2f113ce 100644
--- a/myfile
+++ b/myfile
@@ -1,3 +1,5 @@
+1
something
something else
something again
+2
Stage this hunk [y,n,a,d,/,s,e,?]? s # split this section into two!
Split into 2 hunks.
@@ -1,3 +1,4 @@
+1
something
something else
something again
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? y # yes, I want to stage this
@@ -1,3 +2,4 @@
something
something else
something again
+2
Stage this hunk [y,n,a,d,/,K,g,e,?]? n # no, I don't want to stage this
然后我提交第一个更改:
$ git commit -m "Added first line"
[master cef3d4e] Added first line
1 files changed, 1 insertions(+), 0 deletions(-)
现在我可以提交所有其他更改(即数字“2”放在最后一行):
$ git commit -am "Added last line"
[master 5e284e6] Added last line
1 files changed, 1 insertions(+), 0 deletions(-)
让我们检查一下日志,看看我们有哪些提交:
$ git log -p -n2 | cat
Commit 5e284e652f5e05a47ad8883d9f59ed9817be59d8
Author: ...
Date: ...
Added last line
Diff --git a/myfile b/myfile
Index f9e1a67..2f113ce 100644
--- a/myfile
+++ b/myfile
@@ -2,3 +2,4 @@
something
something else
something again
+2
Commit cef3d4e0298dd5d279a911440bb72d39410e7898
Author: ...
Date: ...
Added first line
Diff --git a/myfile b/myfile
Index 93db4cb..f9e1a67 100644
--- a/myfile
+++ b/myfile
@@ -1,3 +1,4 @@
+1
something
something else
something again
你可以使用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
这假设您希望稍后的提交保留原始的提交消息;这就是我通常发现自己所处的情况(排除一些预备变化)。