我可以用git拆分已经拆分的大块头吗?

问题描述 投票:170回答:4

我最近在patch命令中发现了git的add选项,我必须说它确实是一个很棒的功能。我还发现通过按下s键可以将大块子分成较小的块,这增加了提交的精度。但如果我想要更精确,如果分裂的大块不够小怎么办?

例如,考虑这已经拆分的大块头:

@@ -34,12 +34,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

如何仅将CSS注释删除添加到下一次提交? s选项不再可用!

git split add patch
4个回答
218
投票

如果您正在使用git add -p,甚至在使用s分割后,您没有足够小的更改,您可以使用e直接编辑补丁。

这可能有点令人困惑,但是如果你仔细按照编辑器窗口中的说明按下e后会打开,那么你会没事的。在您引用的情况下,您可能希望将-替换为这些行开头的空格:

-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {

...并删除以下行,即以+开头的行。如果您随后保存并退出编辑器,则只会删除CSS注释。


50
投票

让我们说你的example.css看起来像这样:

.classname {
  width: 440px;
}

/*#field_teacher_id {
  display: block;
} */

form.table-form #field_teacher + label,
form.table-form #field_producer_distributor + label {
  width: 300px;
}

.another {
  width: 420px;
}

现在让我们改变中间块中的样式选择器,当我们处理它时,删除一些我们不再需要的旧注释样式。

.classname {
  width: 440px;
}

#user-register form.table-form .field-type-checkbox label {
  width: 300px;
}

.another {
  width: 420px;
}

这很容易,现在让我们承诺。但是等等,我想保持版本控制中的更改的逻辑分离,以便进行简单的逐步代码审查,这样我和我的团队就可以轻松地搜索提交历史记录以获取具体信息。

删除旧代码在逻辑上与其他样式选择器更改分开。我们需要两个不同的提交,所以让我们添加一个补丁。

git add --patch
diff --git a/example.css b/example.css
index 426449d..50ecff9 100644
--- a/example.css
+++ b/example.css
@@ -2,12 +2,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

Stage this hunk [y,n,q,a,d,/,e,?]?

哎呀,看起来变化太近了,所以git将它们混为一谈。

即使尝试通过按s来分割它也会产生相同的结果,因为分割对于我们的精度变化来说不够精细。更改的行之间需要不变的行,以便git能够自动拆分补丁。

所以,让我们按e手动编辑它

Stage this hunk [y,n,q,a,d,/,e,?]? e

git将在我们选择的编辑器中打开补丁。

# Manual hunk edit mode -- see bottom for a quick guide
@@ -2,12 +2,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.

让我们回顾一下目标:

如何仅将CSS注释删除添加到下一次提交?

我们想把它分成两个提交:

  1. 第一次提交涉及删除一些行(注释删除)。 要删除注释行,只需将它们保持不变,它们已被标记为跟踪版本控制中的删除,就像我们想要的那样。 -/*#field_teacher_id { - display: block; -} */
  2. 第二个提交是一个更改,通过记录删除和添加来跟踪: 删除(删除旧的选择器行) 要保留旧的选择器行(在此提交期间不要删除它们),我们希望... 要删除' - '行,请将它们设为'' ......字面意思是用空间-字符替换负标志。 所以这三条线...... - -form.table-form #field_teacher + label, -form.table-form #field_producer_distributor + label { ...将成为(注意所有3行中第一个的单个空格): form.table-form #field_teacher + label, form.table-form #field_producer_distributor + label { 添加(添加了新的选择线) 为了不注意在此提交期间添加的新选择器行,我们希望... 要删除“+”行,请将其删除。 ...字面意思是删除整行: +#user-register form.table-form .field-type-checkbox label { (额外奖励:如果您恰好使用vim作为编辑器,请按dd删除一行.Nano用户按Ctrl + K)

保存时,您的编辑器应如下所示:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -2,12 +2,7 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */

 form.table-form #field_teacher + label,
 form.table-form #field_producer_distributor + label {
   width: 300px;
 }

# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.

现在让我们提交。

git commit -m "remove old code"

为了确保,让我们看看上次提交的变化。

git show
commit 572ecbc7beecca495c8965ce54fbccabdd085112
Author: Jeff Puckett <[email protected]>
Date:   Sat Jun 11 17:06:48 2016 -0500

    remove old code

diff --git a/example.css b/example.css
index 426449d..d04c832 100644
--- a/example.css
+++ b/example.css
@@ -2,9 +2,6 @@
   width: 440px;
 }

-/*#field_teacher_id {
-  display: block;
-} */

 form.table-form #field_teacher + label,
 form.table-form #field_producer_distributor + label {

完美 - 您可以看到只有删除被包含在原子提交中。现在让我们完成工作并完成剩下的工作。

git add .
git commit -m "change selectors"
git show
commit 83ec3c16b73bca799e4ed525148cf303e0bd39f9
Author: Jeff Puckett <[email protected]>
Date:   Sat Jun 11 17:09:12 2016 -0500

    change selectors

diff --git a/example.css b/example.css
index d04c832..50ecff9 100644
--- a/example.css
+++ b/example.css
@@ -2,9 +2,7 @@
   width: 440px;
 }

-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
   width: 300px;
 }

最后,您可以看到最后一次提交仅包括选择器更改。


9
投票

如果你可以使用git gui,它允许你逐行进行更改。不幸的是,我不知道如何从命令行执行此操作 - 或者即使可能。

我过去使用的另一个选项是回滚部分更改(保持编辑器打开),提交我想要的位,撤消并从编辑器重新保存。不是很优雅,但完成工作。 :)


编辑(git-gui用法):

我不确定msysgit和linux版本中的git-gui是否相同,我只使用了msysgit。但假设它是相同的,当你运行它时,有四个窗格:左上窗格是你的工作目录更改,左下角是你的阶段更改,右上角是所选文件的差异(是工作目录)或者分阶段),右下角用于描述提交(我怀疑你不需要它)。当您单击右上角的文件时,您将看到差异。如果右键单击diff线,您将看到一个上下文菜单。需要注意的两个选项是“提交阶段大块”和“提交阶段线”。您继续在要提交的行上选择“提交阶段行”,您就完成了。您甚至可以选择多行并根据需要进行分级。您始终可以单击暂存框中的文件以查看您要提交的内容。

至于提交,您可以使用gui工具或命令行。


1
投票

一种方法是跳过大块,git add你需要的任何其他东西,然后再次运行git add。如果这是唯一的块,您将能够拆分它。

如果你担心提交的顺序,只需使用git rebase -i

© www.soinside.com 2019 - 2024. All rights reserved.