Git 合并并修复具有两个分支的混合空间和选项卡

发布于 2024-10-21 18:51:16 字数 398 浏览 4 评论 0原文

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

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

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

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

这主要是一个技术领导力、代码检查、代码审查问题,但我目前不处于那个位置或情况。我可以轻松解决这个问题吗? (不幸的是,让违法者处理合并是不可能的!)

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

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.

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.

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!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城旧梦 2024-10-28 18:51:16

默认情况下,git 会将行缩进中的每个差异视为更改,因此,是的,在进行库存合并时,您可能最终会出现大规模冲突。

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

git merge -s recursive -Xignore-space-change

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

  • 如果他们的版本仅对行引入空格更改,则使用我们的版本;
  • 如果我们的版本引入了空格更改,但他们的版本包含重大更改,则使用他们的版本;
  • 否则,合并将按通常方式进行

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

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

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

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.

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

git merge -s recursive -Xignore-space-change

This command would use the recursive strategy and uses it's ignore-space-change option. The git-merge docs explain quite well how this will affect your 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 docs it looks like these options would help you out the most:

-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.

喵星人汪星人 2024-10-28 18:51:16

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

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

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

Why not run both code bases through indent with 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.

森林迷了鹿 2024-10-28 18:51:16

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

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.

心凉 2024-10-28 18:51:16

请注意,如“Git:合并时没有空格冲突”中提到的,使用 git merge -Xignore-space- Change

  • 忽略文件中的所有空白更改,而不仅仅是存在冲突的空白,
  • 恢复空白
  • 但是,生成的合并文件会使用 pre-commit 钩子 合并策略可以帮助完全删除那些尾随空格。

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

  • 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-commit hook in addition of that merge strategy can help remove completely those trailing whitespaces.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文