多开发者项目的 Xcode + Git 最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12817011/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:45:42  来源:igfitidea点击:

Best practices for Xcode + Git for multi-developer projects

xcodegitversion-controlgithubbitbucket

提问by Luke

I can create a repo and use GitHub / BitBucket fine for my own projects. I have had problems when collaborating with other developers or trying to fork a project on GitHub.

我可以创建一个 repo 并在我自己的项目中使用 GitHub / BitBucket。我在与其他开发人员合作或尝试在 GitHub 上分叉项目时遇到了问题。

I am aware of other answers like Best practices for git repositories on open source projectsbut there are OSX / Xcode specific problems I want to know how to solve.

我知道其他答案,例如开源项目上 git 存储库的最佳实践,但我想知道如何解决 OSX/Xcode 特定问题。

  1. .DS_Storefiles can be a pain. You can use .gitignoreto prevent, but what happens if they have already been included, or another developer adds them back in through a clumsy git command?

  2. The .xcodeprojwill have changes to the directory names and developer profiles for the other person. What's the best way to do merges or to avoid conflicts?

  3. If I have forked or pulled from a github project, how can I clean up these issues and also minimise merge conflicts for the maintainer?

  1. .DS_Store文件可能会很痛苦。您可以使用.gitignore来阻止,但是如果它们已经被包含,或者其他开发人员通过笨拙的 git 命令将它们添加回会发生什么?

  2. .xcodeproj将不得不更改目录名称和开发商型材其他人。进行合并或避免冲突的最佳方法是什么?

  3. 如果我已经从 github 项目分叉或拉取,我该如何清理这些问题并最小化维护者的合并冲突?

If people have an example .gitignorecreated for Xcode, or scripts they use to initialise their repos then that would be great!

如果人们有一个为 Xcode 创建的示例.gitignore,或者他们用来初始化他们的存储库的脚本,那就太好了!

采纳答案by Eugene Sajine

I'll try one by one:

我会一一尝试:

I. You need to use git filter-branchonly if you need to remove the files from your history completely. If those files do not contain any credit card information, then i think the following should be enough:

I.git filter-branch仅当您需要从历史记录中完全删除文件时才需要使用。如果这些文件不包含任何信用卡信息,那么我认为以下内容就足够了:

git rm --cached .DS_Store
git commit -m "{Your message}" 

then add this file to .gitignoreand commit it.

然后将此文件添加到.gitignore并提交。

This will commit the removal of the file from the repository but will keep the file in working directory. If you push it though and then somebody else will pull this commit, they might have their file removed, so you MUST communicate this. By committing .gitignoreyou will prevent other developers from adding this file again. If you're not a maintainer, then i don't think you should do anything, but address this issue to the maintainer.

这将提交从存储库中删除文件,但会将文件保留在工作目录中。如果你推送它然后其他人会拉这个提交,他们可能会删除他们的文件,所以你必须传达这一点。通过提交,.gitignore您将阻止其他开发人员再次添加此文件。如果你不是维护者,那么我认为你不应该做任何事情,而是向维护者解决这个问题。

II. I'm a strong believer that hidden files of any nature are most of the time not supposed to be put into the repository exactly for that reason. Therefore i think that you should do the same thing with .xcodeprojas with .DS_Storeand put it into .gitignoreand commit it. .gitignoreis the exception for the rule above.

二、我坚信任何性质的隐藏文件在大多数情况下都不应该因为这个原因被放入存储库。因此,我认为您应该对.xcodeproj.DS_Store做同样的事情,并将其放入.gitignore并提交。.gitignore是上述规则的例外。

III. If those files are properly ignored , then there will be no issues in future with them. If they are already in the repo and somebody wants do such cleanup it should be done by maintainer and communicated inside the team.

三、如果这些文件被正确忽略,那么将来它们就不会有问题。如果他们已经在 repo 中并且有人想要进行此类清理,则应该由维护者完成并在团队内部进行沟通。

Hope that helps!

希望有帮助!

回答by rob mayoff

  1. Put .DS_Storein .gitignore. Then, if you haven't already, add .gitignoreto the repo. (You should not ignore .gitignore.) Now all developers will ignore .DS_Storefiles. If any were added to the repo erroneously before you put .DS_Storein .gitignore, you can now remove them (in a commit) and they should stay out.

  2. The xcodeprojis a directory. The only file in this directory that must be in the repository is the project.pbxprojfile. I generally ignore all of the others by putting these lines in my .gitignore:

    *.xcuserstate
    project.xcworkspace/
    xcuserdata/
    

    You should avoid putting absolute paths in your build settings. Use relative paths.

    Your Debug and Release builds should use iPhone Developeras the code signing identity, so that Xcode will automatically select the local developer's profile. When you want to create an IPA (for distribution), Xcode will offer to re-sign it with a different identity, at which point you can choose your distribution profile if you need to.

  3. If you're trying to use a project from github that has made these mistakes, you can try to get the maintainer to fix them, or you can make sure you don't touch the .DS_Storefiles and the code signing identities in the same commits that you want to send upstream.

  1. .DS_Store.gitignore。然后,如果您还没有,请添加.gitignore到 repo。(你不应该忽略.gitignore。)现在所有的开发者都会忽略.DS_Store文件。如果在您放入之前错误地将任何添加到 repo.DS_Store.gitignore,您现在可以删除它们(在提交中)并且它们应该留在外面。

  2. xcodeproj是一个目录。此目录中唯一必须位于存储库中的project.pbxproj文件是该文件。我通常通过将这些行放在我的中来忽略所有其他人.gitignore

    *.xcuserstate
    project.xcworkspace/
    xcuserdata/
    

    您应该避免在构建设置中放置绝对路径。使用相对路径。

    您的调试和发布版本应用iPhone Developer作代码签名标识,以便 Xcode 自动选择本地开发人员的配置文件。当您想创建 IPA(用于分发)时,Xcode 将提供使用不同身份对其重新签名,此时您可以根据需要选择您的分发配置文件。

  3. 如果您尝试使用 github 中犯了这些错误的项目,您可以尝试让维护者修复它们,或者您可以确保不要.DS_Store在相同的提交中接触文件和代码签名标识你想向上游发送。

回答by stemann

git filter-branch might help you to remove unwanted files (.DS_Store files) from your repository -- see e.g. https://help.github.com/articles/remove-sensitive-data

git filter-branch 可能会帮助您从存储库中删除不需要的文件(.DS_Store 文件)——参见例如https://help.github.com/articles/remove-sensitive-data

If a clumsy git commit has added files you should be able to replay the corrected changesets onto a clean repository.

如果笨拙的 git commit 添加了文件,您应该能够将更正的变更集重播到干净的存储库中。

回答by andrew-caulfield

For the 2nd issue regarding the .xcodeproj and merge conflicts.

关于 .xcodeproj 和合并冲突的第二个问题。

Using a .gitattributesfile to specify that merge conflicts for all .pbxprojfiles should be handled using the merge=unionstrategy, which should mean that Git knows to merge in the changes from both sides of the conflict, taking the upstream changes first.

使用.gitattributes文件指定所有.pbxproj文件的合并冲突应使用该merge=union策略处理,这意味着 Git 知道合并来自冲突双方的更改,首先处理上游更改。

This article explains it in a bit more depth

这篇文章更深入地解释了它

回答by Pudgeball

You're right in the sense that if a .DS_Store is already added the .gitignore won't be of much help however I think this is still a good resource for you and others.

您是对的,如果已经添加了 .DS_Store,则 .gitignore 不会有太大帮助,但我认为这对您和其他人来说仍然是一个很好的资源。

When I start a project, I normally look at this listto see if there is a good .gitignore already existing. More specifically for you, thisone is the Objective-C .gitignore.

当我开始一个项目时,我通常会查看这个列表,看看是否已经存在一个好的 .gitignore。更具体地说,这个是Objective-C .gitignore。

Hopefully those resources are of some use.

希望这些资源有些用处。

回答by Cameron Lowell Palmer

As a Mac user you should download a tool like SourceTreewhich supports Git Flow. Git Flow will help you establish some best practices around how your collaborators will commit code to the repo and at the very least make merge conflicts less frequent and more manageable. For a set of gitignore files for various project types you can go to GitHuband download one that is ready to go. For Xcode they have it listed as Objective-C.gitignore. That is a good starting place and it even covers Cocoapods. If you're using external libraries, your project should use CocoaPods so that you can isolate that code and keep it outside of your repo and avoid git submodules.

作为 Mac 用户,您应该下载像SourceTree这样支持Git Flow的工具。Git Flow 将帮助您围绕合作者如何将代码提交到存储库建立一些最佳实践,并至少使合并冲突不那么频繁且更易于管理。对于用于各种项目类型的一组 gitignore 文件,您可以转到GitHub并下载一个准备就绪的文件。对于 Xcode,他们将其列为Objective-C.gitignore。这是一个很好的起点,它甚至涵盖了Cocoapods。如果您使用外部库,您的项目应该使用 CocoaPods,以便您可以隔离该代码并将其保留在您的存储库之外并避免使用 git 子模块。

Now when you find a file has made it into your repo like .DS_Store just remove it, and move on. Make sure you add it to the .gitignore file that is checked into the project.

现在,当您发现一个文件已进入您的存储库时,例如 .DS_Store 只需将其删除,然后继续。确保将其添加到已签入项目的 .gitignore 文件中。

As for xcodeproj... there shouldn't be that much customization within the file that is user specific since the above mentioned gitignore filters that out. If a scheme is to be shared make sure you check shared under Manage Schemes and you will check in files in that subdirectory. You should be using automatic selection of certificates so the only real choice is Developer or Distribution. You should also take advantage of variables provided within Xcodethat avoid hardcoding complete paths. When trying to think of an example Plists came to mind, in this case, you might have written /Users/me/MyProject/Resources/MyProject.plist, but instead should use $(SRCROOT)/resources/MyProject.plist.

至于 xcodeproj... 不应该在用户特定的文件中进行太多自定义,因为上面提到的 gitignore 将其过滤掉了。如果要共享方案,请确保在“管理方案”下选中“共享”,然后您将签入该子目录中的文件。您应该使用自动选择证书,因此唯一真正的选择是开发人员或分发。您还应该利用Xcode提供变量来避免对完整路径进行硬编码。当试图想到一个 Plists 的例子时,在这种情况下,你可能已经写了/Users/me/MyProject/Resources/MyProject.plist,但应该使用$(SRCROOT)/resources/MyProject.plist.