如何在git中仅添加非空格更改和新文件?

6
我们可以使用一些忽略空格的方式来进行diff操作: 1) git diff --ignore-space-at-eol # 忽略行末空格变化。 2) git diff --ignore-space-change / git diff -b # 忽略空格数量变化。 3) git diff --ignore-all-space / git diff -w # 完全忽略空格。
在应用补丁时,我们可以使用以下命令来忽略空格: git apply --ignore-whitespacegit apply --ignore-space-change 但是如何从git add *中排除具有空格更改的文件呢?
以下解决方案对我无效:
 git diff -w --no-color | git apply --cached --ignore-whitespace

- 有时会出现错误并且无法将新文件添加到跟踪中。
2) [需要翻译的内容暂缺]
 git add `git diff -w --ignore-submodules |grep "^[+][+][+]" |cut -c7-`

- 它会写出错误信息,但不会执行任何操作(可能是因为我有二进制文件,不仅仅是文本文件)

P.S.:也许可以用上次提交的文件替换文件(即使最后一行或EOF之前的空格存在差异)?


首先,您应该尽量避免提交二进制文件。其次,您能否提供一下您有时候遇到的错误信息? - fpietka
为什么您需要在本地进行空格更改,而不想提交呢? - Matthew Strawbridge
我想在我的 Git 代码库中看到真正的变化,但是有太多的空格变更。 - user1742529
可能是Git add only non-whitespace changes的重复问题。 - cmbuckley
1
对我来说更好的是添加“-p”以仅显示非空格更改。 - elmarco
2个回答

0

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

解决方案是使用特殊设置重新创建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的帮助下找到的解决方案。


4
除了极其复杂,这似乎并没有回答你最初的问题。 - Andrew C
@AndrewC,这回答了我的问题。它完全解决了我的问题。这个解决方案非常简单易懂。如果你想的话,甚至可以自动化这个过程。没有人能够给我这个答案。 - user1742529
1
你问如何限制 git add 操作,而你的“解决方案”涉及多个仓库和步骤,不仅在 git add 之后还包括 git commit。看起来可以通过一个简单的 filter-branch 操作或者 .gitattributes 清理过滤器更简单地实现。 - Andrew C
@AndrewC我问了如何限制git add操作以忽略一些可以安全忽略的空格更改。答案是设置git config core.autocrlfgit config core.whitespace。正如您所看到的,它在我的答案中。但我们还需要先修复(使用空格更改提交)破损的存储库。这也在我的答案中。如果修改了某些提交,则无法保存存储库提交历史记录。因此,创建新存储库没有问题。 - user1742529
@AndrewC 使用filter-branch并不是一件简单的事情。我的答案解决方案很简单。filter-branch无法修复core.autocrlfcore.whitespace,因此你的建议是不正确的,因为在你的变体中我们将来仍然会遇到git add的问题。 - user1742529

0
将以下内容添加到您的.gitconfig文件中以:
  1. 仅添加新文件

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

  2. 仅添加非空白更改:

    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 "#"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接