git 技巧

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

直到全部修改完成

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值