git如何只提交一个文件中的部分修改

有时我们修改了一个文件里的多个地方,但只想提交其中一处,这时可以使用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按回车,这时就可以只提交第一段修改的代码了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值