在Git中只提交一部分文件 [英] Commit only part of a file in Git
问题描述
当我在Git中修改一个文件时,我该如何只提交一些修改?
例如,我怎么才能只提交15行30行已被更改为文件?
您可以使用 git add --patch< ;文件名>
(或简称为 -p
),git将开始将文件分解成它认为合理的hunk(文件的一部分)。然后它会提示你这个问题:
将这个hunk [y,n,q,a,d,/,j ,J,G,S,E,?]?
以下是每个选项的说明:
- y 为下一次提交做好准备
- n 下一次提交
- q quit;不要将这个大块头或任何剩余的笨蛋放到这里
-
- a
- g 选择一个要去的文件夹 li>
- / 搜索与给定正则表达式匹配的hunk
- j 未来未确定的垃圾
- J 请将这个垃圾箱未定,请参阅下一个垃圾箱
- k leave这个笨蛋未定,请参阅以前的未定hunk
- K 离开这个大块未定,请参阅前一个hunk
- s git add -N< filename> 。之后,您可以继续使用
git add -p >
。 之后,您可以使用:< br>
git diff --staged
检查您是否执行了正确的更改
git reset -p
在您编辑提交消息时忽略错误地添加的宏块
git commit -v
来查看您的提交。
请注意,这与 git format-patch
命令有很大不同,其目的是将提交数据解析为 .patch
文件。
未来的参考: https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging
When I make changes to a file in Git, how can I commit only some of the changes?
For example, how could I commit only 15 lines out of 30 lines that have been changed in a file?
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:
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?
Here is a description of each option:
- y stage this hunk for the next commit
- n do not stage this hunk for the next commit
- q quit; do not stage this hunk or any of the remaining hunks
- a stage this hunk and all later hunks in the file
- d do not stage this hunk or any of the later hunks in the file
- g select a hunk to go to
- / search for a hunk matching the given regex
- j leave this hunk undecided, see next undecided hunk
- J leave this hunk undecided, see next hunk
- k leave this hunk undecided, see previous undecided hunk
- K leave this hunk undecided, see previous hunk
- s split the current hunk into smaller hunks
- e manually edit the current hunk
- ? print hunk help
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>
.
Afterwards, you can use:
git diff --staged
to check that you staged correct changes
git reset -p
to unstage mistakenly added hunks
git commit -v
to view your commit while you edit the commit message.
Note this is far different than the git format-patch
command, which purpose is to parse commit data into a .patch
files.
Reference for future: https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging
这篇关于在Git中只提交一部分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!