Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git, how to set .gitattributes for Storyboards and .xib files

Background: git -- or any other SCM -- can "successfully" merge Storyboards, xib's, and pbxproj files as at their core they are just xml. Sometimes there are conflicts which can be resolved by the usual conflict resolution strategies. But xib's and Storyboards are serialized formats of some pretty complex data structures, so the fun begins.

Just as with other source code, a successful merge doesn't guarantee all the merged changes work correctly together. With the files in question, git can successfully merge but Xcode sometimes displays a "Could not read archive" error when you try to open it - i.e., the merge corrupted the xib.

I've seen suggestions about having git ignore .xibs (not practical), or in .gitattributes disable diff by setting

*.xib -crlf -diff

Technically, that fixes the merge conflict/corrupted xib problem - but now somebody's changes are lost?

Similarly, the best recommendation I've seen for the project file is merge=union in .gitattributes:

*.pbxproj merge=union

Question: I've searched quite a bit and there doesn't seem to be a good solution. Can people with experience tell me what happens in a few use cases?

  1. In the project I rename a file from x to y, and delete fileA. Meanwhile, another programmer committed changes to add fileC and delete fileD. What is the result of merge? I'm sure the actual file system changes will be correct, but in the Xcode navigator panel:

    • Will I see both x and Y?
    • Will fileA reappear because it's still in the other programmer's .pbxproj?
    • Will fileD still be there because it's still in my .pbxproj?
  2. If git is ignoring .xib files - will I at least get a warning that a file was changed and is not being handled?

like image 553
Kevin OMara Avatar asked Sep 11 '13 16:09

Kevin OMara


People also ask

Where to place Git attributes file?

These path-specific settings are called Git attributes and are set either in a . gitattributes file in one of your directories (normally the root of your project) or in the . git/info/attributes file if you don't want the attributes file committed with your project.

What is difference between Xib and storyboard?

A storyboard is like a canvas where you put all your . xib files. You no longer have any . xibs, you just have View Controllers directly on your canvas.

What is the purpose of gitattributes file?

gitattributes file allows you to specify the files and paths attributes that should be used by git when performing git actions, such as git commit , etc. In other words git automatically saves the file according to the attributes specified, every time a file is created or saved.


1 Answers

I wouldn't set any specific .gitattributes for project files or nibs these days.

Your problems merging concurrent changes to Xcode projects and xibs/storyboards exist in every team with more than one developer.

Xcode projects

A few tricks for dealing with Xcode project conflicts:

  • Take one side of the merge and manually apply the other changes after the merge. If you've deleted files they'll be in red, if you need to add them they'll be available to add.
  • When adding files to your targets they will go at the bottom of the 'Compile Sources' build phase. Avoid conflicts with other people by dragging them up this list to a random position.
  • Use .xcconfig files to configure your projects rather than the build settings section. Config files are trivial to merge.

My best advice if in doubt is to take one side of the merge, relax, and replay your additions.

XIBs and Storyboards

The format of XIBs (and therefor storyboards) has change significantly in recent months. I've found that additional objects are appended to the file's internal collections and merges can be done easily. If you're making changes to the same object, you may have to get in there manually.

This post from Itty Bitty Apps highlights the main changes:

The benefits of this new XIB XML format are many fold:

  • XIBs are much smaller (fewer lines of code).
  • A developer can easily read the XML and understand what it specifies in the user interface.
  • Merging XIBs is now much less likely to lead to broken XIBs. Before we would rarely even bother to try and merge a XIB or Storyboard.
like image 129
Jessedc Avatar answered Sep 26 '22 23:09

Jessedc