Tuesday, 3 April 2012

Resolving Git conflicts with Xcode

For the Xcode projects under git repository, they have a designed visual conflict solver: FileMerge. It is a very powerful text visor which shows and marks different versions of the file.

When you have any conflicts like this:

Auto-merging SampleProject.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in SampleProject.xcodeproj/project.pbxproj
Automatic merge failed; fix conflicts and then commit the result.

You might try:

1. Open the console
2. Type:
git mergetool

3. It will shows you all the candidates to use to see the conflicts.

merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse ecmerge p4merge araxis bc3 emerge vimdiff
Merging:
SampleProject.xcodeproj/project.pbxproj

Normal merge conflict for 'SampleProject.xcodeproj/project.pbxproj':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (opendiff): 

Press enter.
4. It will shows an error message:

xcode-select: Error: No Xcode folder is set. Run xcode-select -switch <xcode_folder_path> to set the path to the Xcode folder.

This is because it cannot find the place where the xcode is it. To set it, run the follow command in the console:

sudo xcode-select -switch /Applications/Xcode.app

Replace "/Applications/Xcode.app" by the place where the Xcode is installed in your compter.

5. Once the Xcode folder has been set, try to run the same command of step 2:

git mergetool

6. It will shows the same message of step 3. Press Enter. It will automatically opens an application called "FileMerge" with the content of the conflicted files.

On the left side, you should have the version of the repository, and on the left side, you should have your local changes.

For any change, it will mark it and shows you an arrow. The arrow indicates which change will be used in the final version. If you want to change it, you just have to select it and on the right corner,  click on "Actions" and select one of the follow options:
- Choose left
- Choose right
- Choose both (left first)
- Choose both (right first)
- Choose neither

7. Once you have finished solving the conflicts, press on File -> Save merge. It will automatically tells to git that the conflict has been solved. To check if there is any other conflicts to solve, just type the same command again:
git mergetool

If there is no more conflicts, it will shows:

merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse ecmerge p4merge araxis bc3 emerge vimdiff
No files need merging


Tip: The FileMerge program has been hidden in Mac OS. To run it, you should go to the folder where the Xcode is:

Xcode.app/Contents/Applications

The type:
open FileMerge.app

2 comments:

  1. Thanks very much for writing this up. It was very helpful and worked perfectly. Thanks! -Robert

    ReplyDelete