在开发 Swift Package 项目的过程中,假如选用 Git 来办理版别,上传到远程库房进行分享,那么本地必定会有一些文件,咱们不需求进行盯梢。

这些文件可能是一些缓存,只用于咱们自己本地。也可能是一个临时文件,是项目运行或者编译为本地项目发生的,亦或是一些用户设置或者敏感信息,咱们不希望将其纳入版别控制,并上传到远端。这时候只需求为Git参加一份.gitignore即可。

不管运用指令或者Xcode14创立Swift Package,都会主动生成 .gitignore 文件。本文是记载一下手动为项目增加.gitignore文件的流程。

创立.gitignore文件

切换到项目目录,初始化Git,然后创立一个.gitignore空文件。

$ cd <项目目录>
$ git init
$ touch .gitignore

假如不喜欢指令,也能够运用VSCode或其他编辑器新建文件也能够。

至于文件文件内容,就根据项目内容中的需求,将不要盯梢的文件或者路径增加进去即可。

这儿推荐一个网站:创立.gitignore内容,能够快速生成.gitnore文件内容。

增加git ignore内容

我用上边的网站生成了一份,其中增加的关键词是 macos, xcode, swift, swiftPM, swift package manager。

主动生成内容许多,包括了其认为咱们可能需求的装备,实际上有不少内容,现在应该是用不到的。

我修正后的内容如下:

### macOS ###
# General
.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
# .com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
# .AppleDB
# .AppleDesktop
# Network Trash Folder
# Temporary Items
# .apdisk
### macOS Patch ###
# iCloud generated files
# *.icloud
### Swift ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## User settings
# xcuserdata/
## Obj-C/Swift specific
# *.hmap
## App packaging
# *.ipa
# *.dSYM.zip
# *.dSYM
## Playgrounds
# timeline.xctimeline
# playground.xcworkspace
# Swift Package Manager
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
.swiftpm
.build
# 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://www.6hu.cc/files/2023/06/1686131611-be4b37a2ccf37b6.png
# fastlane/test_output
# Code Injection
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode
# iOSInjectionProject/
### SwiftPackageManager ###
# Packages
# xcuserdata
# *.xcodeproj
### Xcode Patch ###
# *.xcodeproj/*
# !*.xcodeproj/project.pbxproj
# !*.xcodeproj/xcshareddata/
# !*.xcodeproj/project.xcworkspace/
# !*.xcworkspace/contents.xcworkspacedata
# /*.gcno
# **/xcshareddata/WorkspaceSettings.xcsettings

修正原因:

  1. 现在是运用 macOS 开发,.DS_Store是 macOS 体系用于存储目录的自定义特点的躲藏文件,是本地文件,无需盯梢。
  2. .swiftpm 文件夹是由 Swift Package Manager (SPM) 创立的,用于存储关于项目的本地状况信息, 无需盯梢。
  3. .build 目录是 Swift Package Manager(SPM)在构建过程中生成的,用于存放编译后的产物以及其他与构建过程相关的文件,无需盯梢。
  4. Package.resolved是需求盯梢的,文件记载了 Swift Package Manager (SPM) 的准确依靠版别。

查看增加到git的文件

增加好了.gitignore文件的文件的内容,能够查看一下盯梢的文件对不对。
切换到项目目录下,运用如下指令:

$ git status

在终端能够看见,Git 现已不再盯梢相关的文件。

关于.gitignore文件,便是一份排除名单,能够把需求排除的文件或者目录增加进去,git就再进行盯梢,方便于日常开发。

修正.gitignore文件注意事项

后期发现有新的文件文件或目录不需求Git盯梢,能够从头修正文件。可是要注意流程:

  • 假如这个文件以前没有盯梢过,能够直接修正.gitignore文件,提交即可。
  • 假如这个文件盯梢过,需求先移除盯梢,修正.gitignore文件文件,再提交。

对已盯梢文件或者目录,需求先移除git对文件的盯梢,

$ git rm -r --cached <文件或者目录>

手动修正 .gitignore 文件内容,

然后再提交,

$ git commit -m "update .gitignore"

这时就移除了现已盯梢文件或目录。