在 Git 中只提交文件的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1085162/
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
Commit only part of a file in Git
提问by freddiefujiwara
When I make changes to a file in Git, how can I commit only some of the changes?
当我在 Git 中对文件进行更改时,如何仅提交部分更改?
For example, how could I commit only 15 lines out of 30 lines that have been changed in a file?
例如,我怎么能只提交文件中已更改的 30 行中的 15 行?
回答by cloudhead
You can use git add --patch <filename>
(or -p
for short), and git will begin to break down your file into what it thinks are sensible "hunks" (portions of the file). It will then prompt you with this question:
您可以使用git add --patch <filename>
(或-p
简称),并且 git 将开始将您的文件分解为它认为合理的“大块头”(文件的一部分)。然后它会提示你这个问题:
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?
Here is a description of each option:
以下是每个选项的说明:
- ystage this hunk for the next commit
- ndo not stage this hunk for the next commit
- qquit; do not stage this hunk or any of the remaining hunks
- astage this hunk and all later hunks in the file
- ddo not stage this hunk or any of the later hunks in the file
- gselect a hunk to go to
- /search for a hunk matching the given regex
- jleave this hunk undecided, see next undecided hunk
- Jleave this hunk undecided, see next hunk
- kleave this hunk undecided, see previous undecided hunk
- Kleave this hunk undecided, see previous hunk
- ssplit the current hunk into smaller hunks
- emanually edit the current hunk
- ?print hunk help
- y为下一次提交准备这个大块头
- n不要为下一次提交暂存这个大块头
- q退出; 不要上演这个大块头或任何剩余的大块头
- a在文件中暂存这个大块头和所有后来的大块头
- d不要在文件中暂存这个大块头或任何后面的大块头
- g选择一个大块头去
- /搜索匹配给定正则表达式的大块头
- j让这个大块头未定,看下一个未定的大块头
- J让这个大块头未定,看下一个大块头
- k离开这个大块头未定,见上一个未定的大块头
- K离开这个大块头未定,看以前的大块头
- s将当前大块分割成更小的大块
- e手动编辑当前块
- ?打印大块帮助
If the file is not in the repository yet, you can first do git add -N <filename>
. Afterwards you can go on with git add -p <filename>
.
如果该文件还没有在存储库中,您可以先执行git add -N <filename>
. 之后你可以继续git add -p <filename>
。
Afterwards, you can use:
之后,您可以使用:
git diff --staged
to check that you staged the correct changesgit reset -p
to unstage mistakenly added hunksgit commit -v
to view your commit while you edit the commit message.
git diff --staged
检查您是否进行了正确的更改git reset -p
取消错误添加的帅哥git commit -v
在编辑提交消息时查看您的提交。
Note this is far different than the git format-patch
command, whose purpose is to parse commit data into a .patch
files.
请注意,这与git format-patch
命令有很大不同,其目的是将提交数据解析为.patch
文件。
Reference for future: Git Tools - Interactive Staging
未来参考:Git 工具 - 交互式登台
回答by Jakub Nar?bski
You can use git add --interactive
or git add -p <file>
, and then git commit
(notgit commit -a
); see Interactive modein git-addmanpage, or simply follow instructions.
您可以使用git add --interactive
or , 然后( not); 看到交互模式中的git-ADD手册页,或只需按照说明进行操作。git add -p <file>
git commit
git commit -a
Modern Git has also git commit --interactive
(and git commit --patch
, which is shortcut to patch option in interactive commit).
现代 Git 也有git commit --interactive
(和git commit --patch
,这是交互式提交中补丁选项的快捷方式)。
If you prefer doing it from GUI, you can use git-gui. You can simply mark chunks which you want to have included in commit. I personally find it easier than using git add -i
. Other git GUIs, like QGit or GitX, might also have this functionality as well.
如果您更喜欢从 GUI 执行此操作,则可以使用git-gui。您可以简单地标记要包含在提交中的块。我个人觉得它比使用git add -i
. 其他 git GUI,如 QGit 或 GitX,也可能具有此功能。
回答by Ionu? G. Stan
git guiprovides this functionality under the diff view. Just right click the line(s) you're interested in and you should see a "stage this line to commit" menu item.
git gui在 diff 视图下提供了这个功能。只需右键单击您感兴趣的行,您应该会看到“暂存此行以提交”菜单项。
回答by theFreedomBanana
I believe that git add -e myfile
is the easiest way (my preference at least) since it simply opens a text editor and lets you choose which line you want to stage and which line you don't.
Regarding editing commands:
我相信这git add -e myfile
是最简单的方法(至少是我的偏好),因为它只是打开一个文本编辑器,让您选择要暂存的行和不暂存的行。关于编辑命令:
added content:
补充内容:
Added content is represented by lines beginning with "+". You can prevent staging any addition lines by deleting them.
添加的内容由以“+”开头的行表示。您可以通过删除它们来防止暂存任何添加行。
removed content:
删除的内容:
Removed content is represented by lines beginning with "-". You can prevent staging their removal by converting the "-" to a " " (space).
删除的内容由以“-”开头的行表示。您可以通过将“-”转换为“”(空格)来防止分期删除它们。
modified content:
修改内容:
Modified content is represented by "-" lines (removing the old content) followed by "+" lines (adding the replacement content). You can prevent staging the modification by converting "-" lines to " ", and removing "+" lines. Beware that modifying only half of the pair is likely to introduce confusing changes to the index.
修改后的内容由“-”行(删除旧内容)后跟“+”行(添加替换内容)表示。您可以通过将“-”行转换为“”并删除“+”行来防止暂存修改。请注意,仅修改该对的一半可能会给索引带来令人困惑的更改。
Every details about git add
are available on git --help add
关于的每一个细节git add
都可以在git --help add
回答by Fran?ois
If you are using vim, you may want to try the excellent plugin called fugitive.
如果您正在使用 vim,您可能想尝试名为fugitive的优秀插件。
You can see the diff of a file between working copy and index with :Gdiff
, and then add lines or hunks to the index using classic vim diff commands like dp
. Save the modifications in the index and commit with :Gcommit
, and you're done.
您可以使用 来查看工作副本和索引之间文件的差异:Gdiff
,然后使用经典的 vim diff 命令(如dp
. 将修改保存在索引中并使用 提交:Gcommit
,您就完成了。
回答by Fran?ois
I would strongly recommend using SourceTreefrom Atlassian. (It's free.) It makes this trivial. You can stage individual hunks of code or individual lines of code quickly and easily.
我强烈建议使用来自 Atlassian 的SourceTree。(它是免费的。)它使这变得微不足道。您可以快速轻松地暂存单个代码块或单个代码行。
回答by user1338062
Worth noting that to use git add --patch
for a new fileyou need to first add the file to index with git add --intent-to-add
:
值得注意的是,要git add --patch
用于新文件,您需要首先将文件添加到索引git add --intent-to-add
:
git add -N file
git add -p file
回答by jdsumsion
When I have a lot of changes, and will end up creating a few commits from the changes, then I want to save my starting point temporarily before staging things.
当我有很多更改,并且最终会从更改中创建一些提交,那么我想在暂存之前暂时保存我的起点。
Like this:
像这样:
$ git stash -u
Saved working directory and index state WIP on master: 47a1413 ...
$ git checkout -p stash
... step through patch hunks
$ git commit -m "message for 1st commit"
$ git checkout -p stash
... step through patch hunks
$ git commit -m "message for 2nd commit"
$ git stash pop
Whymarrh's answer is what I usually do, except sometimes there are lots of changes and I can tell I might make a mistake while staging things, and I want a committed state I can fall back on for a second pass.
Whymarrh 的回答是我通常做的,除了有时会有很多变化,我可以告诉我在分阶段做事情时我可能会犯错误,我想要一个我可以依靠的提交状态第二次通过。
回答by Mark van Lent
Should you use emacs, take a look at Magit, which provides a git interface for emacs. It supports staging hunks(parts of files) quite well.
如果您使用 emacs,请查看Magit,它为 emacs 提供了一个 git 接口。它很好地支持暂存大块(文件的一部分)。
回答by Holger Brandl
Intellij IDEA(and I guess all other products of the series) has built in support for partial commits since v2018.1
自 v2018.1 以来,Intellij IDEA(以及我猜该系列的所有其他产品)都内置了对部分提交的支持