我最近发现了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 gui,它允许您逐行进行更改。不幸的是,我不知道如何从命令行完成它——甚至不知道它是否可能。

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


编辑(git gui):

I am not sure if the git-gui is the same in msysgit and linux versions, I've only used the msysgit one. But assuming it is the same, when you run it, there are four panes: top-left pane is your working directory changes, bottom-left is your stages changes, top-right is the diff for the selected file (be it working dir or staged), and bottom right is for description of the commit (I suspect you won't need it). When you click a file in the top-right one, you will see the diff. If you right-click on a diff line, you'll see a context menu. The two options to note are "stage hunk for commit" and "stage line for commit". You keep selecting "stage line for commit" on the lines you want to commit, and you are done. You can even select several lines and stage them if you want. You can always click the file in the staging box to see what you are bout to commit.

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

其他回答

如果你正在使用git add -p,即使在使用s分割之后,你也没有足够小的变化,你可以使用e直接编辑补丁。

这可能有点令人困惑,但如果你仔细按照编辑器窗口中的说明,按下e后就会打开,那么你就没问题了。在你引用的情况下,你会想要在这几行开头用空格替换-:

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

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

一种方法是跳过这个块,添加任何你需要的东西,然后再次运行git add。如果这是唯一的一块,你就可以把它分开。

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

让我们说你的例子。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 指数426449 d . .50 ecff9 100644 ——/ example.css + + + b / example.css @@ -2,12 +2,7 @@ 宽度:440 px; } - / * # field_teacher_id { - display: block; -} * / - —构成。表格形式#field_teacher +标签, —构成。Table-form #field_producer_distributor + label { + #用户注册表单。表-form .field-type-checkbox标签{ 宽度:300 px; } 舞台这个大块头[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注释删除只到下一次提交?

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

The first commit involves deleting some lines (comment removal). To remove the commented lines, just leave them alone, they are already marked to track the deletions in version control just like we want. -/*#field_teacher_id { - display: block; -} */ The second commit is a change, which is tracked by recording both deletions and additions: Deletions (old selector lines removed) To keep the old selector lines (do not delete them during this commit), we want... To remove '-' lines, make them ' ' ...which literally means replacing the minus - signs with a space character. So these three lines... - -form.table-form #field_teacher + label, -form.table-form #field_producer_distributor + label { ...will become (notice the single space at the first of all 3 lines): form.table-form #field_teacher + label, form.table-form #field_producer_distributor + label { Additions (new selector line added) To not pay attention to the new selector line added during this commit, we want... To remove '+' lines, delete them. ...which literally means to delete the whole line: +#user-register form.table-form .field-type-checkbox label { (Bonus: If you happen to be using vim as your editor, press dd to delete a line. Nano users press 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

提交572年ecbc7beecca495c8965ce54fbccabdd085112 作者:杰夫·帕克特<jeff@jeffpuckett.com> 日期:2016年6月11日星期六17:06:48 -0500 删除旧代码 Diff——git a/example.css b/example.css 指数426449 d . .d04c832 100644 ——/ example.css + + + b / example.css @@ -2,9 +2,6 @@ 宽度:440 px; } - / * # field_teacher_id { - display: block; -} * / 的形式。表格形式#field_teacher +标签, 的形式。Table-form #field_producer_distributor + label {

完美——您可以看到原子提交中只包含了删除。现在让我们完成工作,把剩下的交给他们。

git add .
git commit -m "change selectors"
git show

提交83年ec3c16b73bca799e4ed525148cf303e0bd39f9 作者:杰夫·帕克特<jeff@jeffpuckett.com> 日期:2016年6月11日星期六17:09:12 -0500 改变选择器 Diff——git a/example.css b/example.css 指数d04c832 . .50 ecff9 100644 ——/ example.css + + + b / example.css @@ -2,9 +2,7 @@ 宽度:440 px; } - —构成。表格形式#field_teacher +标签, —构成。Table-form #field_producer_distributor + label { + #用户注册表单。表-form .field-type-checkbox标签{ 宽度:300 px; }

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

如果您可以使用git gui,它允许您逐行进行更改。不幸的是,我不知道如何从命令行完成它——甚至不知道它是否可能。

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


编辑(git gui):

I am not sure if the git-gui is the same in msysgit and linux versions, I've only used the msysgit one. But assuming it is the same, when you run it, there are four panes: top-left pane is your working directory changes, bottom-left is your stages changes, top-right is the diff for the selected file (be it working dir or staged), and bottom right is for description of the commit (I suspect you won't need it). When you click a file in the top-right one, you will see the diff. If you right-click on a diff line, you'll see a context menu. The two options to note are "stage hunk for commit" and "stage line for commit". You keep selecting "stage line for commit" on the lines you want to commit, and you are done. You can even select several lines and stage them if you want. You can always click the file in the staging box to see what you are bout to commit.

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