git add文档全部的变更只提交一部分

由于我有代码洁癖,喜欢一个git commit只包含彼此相关的变更,这就意味着有时一个变更了的文件,只有一部分要提交,而非全部。这时git add -p命令就能派上用处。

git add -p src/path/to/file.cpp

这时会进入到一个交互界面。下面有可选命令提示。界面上展示的变更和git diff的相同,可选命令针对界面上的变更。

可选的命令:
h 可选命令的帮助
s 当前展示的大块(hunk)变更再拆分成小块
y 接受(界面上展示的)变更
n 不接受(界面上展示的)变更
q 退出

高版本的git没有s子命令,用e实现相同的功能
错误"Your edited hunk does not apply"是文件编辑的有问题,
官网教程看到正确操作:

added content
Added content is represented by lines beginning with “+”. You can prevent staging any addition lines by deleting them.

removed content
Removed content is represented by lines beginning with “-”. You can prevent staging their removal by converting the “-” to a " " (space).

modified content
Modified content is represented by “-” lines (removing the old content) followed by “+” lines (adding the replacement content). You can prevent staging the modification by converting “-” lines to " ", and removing “+” lines. Beware that modifying only half of the pair is likely to introduce confusing changes to the index.

概括起来三点:

  1. 新增行,不想提交(但是变更仍然在工作区)。删除整行
  2. 删除行,不想提交。用空格符" "代替"-"字符。
  3. 变更行,不想提交。上面两个结合——对于"+"所在行整行删除,对于"-"所在行,用空格符" "代替"-"字符

git status,可以看到file.cpp有两个,一个是在工作区,另一个在暂存区。其中暂存区里包含的是git add -p种被接受的变更;而工作区是不接受的变更。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值