xcode Xcode项目文件git合并冲突

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

Xcode Project file git merge conflict

xcodegit

提问by huggie

In Xcode, what's the best way to avoid Git conflict in the project file? (I am in manual git, not using Xcode interface for git)

在 Xcode 中,避免项目文件中的 Git 冲突的最佳方法是什么?(我在手动 git 中,没有使用 Xcode 接口进行 git)

I've cloned mapbox-ios-sdk from Github, hacked it, and now the remote master has changed. When I tried to pull the remote changes into my local, after merging there would be merge conflict in the project file. (Specifically, I mean the project.pbxproj in the .xcodeproj)

我已经从 Github 克隆了 mapbox-ios-sdk,对其进行了 hack,现在远程 master 已更改。当我尝试将远程更改拉入我的本地时,合并后项目文件中会出现合并冲突。(具体来说,我指的是 .xcodeproj 中的 project.pbxproj)

I do not really think project file should be put into the ignore, since if there are any new files the project file, the .pbxproj file seems to be changed. (Or am I just plain wrong and this file should be put to ignore? But obviously it wasn't on ignored in the mapbox-ios-sdk to begin with. People need the project file after all.) But I've also ran into this conflict before in my collaboration project with another collaborator and it's keeping me from using Git altogether.

我真的不认为应该将项目文件放入忽略中,因为如果项目文件中有任何新文件,.pbxproj 文件似乎已更改。(或者我完全错了,这个文件应该被忽略?但显然它并没有在 mapbox-ios-sdk 中被忽略。毕竟人们需要项目文件。)但我也跑了之前在我与另一个合作者的合作项目中遇到过这种冲突,这使我无法完全使用 Git。

Should I figure out how to manually resolve conflict or is there a better way to deal with this?

我应该弄清楚如何手动解决冲突还是​​有更好的方法来解决这个问题?

采纳答案by cmac

.pbxproj will change when you add new files to the project. There will be conflicts if two or more collaborators add files at the same time (without getting one another's changes first). We are avoiding this in my project by following these steps before and after adding new files:

.pbxproj 将在您向项目添加新文件时更改。如果两个或多个协作者同时添加文件(而不先获取彼此的更改),则会发生冲突。通过在添加新文件之前和之后执行以下步骤,我们在我的项目中避免了这种情况:

  1. Before adding a file, commit your changes, then pull from the master so that you have the latest.If someone has added a file, you now have the latest .pbxproj
  2. Add your file(s).
  3. Immediately commit and push your changes up to the master (hopefully, before another collaborator has added another file).
  1. 在添加文件之前,提交您的更改,然后从 master 中提取,以便您拥有最新的。如果有人添加了文件,您现在拥有最新的 .pbxproj
  2. 添加您的文件。
  3. 立即提交并将您的更改推送到主服务器(希望在另一个协作者添加另一个文件之前)。

It's wimpy, but we don't relish manually resolving .pbxproj conflicts.

它很弱,但我们不喜欢手动解决 .pbxproj 冲突。

Also, see this Stack Overflow question with excellent responses: How to use Git properly with XCode?

另外,请参阅此堆栈溢出问题,并给出了很好的回答:How to use Git correct with XCode?

回答by Warpzit

A lot of websites simply suggest to use a .gitattributes file with:

许多网站只是建议使用 .gitattributes 文件:

*.pbxproj merge=union

*.pbxproj merge=union

We're gonna try this approach before trying to use scripts. If we find any issues I'll be sure to update this post. Also if anyone else has comments to this approach please include.

在尝试使用脚本之前,我们将尝试这种方法。如果我们发现任何问题,我一定会更新这篇文章。此外,如果其他人对此方法有意见,请包括在内。

回答by Xiao

You should check my script xUnique, it is now the best solution to merge Xcode project file before Apple takes action on it.

您应该检查我的脚本xUnique,它现在是在 Apple 对其采取行动之前合并 Xcode 项目文件的最佳解决方案。

What it does & How it works

它的作用和工作原理

  1. convert project.pbxprojto JSON format
  2. Iterate all objectsin JSON and give every UUID an absolute path, and create a new UUID using MD5 hex digest of the path
    • All elements in this json object is actually connected as a tree
    • We give a path attribute to every node of the tree using its unique attribute; this path is the absolute path to the root node,
    • Apply MD5 hex digest to the path for the node
  3. Replace all old UUIDs with the MD5 hex digest and also remove unused UUIDs that are not in the current node tree and UUIDs in wrong format
  4. Sort the project file inlcuding children, files, PBXFileReferenceand PBXBuildFilelist and remove all duplicated entries in these lists
    • see sort_pbxprojmethod in xUnique.py if you want to know the implementation;
    • It's ported from my modified sort-Xcode-project-file, with some differences in ordering PBXFileReferenceand PBXBuildFile
  5. With different options, you can use xUniquewith more flexibility
  1. 转换project.pbxproj为 JSON 格式
  2. objects在 JSON 中迭代所有内容并为每个 UUID 提供一个绝对路径,并使用路径的 MD5 十六进制摘要创建一个新的 UUID
    • 这个json对象中的所有元素实际上都是连接成一棵树
    • 我们使用其唯一属性为树的每个节点赋予路径属性;这个路径是根节点的绝对路径,
    • 将 MD5 十六进制摘要应用于节点的路径
  3. 用 MD5 十六进制摘要替换所有旧的 UUID,并删除不在当前节点树中的未使用的 UUID 和格式错误的 UUID
  4. 排序项目文件计有childrenfilesPBXFileReferencePBXBuildFile列表,并删除这些列表中的所有重复的条目
    • sort_pbxproj如果您想了解实现,请参阅xUnique.py 中的方法;
    • 它是从我的修改版移植过来的sort-Xcode-project-file,在排序PBXFileReferencePBXBuildFile
  5. 通过不同的选项,您可以更灵活地使用xUnique

Check READMEfile for more details.

检查README文件以获取更多详细信息。

回答by summer1991

In most case, you can fix the merge by following code, remove the lines which git adds:

在大多数情况下,您可以通过以下代码修复合并,删除 git 添加的行:

#!/bin/bash
FILE={PRODUCT_NAME}.xcodeproj/project.pbxproj
sed '/======/d' $FILE | sed '/<<<<</d' | sed '/>>>>>/d' > temp
cat temp > $FILE
rm temp

but if you rename the group of your project and it leads to conflicts, you will manually delete the extra lines of your original group.

但是如果你重命名你的项目的组,导致冲突,你将手动删除原来组的多余行。

回答by Ali Beadle

When this is caused by adding files from two or more collaborators/branches (which in my experience has always been the case to date and can be checked by looking at the diff) I do this:

当这是由于添加来自两个或多个合作者/分支的文件(根据我的经验,迄今为止一直如此,可以通过查看差异来检查)引起的,我这样做:

  1. Resolve the conflict by "use ours" or "use theirs" (whichever has added the most files).
  2. Commit (this produces a repo that contains all the added files but some are not in the project).
  3. Within Xcode use "Add Files To..." to add the missing files from the other collaborator/branch (they are easy to find as they are in the project directory and Xcode usefully highlights them for you).
  4. Commit.
  1. 通过“使用我们的”或“使用他们的”(以添加最多的文件为准)解决冲突。
  2. 提交(这会生成一个包含所有添加文件的 repo,但有些不在项目中)。
  3. 在 Xcode 中,使用“Add Files To...”从其他协作者/分支添加丢失的文件(它们很容易找到,因为它们位于项目目录中,Xcode 会为您有效地突出显示它们)。
  4. 犯罪。

(I am not convinced step 2 is really necessary - but feels neater to me).

(我不相信第 2 步真的有必要——但对我来说感觉更整洁)。

回答by Vineeth Joseph

You can also use Mergepbx manually do it as follows

您也可以使用 Mergepbx 手动执行如下操作

  1. Install Mergepbxusing brew.

    brew install mergepbx

  2. Run a command each time you have conflicts in your project file to automatically resolve them.

    git mergetool --tool=mergepbx PROJECT.pbxproj

  1. 使用 brew安装Mergepbx

    brew安装mergepbx

  2. 每次在项目文件中出现冲突时运行命令以自动解决它们。

    git mergetool --tool=mergepbx PROJECT.pbxproj

回答by Gurjinder Singh

I need to get master code in my branch So, I was taking pull from master and I have changed in my .pbxproj file and master has different configuration.So, It was showing conflict in .pbxproj file. Follow these steps to resolve it

我需要在我的分支中获取 master 代码所以,我从 master 拉取,并且我在我的 .pbxproj 文件中进行了更改,并且 master 具有不同的配置。所以,它在 .pbxproj 文件中显示冲突。请按照以下步骤解决它

Open .pbxproj in Finder-> Right clickon file Choose Show Package Contents-> Choose .pbxproj fileand open it with TextEdit-> It will show conflict lines as below.

在 Finder 中打开.pbxproj-> 右键单击文件选择Show Package Contents-> 选择 。pbxproj 文件使用 TextEdit->打开它,它将显示如下冲突行。

<<<<<<< HEAD"
// head changes
===========
// Your changes
>>>>>>>>

You have choose the right one from head Changes and your changes and Delete the rest. Now commit you code

您已经从 head Changes 和您的更改中选择了正确的一个,然后删除其余的。现在提交你的代码

git add .
git commit -m"conflict resolved"
git status // Check you files status

If everything is fine the push you code.

如果一切正常,则推送您的代码。

git push

回答by Mark Bridges

I made an app for fixing things in Xcode project files when these sort of things go wrong. You'd still have to do some manually merging first though, unfortunately. https://itunes.apple.com/us/app/xsaviour/id1449886415?mt=12

我制作了一个应用程序,用于在出现此类问题时修复 Xcode 项目文件中的内容。不幸的是,您仍然必须先进行一些手动合并。 https://itunes.apple.com/us/app/xsaviour/id1449886415?mt=12