简体   繁体   English

Xcode 项目文件 git 合并冲突

[英]Xcode Project file git merge conflict

In Xcode, what's the best way to avoid Git conflict in the project file?在Xcode中,工程文件中避免Git冲突的最佳方法是什么? (I am in manual git, not using Xcode interface for git) (我在manual git,不是用git的Xcode接口)

I've cloned mapbox-ios-sdk from Github, hacked it, and now the remote master has changed.我从 Github 克隆了 mapbox-ios-sdk,破解了它,现在远程主机已经改变了。 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) (具体来说,我指的是.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.我真的不认为应该忽略项目文件,因为如果项目文件有任何新文件,.pbxproj 文件似乎已更改。 (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. (或者我完全错了,这个文件应该被忽略?但显然它并没有在 mapbox-ios-sdk 中被忽略。毕竟人们需要项目文件。)但我也跑了在我与另一个合作者的合作项目之前就陷入了这场冲突,这让我无法完全使用 Git。

Should I figure out how to manually resolve conflict or is there a better way to deal with this?我应该弄清楚如何手动解决冲突还是有更好的方法来处理这个问题?

.pbxproj will change when you add new files to the project. .pbxproj 将在您向项目添加新文件时更改。 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:我们在我的项目中通过在添加新文件之前和之后执行以下步骤来避免这种情况:

  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在添加文件之前,提交您的更改,然后从 master 中提取,以便您拥有最新的。如果有人添加了文件,您现在拥有最新的 .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).立即提交并将您的更改推送到主服务器(希望在另一个协作者添加另一个文件之前)。

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?

A lot of websites simply suggest to use a .gitattributes file with:许多网站只是建议使用 .gitattributes 文件:

*.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.此外,如果其他人对此方法有意见,请包括在内。

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.pbxproj to JSON formatproject.pbxproj转换为 JSON 格式
  2. Iterate all objects in JSON and give every UUID an absolute path, and create a new UUID using MD5 hex digest of the path迭代 JSON 中的所有objects并给每个 UUID 一个绝对路径,并使用路径的 MD5 十六进制摘要创建一个新的 UUID
    • All elements in this json object is actually connected as a tree这个json对象中的所有元素实际上都是连接成一棵树
    • 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将 MD5 十六进制摘要应用于节点的路径
  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用 MD5 十六进制摘要替换所有旧的 UUID,并删除不在当前节点树中的未使用的 UUID 和格式错误的 UUID
  4. Sort the project file inlcuding children , files , PBXFileReference and PBXBuildFile list and remove all duplicated entries in these lists对包含childrenfilesPBXFileReferencePBXBuildFile列表的项目文件进行排序,并删除这些列表中的所有重复条目
    • see sort_pbxproj method in xUnique.py if you want to know the implementation;如果您想了解实现,请参阅sort_pbxproj方法;
    • It's ported from my modified sort-Xcode-project-file , with some differences in ordering PBXFileReference and PBXBuildFile它是从我修改后的sort-Xcode-project-file移植而来的,在PBXFileReferencePBXBuildFile排序上有一些差异
  5. With different options, you can use xUnique with more flexibility通过不同的选项,您可以更灵活地使用xUnique

Check README file for more details.检查README文件以获取更多详细信息。

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.但是如果你重命名你的项目的组,导致冲突,你将手动删除原来组的多余行。

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).提交(这会生成一个包含所有添加文件的 repo,但有些不在项目中)。
  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).在 Xcode 中,使用“Add Files To...”从其他协作者/分支添加丢失的文件(它们很容易找到,因为它们位于项目目录中,Xcode 会为您有效地突出显示它们)。
  4. Commit.犯罪。

(I am not convinced step 2 is really necessary - but feels neater to me). (我不相信第 2 步真的有必要——但对我来说感觉更整洁)。

You can also use Mergepbx manually do it as follows您也可以使用 Mergepbx 手动执行如下操作

  1. Install Mergepbx using brew.使用 brew 安装Mergepbx

    brew install mergepbx brew安装mergepbx

  2. Run a command each time you have conflicts in your project file to automatically resolve them.每次在项目文件中出现冲突时运行命令以自动解决它们。

    git mergetool --tool=mergepbx PROJECT.pbxproj git mergetool --tool=mergepbx PROJECT.pbxproj

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.我需要在我的分支中获取 master 代码所以,我从 master 那里拉取,并且我在我的 .pbxproj 文件中进行了更改,并且 master 具有不同的配置。所以,它在 .pbxproj 文件中显示冲突。 Follow these steps to resolve it请按照以下步骤解决它

Open .pbxproj in Finder -> Right click on file Choose Show Package Contents -> Choose .在 Finder 中打开.pbxproj ->右键单击文件选择Show Package Contents -> 选择 。 pbxproj file and open it with TextEdit- > It will show conflict lines as below. pbxproj 文件使用 TextEdit- >打开它,它将显示如下冲突行。

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

You have choose the right one from head Changes and your changes and Delete the rest.您已经从 head Changes 和您的更改中选择了正确的一个,然后删除其余的。 Now commit you code现在提交你的代码

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

If everything is fine the push you code.如果一切正常,则推送您的代码。

git push

If all else fails, while tedious.如果一切都失败了,虽然乏味。 you can open your pbxproj in your git client, then manually, go file by file and ensure each one is either removed or has 4 lines (6 occurrences).您可以在 git 客户端中打开 pbxproj,然后逐个文件手动打开 go 并确保每个文件都被删除或有 4 行(出现 6 次)。

I had the same problem, so I wrote a tool in Swift that will resolve conflicts due to adding, deleting, moving, and renaming files and groups.我也遇到了同样的问题,于是在Swift写了一个工具,可以解决文件和组的增删改名冲突。 You can set it up as a git merge driver so the script runs automatically when you merge a branch.您可以将其设置为 git 合并驱动程序,以便脚本在您合并分支时自动运行。

You can check it out here: XcodeMergeDriver你可以在这里查看: XcodeMergeDriver

I made an app for fixing things in Xcode project files when these sort of things go wrong.我制作了一个应用程序,用于在出现此类问题时修复 Xcode 项目文件中的内容。 You'd still have to do some manually merging first though, unfortunately.不幸的是,您仍然必须先进行一些手动合并。 https://itunes.apple.com/us/app/xsaviour/id1449886415?mt=12 https://itunes.apple.com/us/app/xsaviour/id1449886415?mt=12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM