Git:提交部分更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16134656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: commit partial changes
提问by Tamás Pap
There are multiple questions on Stack Overflow addressing the problem of staging and committing only parts from files. However, I can't make it work.
Stack Overflow 上有多个问题可以解决暂存和仅提交文件部分的问题。但是,我无法让它发挥作用。
Let's say we want to implement a dummy math class in php (the language doesn't matter) with some basic methods like: add
, subtract
, multiply
and divide.
比方说,我们要在PHP中实现一个虚拟的数学课(语言无所谓)与像一些基本的方法:add
,subtract
,multiply
和divide.
Let's start with the class definition:
让我们从类定义开始:
<?php
class Math {
}
?>
Now:
现在:
$ git add math.php
$ git commit -m "Create Math class."
In the next step we implement both add
and subtract
four methods:
在下一步中,我们实现了这两种方法add
和subtract
四种方法:
<?php
class Math {
public function add($a, $b) {
return $a + $b;
}
public function subtract($a, $b) {
return $a - $b;
}
}
?>
But now we want to commit the implementation of the add
and the subtract
methods in separate commits.
但是现在我们想在单独的提交中提交add
和subtract
方法的实现。
Is this possible?
这可能吗?
What I tried
我试过的
$ git add -p
$ git add -p
The following appears:
出现以下内容:
I would love to split the hunk into smaller hunks, so I press s
, and the following appears:
我很想将大块头分成更小的块头,所以我按s
,然后出现以下内容:
It seems that git didn't split it into smaller hunks.
似乎 git 没有把它分成更小的块。
Let's try now to manually edit the current hunk. So I press: e
.
现在让我们尝试手动编辑当前大块。所以我按:e
。
My default text editor (sublime text) opens up, and here I can edit the hunk:
我的默认文本编辑器(崇高文本)打开,在这里我可以编辑大块:
I try to simply delete line 8,9,10,11 - because I want to stage only the add
function.
I save, close the editor, but git says:
我尝试简单地删除第 8、9、10、11 行 - 因为我只想暂存add
函数。我保存,关闭编辑器,但 git 说:
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?
I'm really new to git add -p
and interactive staging, so maybe it's something I do wrong, or simply it's not possible what I want, and really hope some more experienced git users have some instructions maybe also a solution for me.
我对git add -p
交互式登台非常陌生,所以可能是我做错了什么,或者根本不可能是我想要的,真的希望一些更有经验的 git 用户有一些说明,也许对我来说也是一个解决方案。
Thank you!
谢谢!
采纳答案by Ben Hymanson
You are doing exactly what I've done in the past, and I even recreated your entire experiment on a UNIX machine and it worked just as expected.
您所做的正是我过去所做的,我什至在 UNIX 机器上重新创建了您的整个实验,并且它按预期工作。
I suspect your editor is changing the line endings in the diff hunk when it saves.
我怀疑您的编辑器在保存时更改了差异块中的行尾。
回答by Chronial
This doesn't feel like the absolute “git way” to do it this since git loves the command line, but here's what i do:
感觉这不是绝对的“git 方式”,因为 git 喜欢命令行,但这是我所做的:
Git comes bundled with git gui
– you can start it by running just that command. There you can select your unstaged file, mark the lines you want to commit and click “stages lines for commit”. If you want it to behave more like a command-line tool, you can run git gui citool
, which has the effect that the ui will close when you commit.
Git 与捆绑在一起git gui
- 您可以通过运行该命令来启动它。在那里您可以选择未暂存的文件,标记要提交的行,然后单击“暂存行以进行提交”。如果你想让它表现得更像一个命令行工具,你可以运行git gui citool
,它的效果是在你提交时 ui 将关闭。
Note that this tool is properly integrated with git. You don't need to make your commit in this ui. You can just stage the relevant lines, close it, maybe do some more git add
and then commit via the command line.
请注意,此工具已与 git 正确集成。您无需在此 ui 中进行提交。您可以只暂存相关行,关闭它,也许再做一些git add
,然后通过命令行提交。
In general I love the git comand line interface and even though I am not necessarily a shell fan, I just got to love it after a while. I tried multiple GUIs and threw them all away because they can't compete with the git command line interface. But for git add -p
I would definitely say that that is a task for a GUI. Doing partial commits is just way easier with git gui
. Same goes for complex history browsing. gitk
(also shipped with git) does a way better job there than git log --graph -p
(as long as you really need to understand the tree and don't quite now about the state of the history).
总的来说,我喜欢 git 命令行界面,尽管我不一定是 shell 粉丝,但一段时间后我还是会喜欢上它。我尝试了多个 GUI 并将它们全部扔掉,因为它们无法与 git 命令行界面竞争。但是因为git add -p
我肯定会说这是 GUI 的任务。使用git gui
. 复杂的历史浏览也是如此。gitk
(也随 git 一起提供)在那里做得比git log --graph -p
(只要您确实需要了解树并且现在不太了解历史状态)做得更好。
Note:But you still seem to be doing everything right – I tried what you did and have to agree with Ben Hymanson. That should definitely work. Your editor must be screwing something up.
注意:但你似乎仍然做对了一切——我尝试了你所做的并且不得不同意本Hyman逊的观点。那应该肯定有效。你的编辑一定是搞砸了。
Final Note: You seem to be using the git command line with the standard windows cmd
terminal. I strongly recommend switching to mintty. Download the msys version, put it in your Program Files\Git\bin
directory and run it (create a shortcut) with mintty -
. This will get you the same shell, but with better everything (especially better resizing and copy&paste).
最后注意:您似乎在标准 Windowscmd
终端中使用 git 命令行。我强烈建议切换到mintty。下载 msys 版本,将其放在您的Program Files\Git\bin
目录中并使用mintty -
. 这将为您提供相同的外壳,但具有更好的一切(尤其是更好的调整大小和复制和粘贴)。