Best practices for avoiding target and cache conflicts in Xcode when working with Git worktrees?

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:

  1. 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.
  2. 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.
  3. Simulator/Previews Overwriting: Running an app from Worktree_A installs it on the simulator. Running it from Worktree_B overwrites 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?

Best practices for avoiding target and cache conflicts in Xcode when working with Git worktrees?
 
 
Q