如何在XCode中正确使用Git ?

时间:2022-07-15 20:59:43

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.

我已经做了一段时间的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? 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.

这些设置告诉git排除*。从合并pbxproj吗?这样做有真正的理由吗?例如,当我向项目添加一个文件并将其推送到origin时,我的开发伙伴在拖拽时不会将该文件添加到他们的xcode项目中。然后,如果其中一个构建了一个版本,这个文件可能不包括在内。难道我不应该让git来处理项目文件的合并吗?有人能解释一下为什么或者为什么这个文件不应该被合并,以及当文件被添加到项目中时如何正确地处理这种情况吗?谢谢。

5 个解决方案

#1


126  

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,至少只需删除任何>>>>,<<<,==== = lines。事实上,这是非常常见的,我已经创建了一个简单的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

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文件,从git签出master,然后重新添加文件。但在使用这个脚本的几个月里,我从来没有遇到过这种情况,再次与其他几个开发人员一起在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

#2


6  

Frankly, the existing answers are misleading.

坦率地说,现有的答案具有误导性。

If you never delete 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 problems under 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)由于任何原因,如果您需要修改文件结构并在项目中产生冲突。pbxproj,使用您最喜欢的文本编辑器手动解决它们。当你把你的任务做得很小的时候,冲突很容易解决。

#3


5  

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

#4


3  

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的两个修改版本。对于git来说,最好将其视为二进制文件。

See here for details: Git and pbxproj

详情请参见这里:Git和pbxproj

Update: Even though the git book still agrees with this answer, I no longer do. I version control my .pbxproj just like any other non-binary source file.

更新:即使git簿仍然同意这个答案,我也不再这么做了。我的版本控制我的。pbxproj就像任何其他非二进制源文件一样。

#5


2  

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

您将不得不将它安装为一个合并驱动程序,因此当您的项目文件(自述文件)中出现合并冲突时,将自动调用它。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要好得多,因为mergepbx理解项目文件的语义,因此将正确地解决冲突。

However the project is still alpha, don't expect it to understand every project file that is out there.

但是,项目仍然是alpha,不要期望它能够理解所有的项目文件。

#1


126  

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,至少只需删除任何>>>>,<<<,==== = lines。事实上,这是非常常见的,我已经创建了一个简单的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

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文件,从git签出master,然后重新添加文件。但在使用这个脚本的几个月里,我从来没有遇到过这种情况,再次与其他几个开发人员一起在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

#2


6  

Frankly, the existing answers are misleading.

坦率地说,现有的答案具有误导性。

If you never delete 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 problems under 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)由于任何原因,如果您需要修改文件结构并在项目中产生冲突。pbxproj,使用您最喜欢的文本编辑器手动解决它们。当你把你的任务做得很小的时候,冲突很容易解决。

#3


5  

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

#4


3  

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的两个修改版本。对于git来说,最好将其视为二进制文件。

See here for details: Git and pbxproj

详情请参见这里:Git和pbxproj

Update: Even though the git book still agrees with this answer, I no longer do. I version control my .pbxproj just like any other non-binary source file.

更新:即使git簿仍然同意这个答案,我也不再这么做了。我的版本控制我的。pbxproj就像任何其他非二进制源文件一样。

#5


2  

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

您将不得不将它安装为一个合并驱动程序,因此当您的项目文件(自述文件)中出现合并冲突时,将自动调用它。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要好得多,因为mergepbx理解项目文件的语义,因此将正确地解决冲突。

However the project is still alpha, don't expect it to understand every project file that is out there.

但是,项目仍然是alpha,不要期望它能够理解所有的项目文件。