1. git add 忽略空白修改
有时候只修改了几行代码,但是git diff时一大片代码都红了,git add 又没–ignore-space-change选项。在stackoverflow找了一个不错的方法。
为了方便可以绑定别名
alias.addnw=!sh -c 'git diff -U0 -w --no-color "$@" | git apply --cached --ignore-whitespace --unidiff-zero -'
或者直接运行
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
使用方法
git addnw your/file
参数解释
Added options -U0, and –unidiff-zero respectively to workaround context matching issues, according to this comment.
Basically it applies the patch which would be applied with add without whitespace changes. You will notice that after a git addnw your/file there will still be unstaged changes, it’s the whitespaces left.
The –no-color isn’t required but as I have colors set to always, I have to use it. Anyway, better safe than sorry.
2. git format-patch 不自动省略中括号
如果commit注释首行是以中括号[xxx]开头的,直接使用git am打补丁会去掉注释中的[xxx],这时候使用-k,参数就可以解决这个问题。
生成补丁
git format-patch -k -1 comnit_id
打补丁
git am -k patch_file
3. 修改未push到服务器的提交的注释
只修改最后一次提交的
git commit --amend
修改历史提交的话,使用rebase命令,例如前两个提交
git rebase -i HEAD~2
接着在弹出的编辑框中,将pick改成e,然后
git commit --amend
修改完成,执行命令,继续修改
git rebase --continue
直到全部修改完成