Ignore Generated Xcode .xcworkspace Files in Git Except Package.resolved

Sven Schmidt alerted me to the fact that you can check in Package.resolved to pin Swift package versions in Xcode-managed projects if you add the file from within the generated project.xcworkspace file in your .xcodeproj bundle:

git add *.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

But!

You may have a .gitignore rule that skips *.xcodeproj/project.xcworkspace. Git will inform you about that. Mine was located in ~/.gitignore_global, actually, and I didn’t realize that.

Luckily, you can write .gitignore exceptions with the ! prefix.

But!

These only work on siblings. So these rules compose:

foo/bar/*
!foo/bar/baz

While these don’t:

foo/*
!foo/bar/baz

What I ended up doing is adding as few rules to my global gitignore as possible while adding enough to add an exception for the resolved package dependencies file:

# Ignore generated Xcode project files except SwiftPM resolved packages to pin versions in teams
*.xcodeproj/project.xcworkspace/contents.xcworkspacedata
*.xcodeproj/project.xcworkspace/xcuserdata/*
*.xcodeproj/project.xcworkspace/xcshareddata/*.xcsettings
*.xcodeproj/project.xcworkspace/xcshareddata/*.plist
*.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/*
!*.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

That’s now part of my ~/.gitignore_global and allows me to check in Swift package versions for team projects.

Thank you, Sven!