Xcode项目的Git忽略文件

958

当我在使用 XcodeGit 结合时,应该包括哪些文件在 .gitignore 中?


19
请访问 https://www.gitignore.io/api/swift,xcode,Cobjective-c,osx。该网页提供了相关的忽略文件,可用于在Git中排除Swift、Xcode、C Objective-C和OSX等方面的文件。 - geekay
2
以上链接产生了一个错误:https://www.gitignore.io/api/swift,xcode,objective-c,osx - the Reverend
21个回答

730

我之前使用的是最受欢迎的答案,但需要进行一些清理,因此在这里重新为Xcode 4进行了改进。

我已经研究了列表中的每个文件,但其中有几个在苹果官方的Xcode文档中不存在,所以我不得不去苹果邮件列表上寻找信息。

苹果继续添加未记录的文件,可能会破坏我们的实时项目。在我看来,这是不可接受的,现在我已经开始记录每次他们这样做时的错误。我知道他们不在乎,但也许这会让其中一个人感到羞愧,从而更公平地对待开发人员。


如果您需要自定义,这里有一个代码片段可以fork:https://gist.github.com/3786883


#########################
# .gitignore file for Xcode4 and Xcode5 Source projects
#
# Apple bugs, waiting for Apple to fix/respond:
#
#    15564624 - what does the xccheckout file in Xcode5 do? Where's the documentation?
#
# Version 2.6
# For latest version, see: https://dev59.com/3nVD5IYBdhLWcg3wNY1Z
#
# 2015 updates:
# - Fixed typo in "xccheckout" line - thanks to @lyck for pointing it out!
# - Fixed the .idea optional ignore. Thanks to @hashier for pointing this out
# - Finally added "xccheckout" to the ignore. Apple still refuses to answer support requests about this, but in practice it seems you should ignore it.
# - minor tweaks from Jona and Coeur (slightly more precise xc* filtering/names)
# 2014 updates:
# - appended non-standard items DISABLED by default (uncomment if you use those tools)
# - removed the edit that an SO.com moderator made without bothering to ask me
# - researched CocoaPods .lock more carefully, thanks to Gokhan Celiker
# 2013 updates:
# - fixed the broken "save personal Schemes"
# - added line-by-line explanations for EVERYTHING (some were missing)
#
# NB: if you are storing "built" products, this WILL NOT WORK,
# and you should use a different .gitignore (or none at all)
# This file is for SOURCE projects, where there are many extra
# files that we want to exclude
#
#########################

#####
# OS X temporary files that should never be committed
#
# c.f. http://www.westwind.com/reference/os-x/invisibles.html

.DS_Store

# c.f. http://www.westwind.com/reference/os-x/invisibles.html

.Trashes

# c.f. http://www.westwind.com/reference/os-x/invisibles.html

*.swp

#
# *.lock - this is used and abused by many editors for many different things.
#    For the main ones I use (e.g. Eclipse), it should be excluded
#    from source-control, but YMMV.
#   (lock files are usually local-only file-synchronization on the local FS that should NOT go in git)
# c.f. the "OPTIONAL" section at bottom though, for tool-specific variations!
#
# In particular, if you're using CocoaPods, you'll want to comment-out this line:
*.lock


#
# profile - REMOVED temporarily (on double-checking, I can't find it in OS X docs?)
#profile


####
# Xcode temporary files that should never be committed
# 
# NB: NIB/XIB files still exist even on Storyboard projects, so we want this...

*~.nib


####
# Xcode build files -
#
# NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "DerivedData"

DerivedData/

# NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "build"

build/


#####
# Xcode private settings (window sizes, bookmarks, breakpoints, custom executables, smart groups)
#
# This is complicated:
#
# SOMETIMES you need to put this file in version control.
# Apple designed it poorly - if you use "custom executables", they are
#  saved in this file.
# 99% of projects do NOT use those, so they do NOT want to version control this file.
#  ..but if you're in the 1%, comment out the line "*.pbxuser"

# .pbxuser: http://lists.apple.com/archives/xcode-users/2004/Jan/msg00193.html

*.pbxuser

# .mode1v3: http://lists.apple.com/archives/xcode-users/2007/Oct/msg00465.html

*.mode1v3

# .mode2v3: http://lists.apple.com/archives/xcode-users/2007/Oct/msg00465.html

*.mode2v3

# .perspectivev3: http://stackoverflow.com/questions/5223297/xcode-projects-what-is-a-perspectivev3-file

*.perspectivev3

#    NB: also, whitelist the default ones, some projects need to use these
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3


####
# Xcode 4 - semi-personal settings
#
# Apple Shared data that Apple put in the wrong folder
# c.f. https://dev59.com/NGMl5IYBdhLWcg3wgnSl#19260712
#     FROM ANSWER: Apple says "don't ignore it"
#     FROM COMMENTS: Apple is wrong; Apple code is too buggy to trust; there are no known negative side-effects to ignoring Apple's unofficial advice and instead doing the thing that actively fixes bugs in Xcode
# Up to you, but ... current advice: ignore it.
*.xccheckout

#
#
# OPTION 1: ---------------------------------
#     throw away ALL personal settings (including custom schemes!
#     - unless they are "shared")
# As per build/ and DerivedData/, this ought to have a trailing slash
#
# NB: this is exclusive with OPTION 2 below
xcuserdata/

# OPTION 2: ---------------------------------
#     get rid of ALL personal settings, but KEEP SOME OF THEM
#     - NB: you must manually uncomment the bits you want to keep
#
# NB: this *requires* git v1.8.2 or above; you may need to upgrade to latest OS X,
#    or manually install git over the top of the OS X version
# NB: this is exclusive with OPTION 1 above
#
#xcuserdata/**/*

#     (requires option 2 above): Personal Schemes
#
#!xcuserdata/**/xcschemes/*

####
# Xcode 4 workspaces - more detailed
#
# Workspaces are important! They are a core feature of Xcode - don't exclude them :)
#
# Workspace layout is quite spammy. For reference:
#
# /(root)/
#   /(project-name).xcodeproj/
#     project.pbxproj
#     /project.xcworkspace/
#       contents.xcworkspacedata
#       /xcuserdata/
#         /(your name)/xcuserdatad/
#           UserInterfaceState.xcuserstate
#     /xcshareddata/
#       /xcschemes/
#         (shared scheme name).xcscheme
#     /xcuserdata/
#       /(your name)/xcuserdatad/
#         (private scheme).xcscheme
#         xcschememanagement.plist
#
#

####
# Xcode 4 - Deprecated classes
#
# Allegedly, if you manually "deprecate" your classes, they get moved here.
#
# We're using source-control, so this is a "feature" that we do not want!

*.moved-aside

####
# OPTIONAL: Some well-known tools that people use side-by-side with Xcode / iOS development
#
# NB: I'd rather not include these here, but gitignore's design is weak and doesn't allow
#     modular gitignore: you have to put EVERYTHING in one file.
#
# COCOAPODS:
#
# c.f. http://guides.cocoapods.org/using/using-cocoapods.html#what-is-a-podfilelock
# c.f. http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
#!Podfile.lock
#
# RUBY:
#
# c.f. http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/
#
#!Gemfile.lock
#
# IDEA:
#
# c.f. https://www.jetbrains.com/objc/help/managing-projects-under-version-control.html?search=workspace.xml
# 
#.idea/workspace.xml
#
# TEXTMATE:
#
# -- UNVERIFIED: c.f. https://dev59.com/3nVD5IYBdhLWcg3wNY1Z#50283
#
#tm_build_errors

####
# UNKNOWN: recommended by others, but I can't discover what these files are
#

76
不应忽略 *.lockPodfile.lock(不要在意冗余)。您希望在所有工作区安装完全相同的版本,而不是“最新版本”。 - tvon
10
我已经删除了 Podfile 部分。我最初没有添加它,StackOverflow上说是别人添加的,我疏忽地将其复制粘贴到了gist中。对于任何混淆和误解,我表示歉意。我真的不喜欢 StackOverflow 允许任何人编辑您的答案的方式 :(. - Adam
6
现在每条内容都有一个解释行,这将使其更加清晰,并使您更容易地对其进行自定义。 - Adam
3
据我所见,这个文件包含版本控制系统元数据,因此不应该被提交到版本控制系统中。 不,developer.apple.com 上没有提到过 xccheckout。 但是在官方的 Github 页面上,这个文件已经包含在 gitignore 文件中了。https://github.com/github/gitignore/blob/master/Objective-C.gitignore - skywinder
4
没有官方的说明,我们不会忽略任何事情。几乎每次人们这样做,迟早会破坏某个项目。这太危险了——不要冒这个险。因此,我拒绝在这个文件中添加任何内容,除非我有一个可验证的来源证实可以安全地忽略它! - Adam
显示剩余47条评论

276
根据这份Mercurial指南,我的.gitignore包括以下内容:
.DS_Store
*.swp
*~.nib

build/

*.pbxuser
*.perspective
*.perspectivev3

我还选择了包括:

*.mode1v3
*.mode2v3

根据这篇苹果邮件列表文章,所说的是“用户特定的项目设置”。
而对于Xcode 4:
xcuserdata

53
我不特别喜欢 .pbxuser/.perspective/*.perspectivev3 这些模式,更喜欢以下这种形式:.xcodeproj/ !*.xcodeproj/project.pbxproj这会忽略 *.xcodeproj 内的所有内容,除了 project.pbxproj。 - Lily Ballard
6
我不会忽视 .pbxuser,.perspective 和 *.perspectivev3 文件,因为我想在克隆我的代码库时保留这些设置。 - lajos
8
你可能需要补充说明,可以通过以下方式创建一个“全局”的 gitignore 文件:git config --global core.excludesfile ~/.gitignore - Jess Bowers
65
我想提醒那些在提交项目后添加.gitignore文件的人:你忽略的文件仍然被跟踪。你需要使用 git rm --cached <files> 命令从Git中手动删除它们。请注意不要改变原文意思,使翻译更加易懂通俗。 - pixelfreak
22
@SpacyRicochet:自从我写下这条评论以来,评论格式显然已经发生了变化。因此出现了斜体字。我的模式应该看起来像*.xcodeproj/* !*.xcodeproj/project.pbxproj。当然,现在你需要根据工作区进行调整。 - Lily Ballard
显示剩余9条评论

71
关于“build”目录排除问题 -
如果您像我一样将构建文件放在与源代码不同的目录中,则无需担心树中的该文件夹。
这也使得共享代码、防止臃肿的备份甚至是当您依赖其他Xcode项目(需要将构建文件放在彼此相同的目录中)时更加简单。
您可以从Github gist https://gist.github.com/708713 中获取最新版本的副本。
我的当前 .gitignore 文件是:
# Mac OS X
*.DS_Store

# Xcode
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
*.xcuserstate
project.xcworkspace/
xcuserdata/

# Generated files
*.o
*.pyc


#Python modules
MANIFEST
dist/
build/

# Backup files
*~.nib

8
我有一个构建文件夹,它在项目文件夹之外。但是当其他用户构建项目时,默认情况下会在项目内重新创建这个文件夹,所以我发现将它添加到忽略文件中是更好的解决方案,否则它会在他们的提交中被重新添加。 - lajos

59

对于Xcode 4,我也添加了以下内容:

YourProjectName.xcodeproj/xcuserdata/*
YourProjectName.xcodeproj/project.xcworkspace/xcuserdata/*

80
如果你只是添加 xcuserdata,那么这两个问题都会得到解决。 - ma11hew28
7
由于某些原因,仅添加xcuserdata而没有前缀对我来说无法起作用。虽然我认为应该可以,但很奇怪。 - BastiBen

18

5
这已经在上面的答案中发布过了。我发现它是错误的,支持存疑(有100多个未处理的pull请求!),并且没有文件记录。事实上,“错误”是最糟糕的;他们只为一小部分用例而设计了一个忽略功能,并且没有解释是什么或为什么!因此:我的答案纠正了他们的错误,并解释了正在进行的操作以及原因,因此您可以基于项目逐个做出教育决策(在新项目中,我有时会忘记其中的某些项目原因-注释帮助我做决定 :)) - Adam
@Adam:GitHub的.gitignore现已更新为Xcode 6.3.2和Swift,因此现在是正确的。它也有文档记录。 - Eric
是啊,但发布一种数据破坏性文件并保持这种状态数月甚至数年的问题在于 - 显然没有充分测试 - 这将永久牺牲社区的所有信任和尊重。为时已晚。 - Adam

18

你应该查看gitignore.io,以获取Objective-C和Swift的帮助。

这是我正在使用的.gitignore文件:

# Xcode
.DS_Store
*/build/*
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
profile
*.moved-aside
DerivedData
.idea/
*.hmap
*.xccheckout
*.xcworkspace
!default.xcworkspace

#CocoaPods
Pods

1
始终使用您的示例。不错的设置!谢谢! - Nikolay Shubenkov
我已经有一段时间没有使用http://gitignore.io了 - 如果你还没有使用过,值得一试。你可以使用它为你正在使用的任何IDE /语言等创建一个gitignore文件。它甚至会添加一个cocoapods部分。非常棒! - Ashley Mills
你应该保留 *.xcworkspace 文件,它可能是主项目文件。 - undefined

14

我同时使用AppCode和XCode。 因此应该忽略.idea/

将这个添加到Adam的.gitignore

####
# AppCode
.idea/

13

.gitignore文件添加内容:

Mac OS X + Xcode + Swift

以下是如何将.gitignore文件添加到我的Swift项目中:

  1. 在Xcode中选择您的项目,右键单击→ 新建组 → 将其命名为“Git
  2. 选择Git文件夹,右键单击→ 添加新文件
  3. iOS标签页中 → 选择其他空文件

输入图像描述

  1. 在此处命名文件 ".gitignore"

输入图像描述

  1. 确认文件名称和类型

输入图像描述

这是结果结构:

输入图像描述

  1. 打开文件,将以下代码粘贴到其中
# file

#########################################################################
#                                                                       #
#       Title         - .gitignore file                                 #
#       For           - Mac OS X, Xcode 7 and Swift Source projects     #
#       Updated by    - Ramdhan Choudhary                               #
#       Updated on    - 13 - November - 2015                            #
#                                                                       #
#########################################################################

########### Xcode ###########
# Xcode temporary files that should never be committed

## Build generated
build/
DerivedData

# NB: NIB/XIB files still exist even on Storyboard projects, so we want this
*~.nib
*.swp

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint
*.xcscheme

########### Mac OS X ###########
# Mac OS X temporary files that should never be committed

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

########## Objective-C/Swift specific ##########
*.hmap
*.ipa

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the Git repository. Instead, use fastlane to re-generate the

fastlane/report.xml
fastlane/screenshots

感谢Adam。他的回答帮了我很多,但我还需要添加几个条目来创建一个.gitignore文件,以适用于:

Mac OS X + Xcode + Swift

参考:这个这个


1
非常有用的答案。我将其作为链接添加到我的在Xcode中设置Github答案中。 - Suragch

13
大多数的答案都是来源于Xcode 4-5时代。我推荐使用现代风格的忽略文件。
# Xcode Project
**/*.xcodeproj/xcuserdata/
**/*.xcworkspace/xcuserdata/
**/.swiftpm/xcode/xcuserdata/
**/*.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
**/*.xcworkspace/xcshareddata/*.xccheckout
**/*.xcworkspace/xcshareddata/*.xcscmblueprint
**/*.playground/**/timeline.xctimeline
.idea/

# Build
**/.build/
**/Build/
DerivedData/
*.ipa

# Carthage
Carthage/

# CocoaPods
Pods/

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/sign&cert

# CSV
*.orig
.svn

# Other
*~
.DS_Store
*.swp
*.save
._*
*.bak

保持更新自:https://github.com/BB9z/iOS-Project-Template/blob/master/.gitignore


9
我的是一个 .bzrignore 文件,但是这个想法是一样的 :)
.DS_Store
*.mode1v3
*.pbxuser
*.perspectivev3
*.tm_build_errors

当我使用TextMate构建我的项目时,tm_build_errors可以用来记录错误。它不像Hagelin那样全面,但我认为值得发布tm_build_errors行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接