如何正确使用Git与XCode? [英] How to use Git properly with XCode?

查看:91
本文介绍了如何正确使用Git与XCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直是iphone开发者,最近我在工作流程中加入了git。我使用了 http://shanesbrain.net/2008/上的git设置7/9 / using-xcode-with-git 为我的工作流程。



这些设置告诉git从合并中排除* .pbxproj?这是否有真正的理由?例如,当我向项目中添加一个文件并推送到源文件时,我的同伴开发人员在他们拉动时不会将该文件添加到他们的xcode项目中。然后,如果其中一个构建版本,则可能不包括此文件。我不应该让git处理项目文件的合并吗?有人能解释为什么或为什么不把这个文件合并,以及如何正确处理文件被添加到项目中的情况。谢谢。

解决方案

自SDK推出以来,我一直从事iPhone应用程序的全职工作,其中大部分时间花费在使用多个开发人员。



事实是,禁止合并.pbxproj文件的方式会更有害,而不是它的帮助。正如你所说,除非其他人获得该文件,否则当你添加一个文件时,他们还必须将其添加到他们的项目中 - 在任何大小的应用程序中,这很糟糕,并且它也会带来源代码控制的巨大好处,因为你不能仅仅通过git恢复到完整的早期项目状态。



.pbxproj文件只是一个属性列表(类似于XML)。从经验来看,如果两个人同时添加了文件,那么您所得到的唯一合并冲突就是如此。 99%的合并冲突情况下的解决方案是保持合并的两侧,这对git至少简单地涉及移除任何>>>>,<<<<&和====行。事实上,这是很常见的,我创建了一个简单的shell脚本来从git修复合并状态下的.pbxproj文件,我从项目目录(在类级别)运行它:

 #!/ bin / sh 

projectfile =`find -d。 -name'project.pbxproj'`
projectdir =`echo * .xcodeproj`
projectfile =$ {projectdir} /project.pbxproj
tempfile =$ {projectdir} / project。 pbxproj.out
savefile =$ {projectdir} /project.pbxproj.mergesave

cat $ projectfile | grep -v<<<<<<< HEAD> grep -v=======| grep -v^>>>>>>>> $ tempfile
cp $ projectfile $ savefile
mv $ tempfile $ projectfile

最差如果它失败(你要求XCode加载项目并且加载失败),你只需删除.pbxproj文件,从git中检出master,然后重新添加你的文件。但是我从未在这个脚本使用过多个月的时间里发生过这种情况,并且再次与其他几个开发人员一起全程处理iPhone应用程序。



另一个选项下面的评论),你可以尝试使用脚本代替,就是将这一行添加到.gitattributes文件中:

  * .pbxproj text -crlf -diff -merge = union 

然后git将总是将合并的两边对于.pbxproject文件来说,与我提供的脚本具有相同的效果,而不需要额外的工作。

最后,这是我完整的.gitignore文件,显示了我所做的设置为忽略,因为有一些你不想要的东西 - 在我的情况下,真的只是emacs残余和整个构建目录:

 #xcode noise 
build / *
* .pbxuser
* .mode1v3
*〜

#旧skool
。 svn

#osx noise
.DS_Store
profile


I have been an iphone developer for a while, and I have recently been including git in my workflow. I have used git settings found on http://shanesbrain.net/2008/7/9/using-xcode-with-git for my workflow so far.

Those settings tell git to exclude *.pbxproj from merges? Is there a real reason for doing this? For example, when I add a file to the project and push to origin, my fellow developers will not have that file added to their xcode project when they pull. Then if one of them builds a release this file may not be included. Shouldn't I just let git handle the merges for the project file? Could someone please explain why or why not this file should be in merges and how to properly handle the situation when files are added to the project. Thanks.

解决方案

I have worked on iPhone applications full time since the SDK launch, most of that time spent working on teams with multiple developers.

The truth is that it's way more harmful to disallow merging of that .pbxproj file than it is helpful. As you say, when you add a file unless other people get that file, they have to also add it to their project - in an application of any size, that sucks and it also takes away a huge benefit of source code control in that you cannot really revert to a complete earlier project state just through git.

The .pbxproj file is simply a property list (similar to XML). From experience, just about the ONLY merge conflict you were ever get is if two people have added files at the same time. The solution in 99% of the merge conflict cases is to keep both sides of the merge, which for git at least simply involves removing any >>>>, <<<<, and ==== lines. In fact this is so common that I have created a simple shell script to fix a .pbxproj file in a merge state from git, I run this from within the project directory (at the Classes level):

#!/bin/sh

    projectfile=`find -d . -name 'project.pbxproj'`
    projectdir=`echo *.xcodeproj`
    projectfile="${projectdir}/project.pbxproj"
    tempfile="${projectdir}/project.pbxproj.out"
    savefile="${projectdir}/project.pbxproj.mergesave"

    cat $projectfile | grep -v "<<<<<<< HEAD" | grep -v "=======" | grep -v "^>>>>>>> " > $tempfile
    cp $projectfile $savefile
    mv $tempfile $projectfile

Worst case if it fails (you ask XCode to load the project and it fails to load), you simply delete the .pbxproj file, check out the master from git, and re-add your files. But I've never had that happen in many months of use with this script, again working full time on iPhone applications with several other developers.

Another option (pointed out in comments below) that you can try using in place of the script, is to add this line to a .gitattributes file:

*.pbxproj text -crlf -diff -merge=union

Then git will always take both sides of a merge for the .pbxproject files, having the same effect as the script I provided only without any extra work.

Lastly, here is my complete .gitignore file, showing what I do have it set to ignore as there are a few things you don't want - in my case really just emacs remnants and the whole build directory:

# xcode noise
build/*
*.pbxuser
*.mode1v3
*~

# old skool
.svn

# osx noise
.DS_Store
profile

这篇关于如何正确使用Git与XCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆