git在提交之前删除新文件中的尾部空白 [英] git remove trailing whitespace in new files before commit

查看:120
本文介绍了git在提交之前删除新文件中的尾部空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道删除尾随空白可以用预先提交的钩子完成。我有兴趣做手动。我在这里阅读这个问题:

自动制作git提交之前删除尾部空白
Stack Overflow


最接近我想要的答案是
automatic版本ntc2


 (export VISUAL =:& & git -c apply.whitespace = fix add -ue。)&& git结帐。 &安培;&安培; git reset 



该命令运行良好,除了它似乎只适用于更改对于已经在回购站中的文件,不是新文件。我有一堆新的文件,这意味着他们还没有进入回购。我想从这些文件中删除空白,所以我尝试添加-A而不是-u,但这并没有什么不同。

解决方案

<你可以这样做:



git rebase --whitespace = fix HEAD〜3

要手动清理最后3个提交中的空白, / code>



当我在一个主题分支上工作时,我追踪上游分支(通常通过像这样创建)



git checkout -b topic -t



允许我放弃最后一个参数从 git rebase 。所以一旦我完成了&准备合并,我可以快速清理整个主题分支:

git ws #别名为rebase - whitespace = fix



请注意,与HEAD〜3示例不同的是,如果更改了上游分支,它实际上会更改您的更改! (但这也是我想要的,在我的工作流程中。)


I know removing trailing whitespace can be done with a pre-commit hook. I am interested in doing it manually. I read the question here:
Make git automatically remove trailing whitespace before committing - Stack Overflow
The answer closest to what I want is the "automatic version" from ntc2:

(export VISUAL=: && git -c apply.whitespace=fix add -ue .) && git checkout . && git reset


That command works well except it seems to be only for changes on files that are already in the repo, not new files. I have a bunch of files that are new, meaning they aren't yet in the repo. I want to remove whitespace from those files so I tried add -A instead of -u but that didn't make a difference.

解决方案

To manually clean up whitespace from your last 3 commits, you can do this:

git rebase --whitespace=fix HEAD~3

When I work on a topic branch, I track the upstream branch (usually by creating it like this)

git checkout -b topic -t

Which allows me to drop the last argument from git rebase. So once I'm done & ready to merge, I can clean the whole topic branch quickly with:

git ws # aliased to rebase --whitespace=fix

Note that, unlike the HEAD~3 example, this will actually rebase your changes upon the upstream branch if it's changed! (But that's also what I want, in my workflow.)

这篇关于git在提交之前删除新文件中的尾部空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆