ios 如何在 Xcode 中正确使用 Git?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2615378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Git properly with Xcode?
提问by rickharrison
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-gitfor my workflow so far.
我做 iphone 开发人员已有一段时间了,最近我在我的工作流程中加入了 git。到目前为止,我已经将http://shanesbrain.net/2008/7/9/using-xcode-with-git上的 git 设置用于我的工作流程。
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? Why or why not this file should be in merges and how to properly handle the situation when files are added to the project?
这些设置告诉 git 从合并中排除 *.pbxproj?这样做有真正的理由吗?例如,当我将文件添加到项目并推送到原点时,我的开发人员在拉取时不会将该文件添加到他们的 xcode 项目中。然后,如果其中一个构建版本,则可能不包含此文件。我不应该让 git 处理项目文件的合并吗?为什么或为什么不应该合并这个文件,以及如何正确处理将文件添加到项目中的情况?
回答by Kendall Helmstetter Gelner
I have worked on iPhone applications full time since the SDK launch, most of that time spent working on teams with multiple developers.
自 SDK 发布以来,我一直在全职开发 iPhone 应用程序,大部分时间都花在与多个开发人员的团队工作上。
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.
事实是,禁止合并该 .pbxproj 文件比它有用更有害。正如您所说,除非其他人获得该文件,否则当您添加文件时,他们也必须将其添加到他们的项目中 - 在任何大小的应用程序中,这都很糟糕,而且它还剥夺了源代码控制的巨大好处,因为您仅通过 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):
.pbxproj 文件只是一个属性列表(类似于 XML)。根据经验,您遇到的唯一合并冲突是两个人同时添加了文件。99% 的合并冲突情况的解决方案是保留合并的双方,对于 git 来说,这至少只涉及删除任何 >>>>、<<<< 和 ==== 行。事实上,这很常见,我已经创建了一个简单的 shell 脚本来修复 .pbxproj 文件在来自 git 的合并状态,我从项目目录中(在类级别)运行它:
#!/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.
如果失败,最坏情况(你问的XCode加载项目,它无法加载),您只需删除.pbxproj文件,从混帐退房的主人,并重新添加文件。但是我在使用这个脚本的几个月里从来没有发生过这种情况,再次与其他几个开发人员全职开发 iPhone 应用程序。
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:
您可以尝试代替脚本使用的另一个选项(在下面的评论中指出)是将此行添加到 .gitattributes 文件中:
*.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.
然后 git 将始终对 .pbxproject 文件进行合并,与我提供的脚本具有相同的效果,无需任何额外工作。
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:
最后,这是我完整的 .gitignore 文件,显示了我将它设置为忽略的内容,因为有一些您不想要的东西 - 在我的情况下,实际上只是 emacs 残余和整个构建目录:
# xcode noise
build/*
*.pbxuser
*.mode1v3
*~
# old skool
.svn
# osx noise
.DS_Store
profile
回答by oneyenjug
This works for me in Xcode 4.6 and Git 1.7.5.
这在Xcode 4.6和Git 1.7.5对我的作品。
Add and commit .gitattributes file with this:
添加并提交 .gitattributes 文件:
*.pbxproj binary merge=union
I've tested this with another team member and works great.
我已经与另一位团队成员进行了测试,效果很好。
Taken from: http://robots.thoughtbot.com/post/33796217972/xcode-and-git-bridging-the-gap
取自:http: //robots.thoughtbot.com/post/33796217972/xcode-and-git-bridging-the-gap
回答by Brian
Frankly, the existing answers are misleading.
坦率地说,现有的答案具有误导性。
If you neverdelete or rename files, then using the merge=union
strategy, which just combines the differences in different commits directly, is a good idea.
如果您从不删除或重命名文件,那么使用merge=union
直接结合不同提交中的差异的策略是一个好主意。
However, in the real world, we do need to delete or rename files sometimes. Merging the differences without any modification would make a lot of problemsunder these situations, and these problems usually lead to the "Workspace Integrity - Couldn't load project" issue, which makes you even not able to run the project.
然而,在现实世界中,我们有时确实需要删除或重命名文件。在这些情况下,不做任何修改地合并差异会产生很多问题,这些问题通常会导致“工作区完整性 - 无法加载项目”问题,甚至无法运行项目。
The best solution I got so far:
到目前为止我得到的最佳解决方案:
1) Design the project well and add all the needed files at the beginning, so you would seldom need to change the project.pbxproj
.
1)设计好项目,一开始就添加所有需要的文件,所以你很少需要更改project.pbxproj
.
2) Make your features tiny. Don't do too many things in a branch.
2)让你的特征变小。不要在一个分支中做太多事情。
3) For any reason, if you need to modify the file structure and get conflicts in project.pbxproj
, use your favorite text editor to solve them manually. As you make your tasks tiny, the conflicts might be easy to solve.
3)无论出于何种原因,如果您需要修改文件结构并在 中遇到冲突project.pbxproj
,请使用您喜欢的文本编辑器手动解决它们。当你让你的任务变得很小时,冲突可能很容易解决。
回答by Tom
The short answer is that even if you don't include that line in .gitattributes
, you may not be able to easily merge two modified versions of a .pbxproj. It's better for git to treat it as a binary.
简短的回答是,即使您没有在.gitattributes
.pbxproj 中包含该行,您也可能无法轻松合并 .pbxproj 的两个修改版本。git 最好将其视为二进制文件。
See here for details: Git and pbxproj
有关详细信息,请参见此处:Git 和 pbxproj
Update:Even though the git book still agreeswith this answer, I no longer do. I version control my .pbxproj
just like any other non-binary source file.
回答by Simon
I did create a Python script that can handle merge conflicts in XCode Project files.
我确实创建了一个 Python 脚本,可以处理 XCode 项目文件中的合并冲突。
If you want to try it, you can check it out here: https://github.com/simonwagner/mergepbx
如果你想尝试,你可以在这里查看:https: //github.com/simonwagner/mergepbx
You will have to install it as a merge driver, so it gets called automatically when you have a merge conflict in your project file (the README.md will tell you how to do that).
您必须将其安装为合并驱动程序,因此当您的项目文件中存在合并冲突时,它会自动调用(README.md 将告诉您如何执行此操作)。
It should work much better than using merge=union
as mergepbx
understands the semantics of your project file and therefore will resolve the conflict correctly.
它应该比使用merge=union
asmergepbx
理解项目文件的语义要好得多,因此可以正确解决冲突。
However the project is still alpha, don't expect it to understand every project file that is out there.
然而,该项目仍处于 alpha 阶段,不要指望它能够理解现有的每个项目文件。