有时同时改了两个功能A和B,并且都没有提交,这两个功能涉及到了同一个文件a,都进行了修改,但在某次提交中只希望提交功能A,所以只能提交文件a的关于功能A的部分修改,我们可以使用下面的命令对文件a进行选择性提交:
git add --patch <filename>
或者
git add -p <filename>
执行此命令后,Git将开启一个交互式模式,它会将文件a分解成它认为合理的“块(hunk)”(文件的一部分),并和你分别确认如何处理每处的修改:
Stage this hunk [y,n,a,d,g,/,j,J,k,K,s,e,?]?
参数说明:
- y 暂存该块 - n 不暂存该块 - a 暂存该块及该文件中的剩余所有块 - d 不暂存该块及该文件中的剩余所有块 - g 选择并跳转至指定块块 - / 搜索与给定的正则表达式匹配的块 - j 离开当前未定的块,处理下一个未定的块 - J 离开当前未定的块,处理下一个块 - k 离开当前未定的块,处理上一个未定的块 - K 离开当前未定的块,处理上一个块 - s 将当前的大块分成更小的块 - e 手动编辑当前的块 - ? 打印帮助