赞
踩
由于我有代码洁癖,喜欢一个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.
概括起来三点:
" "
代替"-"
字符。"+"
所在行整行删除,对于"-"
所在行,用空格符" "
代替"-"
字符git status
,可以看到file.cpp有两个,一个是在工作区,另一个在暂存区。其中暂存区里包含的是git add -p
种被接受的变更;而工作区是不接受的变更。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。