有时我们修改了一个文件里的多个地方,但只想提交其中一处,这时可以使用git add -p
命令进行操作。
假设我们当前的test.txt文件里的内容为:
111
222
333
现在修改成了
1
222
33333
如果我们只想提交第一处的修改,而暂时先不提交第二处的修改,可以使用git add -p
进入交互模式。此时可以看到类似下面的界面:
-111
+1
222
-333
+33333
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]?
其中各个字母代表的含义如下,也可以直接输入回车查看:
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
首先我们需要输入s并按回车进行切分,此时git会提示我们已经将修改分为两段了,第一段我们输入y按回车,第二段我们输入n按回车,这时就可以只提交第一段修改的代码了。