假设你的example.css
长这个样子:
.classname {
width: 440px;
}
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;
}
-
-
-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;
}
-
-
-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注释删除添加到下一个提交中?
我们想将其拆分为两个提交:
第一个提交涉及删除一些行(注释删除)。
要删除注释行,请不要做任何修改,它们已经被标记以跟踪版本控制中的删除操作,就像我们想要的那样。
-/*#field_teacher_id {
- display: block;
-} */
第二个提交是更改,通过记录删除和添加来跟踪:
删除(旧选择器行被删除)
为了保留旧选择器行(在此提交期间不要删除它们),我们希望...
删除 '-' 行,将其替换为 ' '
...这意味着将减号 -
替换为一个空格字符
。
因此,这三行...
-
-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;
}
-
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 <jeff@jeffpuckett.com>
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;
}
-
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 <jeff@jeffpuckett.com>
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;
}
最后,您可以看到最后一次提交仅包括选择器更改。
git add -p
并使用e
编辑了一个块,那应该只影响已暂存的内容,而不是你的工作目录。 - Mark Longair