git git在提交之前删除新文件中的尾随空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19151940/
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
git remove trailing whitespace in new files before commit
提问by loop
I know removing trailing whitespace can be done with a pre-commit hook. I am interested in doing it manually. I read the question here:
Make git automatically remove trailing whitespace before committing - Stack Overflow
The answer closest to what I want is the "automatic version" from ntc2:
我知道可以使用预提交挂钩来删除尾随空格。我有兴趣手动完成。我在这里阅读了这个问题:
在提交之前让 git 自动删除尾随空格 - 堆栈溢出
最接近我想要的答案是来自 ntc2 的“自动版本”:
(export VISUAL=: && git -c apply.whitespace=fix add -ue .) && git checkout . && git reset
That command works well except it seems to be only for changes on files that are already in the repo, not new files. I have a bunch of files that are new, meaning they aren't yet in the repo. I want to remove whitespace from those files so I tried add -A instead of -u but that didn't make a difference.
该命令运行良好,但它似乎仅适用于 repo 中已有文件的更改,而不是新文件。我有一堆新文件,这意味着它们还没有在 repo 中。我想从这些文件中删除空格,所以我尝试添加 -A 而不是 -u 但这并没有什么区别。
回答by Luke Usherwood
To manually clean up whitespace from your last 3 commits, you can do this:
要手动清理最近 3 次提交中的空白,您可以执行以下操作:
git rebase --whitespace=fix HEAD~3
git rebase --whitespace=fix HEAD~3
When I work on a topic branch, I track the upstream branch (usually by creating it like this)
当我在一个主题分支上工作时,我会跟踪上游分支(通常是这样创建的)
git checkout -b topic -t
git checkout -b topic -t
Which allows me to drop the last argument from git rebase
. So once I'm done & ready to merge, I can clean the whole topic branch quickly with:
这允许我从git rebase
. 因此,一旦我完成并准备好合并,我就可以使用以下命令快速清理整个主题分支:
git ws
# aliased to rebase --whitespace=fix
git ws
# 别名为 rebase --whitespace=fix
Note that, unlike the HEAD~3 example, this will actually rebase your changes upon the upstream branch if it's changed! (But that's also what I want, in my workflow.)
请注意,与 HEAD~3 示例不同,如果上游分支发生更改,这实际上会将您的更改重新设定在上游分支上!(但这也是我想要的,在我的工作流程中。)
回答by jbyler
I like Luke's answer, except for the limitation that you need to either manually specify the base commit, or use a rebase-style workflow, where your history is linearized. I propose a modification that doesn't need an extra argument and doesn't change the topology of your commit graph. As a shell command:
我喜欢 Luke 的回答,除了您需要手动指定基本提交或使用 rebase 风格的工作流程的限制,其中您的历史是线性的。我提出了一个不需要额外参数的修改,也不会改变提交图的拓扑结构。作为 shell 命令:
git rebase --whitespace=fix --onto $(git merge-base HEAD @{u})
Or as a ~/.gitconfig alias:
或者作为 ~/.gitconfig 别名:
ws = "!git rebase --whitespace=fix --onto $(git merge-base HEAD @{u})"
I prefer this because sometimes I want to rebase my changes, but if I think think there might be a merge conflict I prefer to merge, so that both my original change and the conflict resolution will be recorded in the history. That way I can later second-guess the conflict resolution and redo it if necessary.
我更喜欢这个,因为有时我想重新调整我的更改,但如果我认为可能存在合并冲突,我更愿意合并,以便我的原始更改和冲突解决方案都将记录在历史记录中。这样我以后就可以对冲突解决方案进行二次猜测,并在必要时重做。
Given that I don't always rebase, I prefer not to mix whitespace-fixing with rebasing; hence this modification to Luke's answer.
鉴于我并不总是变基,我不喜欢将空格修复与变基混用;因此对卢克的答案进行了修改。
In addition, I enable the default pre-commit hook which aborts on whitespace errors:
此外,我启用了默认的 pre-commit 钩子,它会在出现空格错误时中止:
cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
This gives the following workflow, which I like because it's manual enough that I know what's going on but automated enough not to get in the way:
这提供了以下工作流程,我喜欢它,因为它足够手动,我知道发生了什么,但足够自动化,不会妨碍:
- hack hack hack, introduce whitespace error
- attempt to commit
- commit fails with whitespace error due to pre-commit hook
git commit --no-verify
to commit anywaygit ws
use the alias to fix
- hack hack hack,引入空格错误
- 尝试提交
- 由于预提交钩子,提交失败并出现空格错误
git commit --no-verify
无论如何都要提交git ws
使用别名修复
Note on the usage of --onto
: It's not necessary here, but I find it easier to reason about how the rebase works this way. In Luke's version, HEAD~3
is the <upstream>
in the man page, while in my version <upstream>
keeps its default value of the real upstream of the branch. You wind up with the same result either way though.
注意--onto
:的用法在这里不是必需的,但我发现更容易推理 rebase 如何以这种方式工作。在 Luke 的版本中,HEAD~3
是<upstream>
在手册页中,而在我的版本中,<upstream>
它的默认值是分支的真实上游。不管怎样,你最终都会得到相同的结果。
回答by ntc2
Simple fix
简单的修复
The command you quoted
你引用的命令
(export GIT_EDITOR=: && git -c apply.whitespace=fix add -ue .) && git checkout . && git reset
works if you first add the files you want to fix with git add -N <files you want to fix>
. The add -N
essentially tells Git to pretend you'd previously committed empty versions of the files.
如果您首先添加要修复的文件,则可以使用git add -N <files you want to fix>
. 在add -N
本质上是告诉GIT中假装你以前提交的文件的空版本。
Error you got
你得到的错误
I don't understand why you get fatal: Empty patch. Aborted.
error with add -Ae
, but it looks like a bug, since doing plain git add -A . && git diff --cached
shows that the patch should not actually be empty.
我不明白为什么你会收到fatal: Empty patch. Aborted.
错误add -Ae
,但它看起来像一个错误,因为简单git add -A . && git diff --cached
显示补丁实际上不应该是空的。
Better whitespace fixer
更好的空白固定器
I recently updated my answer that you linked towith a better Git alias for fixing whitespace. Here's a rewrite of that alias using Luke's rebase trickand a less redundant control flow:
我最近更新 了您链接到的答案,并使用了一个更好的 Git 别名来修复空格。这是使用Luke 的 rebase 技巧和较少冗余的控制流重写该别名 :
fixws =!"\
if (! git diff-index --quiet --cached HEAD); then \
\
git diff-files --quiet `git rev-parse --show-toplevel` ; \
export NEED_TO_STASH=$? ; \
\
git commit -m FIXWS_SAVE_INDEX && \
if [ 1 = $NEED_TO_STASH ] ; then git stash save FIXWS_SAVE_TREE; fi && \
git rebase --whitespace=fix HEAD~ && \
git reset --soft HEAD~ && \
if [ 1 = $NEED_TO_STASH ] ; then git stash pop; fi ; \
fi"
This fixes whitespace in the index, while preserving the index, and leaving the tree untouched. With this alias, you can fix unversioned files in the repo with
这修复了索引中的空白,同时保留了索引,并使树保持不变。使用此别名,您可以使用以下命令修复 repo 中的未版本控制文件
git add --all :/ && git fixws && git reset
But, it also handles the more common case of fixing up whitespace in a commit you're working on. It's complicated because it works even when the index or tree are clean.
但是,它还处理更常见的情况,即在您正在处理的提交中修复空格。这很复杂,因为即使索引或树是干净的,它也能工作。
回答by fjammes
If you use emacs, you can use "M^x delete-trailing-whitespace" to delete them before saving the file. (it can be customized in your .emacs also)
如果您使用 emacs,您可以在保存文件之前使用“M^x delete-trailing-whitespace”删除它们。(它也可以在您的 .emacs 中自定义)
vi seems to also allow this : http://vim.wikia.com/wiki/Remove_unwanted_spaces
vi 似乎也允许这样做:http: //vim.wikia.com/wiki/Remove_unwanted_spaces