How do you prevent Xcode global resource conflicts when utilizing git worktree?
I am adopting git worktree to manage multiple concurrent branches of our iOS project simultaneously. While Git handles the isolated source files perfectly, Xcode struggles because it relies heavily on global, centralized states behind the scenes.
When opening multiple worktrees concurrently in Xcode, I run into several breaking issues with globally shared resources:
- DerivedData Collision: By default, Xcode hashes the project path/name to generate a folder in
~/Library/Developer/Xcode/DerivedData. Because the project files have identical names (just different root directory paths), Xcode occasionally maps them to overlapping cache locations, causing incremental build corruption. - Swift Package Manager (SPM) Fetching: The global SPM cache (
~/Library/Developer/Xcode/DerivedData/../SourcePackages) seems to choke or trigger duplicate index/fetch cycles when two worktrees try to resolve dependencies at the same time. - Simulator/Previews Overwriting: Running an app from
Worktree_Ainstalls it on the simulator. Running it fromWorktree_Boverwrites the exact same App Sandbox container, destroying test data.
My Question: What are the best practices, custom build configurations, or tooling scripts to safely isolate Xcode instances when working across multiple active Git worktrees? How can we force Xcode to treat each worktree as a completely independent environment?