有时同时改了两个功能A和B,并且都没有提交,这两个功能涉及到了同一个文件a,都进行了修改,但在某次提交中只希望提交功能A,所以只能提交文件a的关于功能A的部分修改,我们可以使用下面的命令对文件a进行选择性提交:
git add --patch <filename>
或其简短模式:
git add -p <filename>
执行此命令后,Git将开启一个交互式模式,它会将文件a分解成它认为合理的“块(hunk)”(文件的一部分),并和你分别确认如何处理每处的修改:
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? y
以下是每个选项的说明:
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
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
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
如果该文件尚未存储在存储库中,则可以先执行 git add -N <filename>。之后你可以继续
git add -p <filename>。
之后,您可以使用:
- git diff --staged 检查暂存的修改是否正确
- git reset -p 撤销暂存的错误修改
- git commit -v 在编辑提交消息时查看您的提交










网友评论