在提交之前,让git自动删除尾部空白 [英] Make git automatically remove trailing whitespace before committing

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

问题描述

我使用git与我的团队,并希望从我的差异,日志,合并等删除空白更改。我假设这样做最简单的方法是git自动删除结尾的空白(和其他的空白错误)从所有提交应用。



我试图通过〜/ .gitconfig 文件,但是当我提交时它不会做任何事情。也许它是为不同的事物设计的。什么是解决方案?

  [核心] 
whitespace =尾部空间,空格前标签
[apply]
whitespace = fix

我使用ruby来防止任何人有红宝石具体的想法。自动代码格式化提交之前将是下一步,但这是一个难题,并没有真正造成大问题。

那些设置( core.whitespace apply.whitespace )不会删除尾部空格,而是:




  • core.whitespace :检测它们并引发错误
  • apply.whitespace :并剥离它们,但仅在修补程序期间,而不是始终自动删除


    我相信 git hook pre-提交 可以做得更好(包括删除结尾的空白)

    $ hr

    请注意,在任何时候,您都可以选择不运行 pre-commit 钩子:




    • 暂时: git commit --no-verify。

    • cd .git / hooks /; chmod -x pre-commit



    警告:默认情况下,提交脚本(如此一个),有不是一个删除尾随功能,但一个警告功能,如:

      if(/ \s $ /){
    bad_line(Last whitespace,$ _);
    }

    然而,您可以 建立一个更好的 pre-commit a> ,特别是当你考虑到这一点时:


    可能永远不会作为工作副本存在的原子修订







    例如, oldman 在另一个答案中提出了 a pre-commit 钩子检测并删除空白。

    由于该钩子获取每个文件的文件名,我建议小心某些类型的文件: t要删除 .md (降价)文件中的尾部空格!


    I'm using git with my team and would like to remove whitespace changes from my diffs, logs, merges, etc. I'm assuming that the easiest way to do this would be for git to automatically remove trailing whitespace (and other whitespace errors) from all commits as they are applied.

    I have tried to add the following to by ~/.gitconfig file but it doesn't do anything when I commit. Maybe it's designed for something different. What's the solution?

    [core]
        whitespace = trailing-space,space-before-tab
    [apply]
        whitespace = fix
    

    I'm using ruby in case anyone has any ruby specific ideas. Automatic code formatting before committing would be the next step, but that's a hard problem and not really causing a big problem.

    解决方案

    Those settings (core.whitespace and apply.whitespace) are not there to remove trailing whitespace but to:

    • core.whitespace: detect them, and raise errors
    • apply.whitespace: and strip them, but only during patch, not "always automatically"

    I believe the git hook pre-commit would do a better job for that (includes removing trailing whitespace)


    Note that at any given time you can choose to not run the pre-commit hook:

    • temporarily: git commit --no-verify .
    • permanently: cd .git/hooks/ ; chmod -x pre-commit

    Warning: by default, a pre-commit script (like this one), has not a "remove trailing" feature", but a "warning" feature like:

    if (/\s$/) {
        bad_line("trailing whitespace", $_);
    }
    

    You could however build a better pre-commit hook, especially when you consider that:

    Committing in Git with only some changes added to the staging area still results in an "atomic" revision that may never have existed as a working copy and may not work.


    For instance, oldman proposes in another answer a pre-commit hook which detects and remove whitespace.
    Since that hook get the file name of each file, I would recommend to be careful for certain type of files: you don't want to remove trailing whitespace in .md (markdown) files!

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

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