【问题标题】:How to git-add only non-whitespace changes and new files?如何仅添加非空白更改和新文件?
【发布时间】:2023-03-16 04:22:01
【问题描述】:
  1. 我们可以对某些类型的忽略空格进行 diff:

    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 差异之前的空格)?

【问题讨论】:

  • 首先,您应该完全避免提交二进制文件。其次,您能否向我们提供您有时遇到的错误消息?
  • 为什么需要在本地进行您不想提交的空白更改?
  • 我想在我的 git 存储库树中看到真正的变化,但是我有太多的空白变化。
  • 对我来说更好的是添加 -p 以仅显示非空白更改

标签: git whitespace gitignore ignore removing-whitespace


【解决方案1】:

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

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

来源错误的存储库:

/home/user/truepower

新的好仓库:

/home/user/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"

现在您可以删除旧的坏 ../truepower 并使用新的 ../onepower git 存储库。

顺便说一句,在此之后,您将不会在此存储库中遇到文件末尾、字符串末尾和部分字符串开头的空格更改的问题。当然,字符串中间的空格变化会被解释为变化。

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

【讨论】:

  • 除了极其复杂之外,这似乎根本无法回答您的原始问题
  • @AndrewC 它回答了我的问题。它完全解决了我的问题。这个解决方案非常简单易行。如果你愿意,你甚至可以自动化这个过程。没人能给我这个答案。
  • 您询问如何限制git add 操作,并且您的“解决方案”涉及多个存储库和步骤,不仅发生在git add 之后,而且发生在git commit 之后。看起来它可以通过一个简单的过滤器分支操作,甚至一个 .gitattributes 清洁过滤器来更简单地完成。
  • @AndrewC 我问如何限制git add 操作以忽略一些空白更改,我们可以安全地忽略这些更改。答案是设置git config core.autocrlfgit config core.whitespace。如您所见,这在我的回答中。但是我们还需要首先修复损坏的(带有空格更改的提交)存储库。这也在我的回答中。如果修改某些提交,您将无法保存存储库提交历史记录。所以创建新的仓库是没有问题的。
  • @AndrewC 使用过滤器分支并非易事。我的答案是微不足道的。 filter-branch 不会修复 core.autocrlfcore.whitespace,因此您的建议是不正确的,因为在您的变体中,我们将来会再次遇到 git add 的问题。
【解决方案2】:

将以下内容添加到您的.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 "#"

【讨论】:

    猜你喜欢
    • 2011-03-31
    • 2021-09-28
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多