向SwiftPackage中添加.gitignore文件

2,209 阅读1分钟

在开发 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://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
# Pods/
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

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

# Carthage/Build/

# Accio dependency management
# Dependencies/
# .accio/

# fastlane
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

# fastlane/report.xml
# fastlane/Preview.html
# fastlane/screenshots/**/*.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"

这时就移除了已经跟踪文件或目录。