假设您的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 注释删除仅添加到下一次提交?
我们想把它分成两个提交:
-
第一次提交涉及删除一些行(注释删除)。
要删除注释行,请不要理会它们,它们已经被标记为跟踪版本控制中的删除,就像我们想要的那样。
-/*#field_teacher_id {
- display: block;
-} */
-
第二次提交是一次更改,通过记录删除和添加来跟踪:
-
删除(删除了旧的选择器行)
为了保留旧的选择器行(在此提交期间不要删除它们),我们希望...
要删除 '-' 行,请将它们设为 ' '
...字面意思是用空格 字符替换减号- 符号。
所以这三行...
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
...将变为(notice所有 3 行中第一行的单个空格):
<br>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 <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;
}
-/*#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 <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;
}
最后你可以看到最后一次提交只包括选择器的变化。