Function: projectile-switch-worktree

projectile-switch-worktree is an autoloaded, interactive and byte-compiled function defined in projectile.el.

Signature

(projectile-switch-worktree &optional ARG)

Documentation

Switch to another checkout of the current project's repository.

That's the project's git worktrees, plus any other clone of the same upstream that Projectile already knows about - both are the same thing in practice, the place this project is checked out on another branch.

Invokes the command referenced by projectile-switch-project-action on switch. With a prefix ARG invokes projectile-dispatch instead.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/projectile-20260820.1509/projectile.el
;;;###autoload
(defun projectile-switch-worktree (&optional arg)
  "Switch to another checkout of the current project\\='s repository.

That's the project\\='s git worktrees, plus any other clone of the same
upstream that Projectile already knows about - both are the same thing in
practice, the place this project is checked out on another branch.

Invokes the command referenced by `projectile-switch-project-action' on
switch.  With a prefix ARG invokes `projectile-dispatch' instead."
  (interactive "P")
  (let* ((root (projectile-acquire-root))
         (worktrees (seq-remove
                     (lambda (worktree)
                       ;; The checkout we're already in is not somewhere to
                       ;; switch to, and one that's been deleted from under
                       ;; its registration can't be switched to at all.
                       (or (plist-get worktree :prunable)
                           (file-equal-p (plist-get worktree :path) root)))
                     (projectile-project-worktrees root)))
         ;; Offered in the spelling every other switch command uses, so a
         ;; worktree looks the same here as in `projectile-switch-project'.
         (by-path (mapcar (lambda (worktree)
                            (cons (projectile--known-project-root
                                   (plist-get worktree :path))
                                  worktree))
                          worktrees)))
    (unless worktrees
      ;; Say which of the two it is: nothing to switch to, or nothing
      ;; Projectile is able to look at.  They call for different responses
      ;; and the same message for both sends people hunting for a bug.
      (cond
       ((file-remote-p root)
        (user-error "Projectile doesn't look for the checkouts of a remote project"))
       ((projectile-repo-identity root)
        (user-error "No other checkout of %s found" (projectile-project-name root)))
       (t
        (user-error "Cannot tell what %s is a checkout of - only git, Mercurial and Jujutsu say.  Try `projectile-switch-sibling-project'"
                    (projectile-project-name root)))))
    (projectile-completing-read
     "Switch to worktree: " (mapcar #'car by-path)
     :action (lambda (path)
               (projectile-switch-project-by-name path arg))
     :annotation-function (lambda (path)
                            (projectile--worktree-annotation
                             (cdr (assoc path by-path))))
     :category 'projectile-worktree)))