让 git 撤消任何仅空白的更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13793241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make git undo any whitespace-only changes?
提问by Sophistifunk
Is there an easy way to automatically do a git checkout on any file that only has whitespace changes? I'm dealing with both Windows and some code generation that's run from Eclipse, so I get newline changes and whitespace changes that are clogging up my workflow with noise, and making it difficult to track actual changes.
有没有一种简单的方法可以对任何只有空格更改的文件自动执行 git checkout?我正在处理 Windows 和一些从 Eclipse 运行的代码生成,因此我得到换行符更改和空格更改,这些更改会因噪音堵塞我的工作流程,并使跟踪实际更改变得困难。
Even just a nice way to reportwhich files have real changes and which don't would be a start, rather than having to do a diff -w for each one
甚至只是一种报告哪些文件有实际更改而哪些文件没有更改的好方法,而不必为每个文件做一个 diff -w
回答by Kent Munthe Caspersen
If your changes are not staged
如果您的更改未暂存
To stage changes that are not just whitespace changes, you can do:
要暂存不只是空白更改的更改,您可以执行以下操作:
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
Afterwards, to remove all unstaged changes (those changes that differ only in whitespace), you can do:
之后,要删除所有未暂存的更改(这些更改仅在空白处有所不同),您可以执行以下操作:
git checkout .
git checkout .
If your changes are staged
如果您的更改是暂存的
Unstage your changes by doing a git reset --mixed
and continue from the top of this answer. Note that mixed
is the default mode and can be omitted.
通过执行 a 取消暂存您的更改git reset --mixed
并从本答案的顶部继续。请注意,这mixed
是默认模式,可以省略。
回答by mipadi
This is most likely a line ending issue; Windows uses CRLF (carriage return + line feed, or \r\n
) line endings, whereas most other systems use LF (line feed, or \n
). Luckily, Git can normalize files such that they are alwaysstored as LF in the repo, but will be checked out as CRLF on Windows. You can do this by setting core.autocrlf
to true
on Windows:
这很可能是行尾问题;Windows 使用 CRLF(回车 + 换行,或\r\n
)换行符,而大多数其他系统使用 LF(换行,或\n
)。幸运的是,Git 可以规范化文件,以便它们在 repo中始终存储为 LF,但在 Windows 上将作为 CRLF 检出。您可以通过在 Windows 上设置core.autocrlf
为来执行此操作true
:
$ git config --global core.autocrlf true
and setting core.autocrlf
to input
on Mac and Linux:
并在 Mac 和 Linux 上设置core.autocrlf
为input
:
$ git config --global core.autocrlf input
More info is available here(scroll down to the section titled core.autocrlf).
回答by rumpel
If your changes are commited
如果您的更改已提交
(there's probably a smarter way but this works for me)
(可能有更聪明的方法,但这对我有用)
mybranch=master
git checkout -b tmp origin/master
# compute the non-ws diff to mybranch and apply it
git diff -U0 -w --no-color $mybranch | git apply -R --cached --ignore-whitespace --unidiff-zero -
git commit -m "non ws changes"
git reset --hard # discard all non-staged data
Your now on a new "tmp" branch. You may want to clean up (NOTE: this loses history of $mybranch
)
你现在在一个新的“tmp”分支上。您可能想要清理(注意:这会丢失 的历史记录$mybranch
)
git checkout $mybranch
git reset --hard tmp
git branch -D tmp
回答by Breakpoint25
Another approach is to use dos2unix command on a unix or (windows pc as a special install) to convert the line endings and commit and push
另一种方法是在 unix 或(windows pc 作为特殊安装)上使用 dos2unix 命令来转换行尾并提交和推送
回答by Motchouk
rumpel's answerwas the right fix for me. I would just like to complete a bit :
rumpel 的回答对我来说是正确的解决方法。我只想完成一点:
If your changes have been pushed
如果您的更改已被推送
Once you followed rumpel's procedure, do not push again on your branch. Github will ask you to pull before and you will get every whitespace diff again. Instead create a new branch from your current branch and push the new one :
一旦你遵循了 rumpel 的程序,就不要再推你的树枝了。Github 会要求您之前拉取,您将再次获得每个空白差异。而是从当前分支创建一个新分支并推送新分支:
# From $mybranch
git checkout -b $mybranch-v2
git push --set-upstream origin $mybranch-v2
Then on GitHub close and delete $mybranch
然后在 GitHub 上关闭并删除 $mybranch