Git 合并和修复带有两个分支的混合空格和制表符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5256769/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:10:58  来源:igfitidea点击:

Git Merge and Fixing Mixed Spaces and Tabs with two Branches

gitmergebranchspacesgit-branch

提问by Bryan Ruiz

I've gone through some similar SOQ's and have not seen an adequate solution for this case.

我经历了一些类似的 SOQ,但没有看到针对这种情况的适当解决方案。

I've noticed that in many files there is a dirty mix of tabs and spaces used for indenting. The coding standard we follow uses 4 spaces for a tab currently.

我注意到在许多文件中,用于缩进的制表符和空格混杂在一起。我们遵循的编码标准目前使用 4 个空格作为制表符。

Although this should have been addressed when it happened, I need to consider it now and would like to fix the files I come across. The issue is that there are two teams using different branches of code and we will eventually have to merge those branches. What will happen if we change all the files of our branch to be the correct formatting and try to merge it? Will it end up being difficult to do so? Will it show me a ton of conflicts? Ideally id like git merge to ignore whitespace but I dont know how it would know which version to choose.

虽然这应该在发生时得到解决,但我现在需要考虑它并希望修复我遇到的文件。问题是有两个团队使用不同的代码分支,我们最终必须合并这些分支。如果我们将分支的所有文件更改为正确的格式并尝试合并它,会发生什么?这样做最终会很困难吗?它会向我展示大量的冲突吗?理想情况下,id 喜欢 git merge 来忽略空格,但我不知道它如何知道选择哪个版本。

Are there better solutions from a reactive point of a view?

从反应的角度来看,是否有更好的解决方案?

This is primarily a tech leadership, code lint, code review issue, yet I am not in that position or case currently. Can I fix this easily? (Having the offenders handle the merge is out of the question unfortunately!)

这主要是一个技术领导、代码 lint、代码问题,但我目前不在那个位置或案例中。我可以轻松解决这个问题吗?(不幸的是,让违规者处理合并是不可能的!)

回答by ghickman

By default git will see each difference in a line's indentation as a change, so yes you're likely to end up with mass conflicts doing a stock merge.

默认情况下,git 会将一行缩进中的每个差异视为更改,因此是的,您可能会在进行股票合并时遇到大量冲突。

However you can choose the merge strategy to use with the -soption:

但是,您可以选择与-s选项一起使用的合并策略:

git merge -s recursive -Xignore-space-change

This command would use the recursive strategy and uses it's ignore-space-changeoption. The git-merge docsexplain quite well how this will affect your merge:

此命令将使用递归策略并使用它的ignore-space-change选项。git-merge文档很好地解释了这将如何影响您的合并:

  • If their version only introduces whitespace changes to a line, our version is used;
  • If our version introduces whitespace changes but their version includes a substantial change, their version is used;
  • Otherwise, the merge proceeds in the usual way
  • 如果他们的版本只在一行中引入空格更改,则使用我们的版本;
  • 如果我们的版本引入了空白更改,但他们的版本包含实质性更改,则使用他们的版本;
  • 否则,合并以通常的方式进行

It would also be prudent to look at what git thinks has changed before doing the merge using diff with some extra options. Looking through the diff docsit looks like these options would help you out the most:

在使用 diff 和一些额外选项进行合并之前,查看 git 认为发生了什么变化也是谨慎的。查看diff 文档,这些选项似乎对您最有帮助:

-b
--ignore-space-change Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.

-w
--ignore-all-space Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.

-b
--ignore-space-change 忽略空白数量的变化。这会忽略行尾的空格,并认为一个或多个空格字符的所有其他序列是等效的。

-w
--ignore-all-space 比较行时忽略空格。即使一行有空格而另一行没有空格,这也会忽略差异。

回答by Makis

Why not run both code bases through indentwith a the same style on both branches? Doesn't take long.

为什么不在两个分支上通过缩进以相同的样式运行两个代码库?不需要很长时间。

Then you won't have any whitespace or other issues (like different block styles). Otherwise, yes, it will be hard to merge those branches.

那么您将不会有任何空格或其他问题(例如不同的块样式)。否则,是的,合并这些分支将很困难。

Assuming you code in C, obviously.

显然,假设您用 C 编写代码。

回答by RDL

Assuming you have at least three branches (say 'master', 'team1', 'team2') then update the 'master' branch with all of the correct spacing/indenting and have each team pull in the changes from 'master'. Each team should then ensure all of their new code/files meet your standard coding practice.

假设您至少有三个分支(例如“master”、“team1”、“team2”),然后使用所有正确的间距/缩进更新“master”分支,并让每个团队从“master”中提取更改。然后,每个团队都应确保他们所有的新代码/文件都符合您的标准编码实践。

回答by VonC

Note that, as mentioned in "Git: Merging without whitespace conflicts", using git merge -Xignore-space-changewill

请注意,如“ Git:没有空格冲突的合并”中所述,使用git merge -Xignore-space-changewill

  • ignore all whitespace changes in the file, not just whitespace where there are conflicts,
  • however, the resulting merged file get the whitespaces back,
  • using a pre-commithook in addition of that merge strategy can help remove completely those trailing whitespaces.
  • 忽略文件中的所有空白更改,而不仅仅是存在冲突的空白,
  • 然而,生成的合并文件将空格取回,
  • pre-commit在合并策略之外使用钩子可以帮助完全删除那些尾随空格。