Tutorials Search / Native Mac IDE / Experiment safely with worktrees
📝 Written ● Intermediate Updated 2026-05-13

How do I experiment safely with git worktrees in LingCode?

TL;DR: Run Source Control → New Worktree (or git worktree add ../experiment-X feature-X) to create an isolated working copy on a branch. Point the agent at the worktree; if the experiment fails, delete the worktree — your main branch never touched it.

When you want the agent to try something speculative — a rewrite, a tricky migration, a "just see if this works" change — don't let it loose on your main branch. Fork a worktree, let it experiment, merge only what you want.

AI agents are at their most useful when you let them try something you wouldn't write yourself — a speculative refactor, a tricky migration, a "just see if this works" pass. The catch: trying things on your live working tree is risky. If the agent goes off the rails, it corrupts the same files you're trying to ship from. Your build breaks. Your editor's open chat is now confused about which version of the code is real.

The instinctive answer is "use a branch." That's not wrong, but it doesn't actually isolate anything. Switching branches in your main checkout drags your dirty files along, resets your IDE state, and forces context-switching in the AI chat. A branch is a name for a chain of commits; it's not a place. You can't be in two branches at once.

A worktree is git's real answer to "I want two places at once." It's a second physical checkout of your repository, pointed at a different branch, sharing the same .git store. Two trees, one history. You can have the agent rewrite something in tree B while you keep editing in tree A — separate windows, separate files, no contamination. This tutorial is about reaching for that when the stakes feel high enough to want isolation, and merging back selectively when the experiment lands.

What you'll learn

Step 1: Know when worktrees beat branches

1

Branches don't isolate your workspace

Switching branches in your main checkout drags your in-progress work along — your dirty files follow you, your IDE state resets, the AI's open chat tab is now talking about different code. For a quick fix that's fine. For a high-risk experiment, it's friction.

A worktree is a second physical checkout of your repo, pointed at a different branch, sharing the same .git store. Two trees, one history. You can have the agent rewrite something in tree B while you keep editing in tree A.

Reach for a worktree when the prompt is "rewrite this in async/await," "migrate from CoreData to SwiftData," "convert these UIKit views to SwiftUI" — anything where you want to see the result without committing to it yet.

Step 2: Open the Workspaces panel

2

From the sidebar

In the left sidebar, click the Workspaces tab. You'll see your current project listed with its active branch, plus a + New Worktree button at the top.

Workspaces is also where shadow worktrees show up — the implicit isolated trees the agent uses when it wants to try a tool call without touching your real files (more on that below).

Step 3: Pick a worktree mode

3

Four ways to fork

Click + New Worktree and pick a mode:

  • New branch — branch off your current HEAD with a name you supply. Most common for "try this idea."
  • Existing branch — check out a branch that already exists. Useful for reviewing teammate work without leaving your own.
  • Pull request — pulls a remote PR head into a worktree. Great for code review with the agent's help.
  • Linear issue — if you have Linear linked, creates a worktree pre-named for a given issue and ready for work.

Pick New branch for this tutorial. Name it something like try-async-await. LingCode runs git worktree add under the hood and opens the new tree in a fresh window.

Step 4: Let the agent run wild

4

Risky prompts are now contained

In the new window, the AI panel is talking about the worktree's files. Give it the speculative prompt: "Convert all of NetworkManager to async/await, no completion handlers." Whatever it changes is local to this tree. Your main branch is untouched.

You can also flip permission mode here to acceptEdits without worrying — auto-accept is a lot safer when the surface is contained.

Shadow worktrees: The agent itself sometimes opens an even-more-isolated shadow worktree to run builds or tests without touching your editor's file buffers. It uses APFS copy-on-write when worktrees aren't available, so it's effectively free. You'll see shadow workspaces in the Workspaces panel with a dotted icon.

Step 5: Review the diff

5

Compare against your main branch

When the agent finishes, open the diff view from the Workspaces panel — right-click the worktree and choose Compare with main. You'll see every file the experiment touched.

Walk the diff. Some changes will be good; some will be bizarre. That's expected — this is the whole point of doing it in a worktree first.

Step 6: Merge selectively

6

Cherry-pick what you want

Two patterns:

  • Like all of it: commit the changes in the worktree, switch back to your main window, merge the experimental branch in.
  • Like some of it: in the diff view, click the checkbox next to each hunk you want, then Apply to main. The selected hunks land as a new commit on your main branch; the rest of the worktree's changes are discarded.

This is the part git CLI makes painful and LingCode makes obvious — you can adopt the agent's good ideas without inheriting its bad ones.

Step 7: Clean up

7

Delete the worktree when done

Right-click the worktree in the panel and choose Remove worktree. LingCode runs git worktree remove, which deletes the working files but leaves the branch in git history if you committed. If you want the branch gone too, also choose Delete branch.

Don't delete a worktree with uncommitted changes you want to keep. LingCode warns before removing, but if you confirm, those changes are gone — they only existed inside that tree.

What's next