代码之家  ›  专栏  ›  技术社区  ›  user1742529 Mihai

如何git只添加非空白更改和新文件?

  •  6
  • user1742529 Mihai  · 技术社区  · 12 年前
    1. 我们可以对某些类型的忽略空白区进行区分:

      1) git diff --ignore-space-at-eol #忽略EOL处的空白更改。

      2) git diff --ignore-space-change / git diff -b #忽略空白量的更改。

      3) git diff --ignore-all-space / git diff -w #完全空白忽略

    2. 我们可以做到

      git apply --ignore-whitespace \ git apply --ignore-space-change #应用补丁时忽略白边

    但是如何从 git add * ?

    这些解决方案不适用于我:

    1)

     git diff -w --no-color | git apply --cached --ignore-whitespace
    

    -它有时会写入错误,并且不会将新文件添加到跟踪中。

    2)

     git add `git diff -w --ignore-submodules |grep "^[+][+][+]" |cut -c7-`
    

    -它写错误,什么也不做(可能是因为我有二进制文件,而不仅仅是文本文件)

    P.S.:也许有办法用上次提交的文件替换文件(在末尾或行处有空格差异,在EOF差异之前有空格)?

    2 回复  |  直到 11 年前
        1
  •  0
  •   Community Mohan Dere    6 年前

    这个问题的唯一真正解决方案是。

    解决方案是使用特殊设置重新创建git存储库,然后将原始提交从一个签出状态复制到另一个状态。

    源错误存储库:

    /家庭/用户/真功率

    新的良好存储库:

    /家庭/用户/onepower

    cd /home/user
    rm -rf ./onepower
    mkdir ./onepower
    cd ./onepower
    git init
    
    # set style of lineendings to be autoconverted to Linux/Unix LF
    #git config core.autocrlf true # uncomment this if you prefer Windows CRLF style
    git config core.autocrlf input # comment this if you prefer Windows CRLF style
    
    # set trailing whitespace (and other similar) to ignore
    git config core.whitespace \
    trailing-space,space-before-tab,indent-with-non-tab
    
    # you can use git config --global ... if you want global settings changing.
    
    cd ../truepower
    git log
    

    提交cccc

    提交bbbb

    提交aaaa

    cd ../truepower
    git checkout aaaa
    cd ../onepower
    rm -rf ./*
    cp -a ../truepower/* ./
    git add -A
    git commit -m "fixed aaaa"
    
    cd ../truepower
    git checkout bbbb
    cd ../onepower
    rm -rf ./*
    cp -a ../truepower/* ./
    git add -A
    git commit -m "fixed bbbb"
    
    cd ../truepower
    git checkout cccc
    cd ../onepower
    rm -rf ./*
    cp -a ../truepower/* ./
    git add -A
    git commit -m "fixed cccc"
    

    现在你可以删除旧的坏/真正的力量和使用新的/onepower git存储库。

    顺便说一句,在此之后,您将不会在文件结尾、字符串结尾和部分字符串开头出现空格更改的情况下使用此存储库。但当然,字符串中间的空白更改将被解释为更改。

    在以下帮助下找到解决方案: http://git-scm.com/book/en/Customizing-Git-Git-Configuration#Formatting-and-Whitespace

        2
  •  0
  •   Community Mohan Dere    9 年前

    将以下内容添加到 .gitconfig 至:

    1. Add only new files

      adduntracked=!git add $(git ls-files -o --exclude-standard)

    2. Add only non-whitespace changes :

      addnows = !git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"

    3. 两者一起:

      addnewnows=!git add $(git ls-files -o --exclude-standard) && git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"