简体   繁体   中英

Xcode Project file git merge conflict

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)

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)

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.

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

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

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

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

What it does & How it works

  1. convert project.pbxproj to JSON format
  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
    • 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 , PBXFileReference and PBXBuildFile list and remove all duplicated entries in these lists
    • see sort_pbxproj method 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 PBXFileReference and PBXBuildFile
  5. With different options, you can use xUnique with more flexibility

Check README file for more details.

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

#!/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).
  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.

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

You can also use Mergepbx manually do it as follows

  1. Install Mergepbx using 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

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

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

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

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

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. You can set it up as a git merge driver so the script runs automatically when you merge a branch.

You can check it out here: XcodeMergeDriver

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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