如何在 Git 中将最后一次提交拆分为两个 [英] How to split last commit into two in Git

查看:33
本文介绍了如何在 Git 中将最后一次提交拆分为两个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个工作分支,ma​​sterforum,我刚刚在 forum 分支中做了一些修改,我想要挑选成为大师.但不幸的是,我想要挑选的提交也包含一些我不想要的修改.

I have two working branches, master and forum and I've just made some modifications in forum branch, that I'd like to cherry-pick into master. But unfortunately, the commit I want to cherry-pick also contains some modifications that I don't want.

解决方案可能是以某种方式删除错误的提交,并用两个单独的提交替换它,一个是我想在 master 中选择的更改,另一个是不属于那里的.

The solution would probably be to somehow delete the wrong commit and replace it with two separate commits, one with changes I want to pick in master, and others that doesn't belong there.

我尝试过

git reset --hard HEAD^

删除了所有更改,所以我不得不回去

which deleted all changes, so I had to go back with

git reset ORIG_HEAD

所以我的问题是,最后一次提交分成两个单独的提交的最佳方法是什么?

So my question is, what is the best way to split last commit into two separate commits?

推荐答案

你应该使用索引.混合重置后("git reset HEAD^"),添加将第一组更改放入索引,然后提交它们.然后提交休息.

You should use the index. After doing a mixed reset ("git reset HEAD^"), add the first set of changes into the index, then commit them. Then commit the rest.

您可以使用 "git add" 将文件中所做的所有更改放入指数.如果你不想暂存文件中所做的每一个修改,只有其中一些,你可以使用git add -p".

You can use "git add" to put all changes made in a file to the index. If you don't want to stage every modification made in a file, only some of them, you can use "git add -p".

让我们看一个例子.假设我有一个名为 myfile 的文件,其中包含以下文字:

Let's see an example. Let's suppose I had a file called myfile, which contains the following text:

something
something else
something again

我在上次提交时修改了它,现在它看起来像这样:

I modified it in my last commit so that now it looks like this:

1
something
something else
something again
2

现在我决定要把它分成两部分,我想插入第一行要在第一次提交中,最后一行要插入在第二次提交中.

Now I decide that I want to split it into two, and I want the insertion of the first line to be in the first commit, and the insertion of the last line to be in the second commit.

首先我回到 HEAD 的父级,但我想保留文件系统中的修改,所以我不加参数地使用git reset"(这将做一个所谓的混合"重置):

First I go back to HEAD's parent, but I want to keep the modifications in file system, so I use "git reset" without argument (which will do a so-called "mixed" reset):

$ git reset HEAD^
myfile: locally modified
$ cat myfile
1
something
something else
something again
2

现在我使用git add -p"来添加我想要提交到索引的更改(=I上演他们).git add -p"是一个交互式工具,它会询问你什么如果将文件添加到索引中,则对文件进行更改.

Now I use "git add -p" to add the changes I want to commit to the index (=I stage them). "git add -p" is an interactive tool that asks you about what changes to the file should it add to the index.

$ 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

然后我提交第一个更改:

Then I commit this first change:

$ git commit -m "Added first line"
[master cef3d4e] Added first line
 1 files changed, 1 insertions(+), 0 deletions(-)

现在我可以提交所有其他更改(即放在最后一行的数字2"):

Now I can commit all the other changes (namely the numeral "2" put in the last line):

$ git commit -am "Added last line"
[master 5e284e6] Added last line
 1 files changed, 1 insertions(+), 0 deletions(-)

让我们检查日志,看看我们有哪些提交:

Let's check the log to see what commits we have:

$ 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 中将最后一次提交拆分为两个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆