如何编辑Git“添加补丁”选择性舞台期间的帅哥/差异/台词?
我有一个源文件,其中添加了 2 个功能。为了允许挑选,我想分两个阶段进行:每个功能一个阶段。到目前为止,在类似的情况下,使用 git add -p 效果很好,可以提交一项功能,同时将本地文件保留在最后阶段。
但是,我现在遇到的问题是 git add -p 想要上演一个包含这两个功能编辑的大块。即使编辑是在单独的行上,s
(“分割”)不再希望将大块分成更小的部分......
简而言之:我无法将 2 的更改分开具有这样的特点。有没有办法手动编辑补丁,例如使用 vi,而不实际更改原始文件?
I have a source file where 2 features have been added. In order to allow cherry-picking, I'd like to commit that in 2 phases: one for each feature. Until now, in similar situations, using git add -p
served me well, to commit one feature while leaving the local files in their final stage.
However, I now have the problem that git add -p
wants to stage a hunk that includes edits for both features. Even though the edits are on separate lines, s
(for "split") no longer wants to split up the hunk into smaller pieces...
In short: I can't separate the changes for the 2 features this way. Is there a way to manually edit the patch, for example using vi, without actually changing the original file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 Alan 所说,在
git add -p
期间按e
(而不是s
)来编辑补丁。这将启动带有该补丁块的编辑器,以便您可以手动编辑它。文本中有一些注释解释了如何正确丢弃修改,实际上非常简单。完成后,请注意,您可以通过执行
git stash --keep-index
来测试它仅使用刚刚添加的更改。您未添加到索引的更改将被隐藏起来,您现在可以自由地测试您将要提交的更改。完成后,只需git stash pop
或git stash apply
即可恢复其他更改。As Alan says, edit the patch by pressing
e
(instead ofs
) duringgit add -p
. This will launch your editor with that hunk of the patch so that you can manually edit it. There are comments within the text that explain how to properly discard modifications and it's actually pretty easy.When you are done, note that you can test it with only the changes you've just added by doing
git stash --keep-index
. The changes you did not add to the index will be stashed away and you are now free to test just the changes that you are about to commit. When done, simplygit stash pop
orgit stash apply
to get the other changes back.正如其他人所说,您可以使用
e
编辑要拆分的块。要仅添加块的一部分,您可以从要拆分的更改中删除行。
假设您想将第 1 行和第 3 行保留在一次提交中,将第 2 行保留在另一次提交中。您所要做的就是删除第 2 行:
这会将第 1 行和第 3 行放入您的停靠区域。第 2 行仍将被索引,但不会暂存。
Like other people have said, you can use
e
to edit the hunk you want to split up.To only add a portion of the hunk, you can delete the lines from the change you want to split out.
Lets say you want to keep Line 1 and Line 3 in one commit and Line 2 in another. All you have to do is delete Line 2:
This will put Line 1 and Line 3 in your staging area. Line 2 will still be indexed but not staged.
您可以在
git add -p
期间按e
来编辑补丁。不会影响原文件。You can edit the patch by pressing
e
during agit add -p
. It will not affect the original file.有些 Git GUI 可以让您选择要暂存的单独行,从而可以分割通常无法从命令行使用常规
git add --patch
的行。两个这样的 GUI 是:
There are Git GUIs that will let you select individual lines that you want to stage, allowing you to split lines that you normally wouldn't be able to using the regular
git add --patch
from the command line.Two such GUIs are:
我通常会从 @Dan 的答案中描述的 git stash pop 中得到合并冲突。有关避免冲突的解决方案,请参阅 git stash 和edited hunks。
I usually get merge conflicts from the
git stash pop
described in @Dan's answer. See git stash and edited hunks for a solution that avoids the conflicts.