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

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

问题描述

我有两个工作分支,论坛,我刚刚在论坛分支中进行了一些修改,我希望挑选出主人。但不幸的是,我想挑选的提交也包含一些我不想要的修改。



解决方案可能会以某种方式删除错误的提交和替换它有两个独立的提交,其中一个我想在master中选择,另一个不属于那个。



我试过了

  git reset --hard HEAD ^ 

删除了所有更改,因此我必须返回

  git reset ORIG_HEAD 

code>

所以我的问题是 split 最后一次提交到两个单独提交?

解决方案

您应该使用索引。在执行混合重置( git reset HEAD ^)后,添加
第一组更改到索引中,然后提交它们。然后提交
rest。



您可以使用 git add 将文件中所做的所有更改放入索引。如果你
不想在文件中进行每个修改,只有其中一些,你
可以使用git add -p。

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

  something 
其他
又一次

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

  1 
东西
别的东西
东西再次
2

现在我决定将它分成两部分,我想先插入
行在第一次提交中,并且在第二次提交中插入最后一行为



首先回到HEAD的父项,但是我想保留文件系统中的修改,
,所以我使用不带参数的git reset(它将执行所谓的混合
重置):

  $ git reset HEAD ^ 
myfile:本地修改
$ cat myfile
1
某物
某物else
又一次
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
东西
其他东西
东西再次
+2
舞台这个笨蛋[y,n,a,d,/,s,e,?]? s#将这部分分成两部分!
分成两部分。
@@ -1,3 +1,4 @@
+1
东西
其他东西
东西再次
舞台这个大块[y,n中,a,d,/,J,J,G,E,?]? y#是的,我想演示这个
@@ -1,3 +2,4 @@
某物
其他物
再次
+2
演出这个大块头[y,n,a,d,/,K,g,e,?]? n#no,我不想这样做

然后我承诺第一次更改:

  $ git commit -m新增第一行
[master cef3d4e]新增第一行
1个文件已更改,1插入(+),0删除( - )

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

  $ git commit -am添加最后一行
[master 5e284e6]添加最后一行
1个文件已更改,1个插入(+),0个删除( - )

让我们来检查日志,看看我们有什么提交:

  $ git log -p -n2 | cat 
提交5e284e652f5e05a47ad8883d9f59ed9817be59d8
作者:...
日期:...

添加最后一行

Diff --git a / myfile b / myfile
索引f9e1a67..2f113ce 100644
--- a / myfile
+++ b / myfile
@@ -2,3 +2,4 @@
东西
其他东西
东西再次
+2

提交cef3d4e0298dd5d279a911440bb72d39410e7898
作者:...
日期:.. 。

添加第一行

Diff --git a / myfile b / myfile
索引93db4cb..f9e1a67 100644
--- a / myfile
+++ b / myfile
@@ -1,3 +1,4 @@
+1
东西
其他东西
东西再次


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.

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.

I've tried doing

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?

解决方案

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.

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".

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.

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

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(-)

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天全站免登陆