Notessh2a

Worktrees

Git worktrees let you have multiple working directories attached to a single repository.

  • git worktree
    • list - Show all active worktrees.
    • add <path> <branch_name> - Create a worktree for an existing branch.
    • add <path> <commit_hash> - Create a worktree at a specific commit in a detached HEAD state.
    • remove <path> - Remove the worktree folder and its reference.

How?

  • A worktree is simply another folder connected to the same repository.
  • It can point to any branch or commit.
  • Each worktree is isolated in its working files, but all of them share the same Git history.
  • Switching to a worktree is simply moving into that folder with cd.

Why?

  • You can work on different branches or commits in parallel.
  • You avoid stashing or making temporary commits just to move around.
  • Each task stays in its own separate folder.

Worktree Strategies

Do not place worktrees inside other working directories.

If you create a worktree folder inside another working directory, Git treats the folder as new content.

While Git can detect that it is a worktree and reduce it to a single entry, it still shows up as untracked content, which you usually don't want in your real project history.

With this warning in mind, there are two approaches to structuring projects when working with worktrees:

  1. Keep your worktrees outside the main working directory.

    E.g., right next to your main project folder. git worktree add ../feat feature

    ├─ project/  # <- main working directory
    ├─ feat/     # <- worktree for a feature branch
  2. Use a bare clone.

    • git clone --bare <remote_url>: Create a local bare copy of a remote repository.

    A bare clone is a repository without a working directory. It contains only the .git data.

    To start working, you need to create a worktree.