如果不创建一个分支并在一个新分支上做一堆奇怪的工作,那么在将一个提交提交到本地存储库之后,是否有可能将其分解为几个不同的提交?


当前回答

如果您的更改主要是添加新内容,则此方法非常有用。

有时您不希望丢失与正在拆分的提交相关联的提交消息。如果您提交了一些要拆分的更改,可以:

编辑要从文件中删除的更改(即删除行或适当更改文件以适合首次提交)。您可以使用所选编辑器和gitcheckout-p HEAD^--path/to/file的组合将一些更改还原到当前树中。将此编辑作为新提交提交,使用类似gitadd的方式;gitcommit-m“删除以后应该更改的内容”,因此您将在历史中进行原始提交,并且还将进行另一次提交,其中包含您所做的更改,因此当前HEAD上的文件看起来就像您希望在拆分后的第一次提交中一样。

000aaa Original commit
000bbb removal of things that should be changed later

使用git-restore HEAD还原编辑,这将创建还原提交。文件将看起来像原始提交时一样,您的历史记录现在看起来像

000aaa Original commit
000bbb removal of things that should be changed later
000ccc Revert "removal of things that should be changed later" (assuming you didn't edit commit message immediately)

现在,您可以使用gitrebase-i将前两个提交压缩/修复为一个,如果您之前没有向其提供有意义的提交消息,则可以选择修改还原提交。你应该留下来

000ddd Original commit, but without some content that is changed later
000eee Things that should be changed later

其他回答

使用git-rebase--interactive编辑先前的提交,运行git-resetHEAD~,然后git-add-p添加一些,然后进行提交,然后再添加一些,再进行另一次提交,次数尽可能多。完成后,运行git rebase--continue,您将在堆栈中更早地获得所有拆分提交。

重要提示:请注意,您可以四处游玩并进行所有所需的更改,而不必担心丢失旧的更改,因为您可以始终运行git reflog来找到项目中包含所需更改的点(我们称之为a8c4ab),然后git重置a8c4aa。

下面是一系列命令来说明它的工作原理:

mkdir git测试;cd git测试;初始化

现在添加文件a

vi A

添加此行:

one

gitcommit-我是一个

然后将此行添加到A:

two

gitcommit-am二

然后将此行添加到A:

gitcommit-am三

现在文件A看起来像这样:

one
two
three

我们的git日志如下所示(好吧,我使用的是git log--prett=oneline--prett=“%h%cn%cr----%s”

bfb8e46 Rose Perrone 4 seconds ago ---- three
2b613bc Rose Perrone 14 seconds ago ---- two
9aac58f Rose Perrone 24 seconds ago ---- one

假设我们想将第二次提交分成两份。

git-rebase--交互式HEAD~2

这将显示如下消息:

pick 2b613bc two
pick bfb8e46 three

将第一个pick更改为e以编辑提交。

git重置HEAD~

gitdiff告诉我们,我们刚刚取消了第二次提交的提交:

diff --git a/A b/A
index 5626abf..814f4a4 100644
--- a/A
+++ b/A
@@ -1 +1,2 @@
 one
+two

让我们进行更改,并在文件a中的行中添加“和第三个”。

git添加。

这通常是在交互式rebase期间,我们将运行gitrebase--continue,因为我们通常只想回到提交堆栈中编辑先前的提交。但这一次,我们希望创建一个新的提交。所以我们将运行gitcommit-am“二加三”。现在我们编辑文件A,并添加第二行和第三行。

git添加。gitcommit-am“二分之二”git rebase—继续

我们的承诺有冲突,三,所以让我们解决它:

我们会改变的

one
<<<<<<< HEAD
two and a third
two and two thirds
=======
two
three
>>>>>>> bfb8e46... three

to

one
two and a third
two and two thirds
three

git添加。;git rebase—继续

现在我们的gitlog-p看起来像这样:

commit e59ca35bae8360439823d66d459238779e5b4892
Author: Rose Perrone <roseperrone@fake.com>
Date:   Sun Jul 7 13:57:00 2013 -0700

    three

diff --git a/A b/A
index 5aef867..dd8fb63 100644
--- a/A
+++ b/A
@@ -1,3 +1,4 @@
 one
 two and a third
 two and two thirds
+three

commit 4a283ba9bf83ef664541b467acdd0bb4d770ab8e
Author: Rose Perrone <roseperrone@fake.com>
Date:   Sun Jul 7 14:07:07 2013 -0700

    two and two thirds

diff --git a/A b/A
index 575010a..5aef867 100644
--- a/A
+++ b/A
@@ -1,2 +1,3 @@
 one
 two and a third
+two and two thirds

commit 704d323ca1bc7c45ed8b1714d924adcdc83dfa44
Author: Rose Perrone <roseperrone@fake.com>
Date:   Sun Jul 7 14:06:40 2013 -0700

    two and a third

diff --git a/A b/A
index 5626abf..575010a 100644
--- a/A
+++ b/A
@@ -1 +1,2 @@
 one
+two and a third

commit 9aac58f3893488ec643fecab3c85f5a2f481586f
Author: Rose Perrone <roseperrone@fake.com>
Date:   Sun Jul 7 13:56:40 2013 -0700

    one

diff --git a/A b/A
new file mode 100644
index 0000000..5626abf
--- /dev/null
+++ b/A
@@ -0,0 +1 @@
+one

您可以执行交互式rebase git rebase-i。手册页完全符合您的要求:

http://git-scm.com/docs/git-rebase#_splitting_commits

gitrebase——交互式可用于将提交拆分为较小的提交。关于rebase的Git文档对这个过程进行了简要的演练-拆分提交:

在交互模式下,您可以使用“编辑”操作标记提交。然而,这并不一定意味着gitrebase希望这次编辑的结果正好是一次提交。实际上,您可以撤消提交,也可以添加其他提交。这可用于将提交分为两部分:使用gitrebase-i<commit>^启动交互式rebase,其中<commit’是要拆分的提交。事实上,任何提交范围都可以,只要它包含该提交。使用“编辑”操作标记要拆分的提交。在编辑提交时,执行git-resetHEAD^。结果是HEAD被重绕了一圈,索引也随之改变。然而,工作树保持不变。现在,将更改添加到第一次提交时所需的索引中。您可以使用gitadd(可能以交互方式)或gitgui(或两者都使用)来实现这一点。使用现在合适的提交消息提交当前索引。重复最后两个步骤,直到工作树干净。使用git-rebase继续rebase--Continue。如果您不能绝对确定中间版本是否一致(编译、通过测试套件等),则应在每次提交、测试和修改提交后使用git stash来隐藏尚未提交的更改。

这已经过去8年多了,但也许有人会发现它对你有所帮助。我能够做到这一点,而不需要重新启动。其目的是将git引导到与git提交之前相同的状态:

# first rewind back (mind the dot,
# though it can be any valid path,
# for instance if you want to apply only a subset of the commit)
git reset --hard <previous-commit> .

# apply the changes
git checkout <commit-you-want-to-split>

# we're almost there, but the changes are in the index at the moment,
# hence one more step (exactly as git gently suggests):
# (use "git reset HEAD <file>..." to unstage)
git reset

在此之后,您将看到重置后的这些闪亮的未分级更改:并且您的回购处于一种即将提交所有这些文件的状态。从现在起,你可以像往常一样轻松地再次提交。希望这会有所帮助。

如果您的更改主要是添加新内容,则此方法非常有用。

有时您不希望丢失与正在拆分的提交相关联的提交消息。如果您提交了一些要拆分的更改,可以:

编辑要从文件中删除的更改(即删除行或适当更改文件以适合首次提交)。您可以使用所选编辑器和gitcheckout-p HEAD^--path/to/file的组合将一些更改还原到当前树中。将此编辑作为新提交提交,使用类似gitadd的方式;gitcommit-m“删除以后应该更改的内容”,因此您将在历史中进行原始提交,并且还将进行另一次提交,其中包含您所做的更改,因此当前HEAD上的文件看起来就像您希望在拆分后的第一次提交中一样。

000aaa Original commit
000bbb removal of things that should be changed later

使用git-restore HEAD还原编辑,这将创建还原提交。文件将看起来像原始提交时一样,您的历史记录现在看起来像

000aaa Original commit
000bbb removal of things that should be changed later
000ccc Revert "removal of things that should be changed later" (assuming you didn't edit commit message immediately)

现在,您可以使用gitrebase-i将前两个提交压缩/修复为一个,如果您之前没有向其提供有意义的提交消息,则可以选择修改还原提交。你应该留下来

000ddd Original commit, but without some content that is changed later
000eee Things that should be changed later