当前位置:   article > 正文

git add文档全部的变更只提交一部分_只想提交部分add文件怎么办

只想提交部分add文件怎么办

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

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

这时会进入到一个交互界面。下面有可选命令提示。界面上展示的变更和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种被接受的变更;而工作区是不接受的变更。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/851078
推荐阅读
相关标签
  

闽ICP备14008679号