Skip to content

Checkout PR by creating a new worktree #972

Closed

Description

@tony84727

Describe the feature or problem you’d like to solve

Make gh pr checkout support worktree to checkout the PR in a separate working directory without interrupting on-going work of current working directory.

Proposed solution

Maybe add a flag to gh pr checkout to instead checking out the branch inside the current worktree but create a new worktree with that PR checked out at a given location.

We can also add a config of the "directory of PR worktrees"? So the cli will create a new worktree inside the worktree directory <worktrees_dir>/pull/<pr-number>

Activity

  1. added
    enhancement
    on May 20, 2020
  2. added
    core
    on Oct 7, 2020
  3. lilyball commented on Oct 8, 2020

    @lilyball

    I'd like to see this, especially the "create a worktree in a known temporary location", but I would also want a way to say "prune all of my PR worktrees" so I can run that periodically.

  4. YakDriver commented on Apr 2, 2021

    @YakDriver

    If a directory is passed in, it could be the worktree directory.

    % git status
    On branch main
    Your branch is up to date with 'origin/main'.
    
    nothing to commit, working tree clean
    % gh pr checkout 1492 ../worktree1
    % cd ../worktree1
    % git status
    On branch b-branch-associated-with-1492
    Your branch is up to date with 'upstream/b-branch-associated-with-1492'.
    
    nothing to commit, working tree clean
  5. macintacos commented on Apr 15, 2021

    @macintacos

    @YakDriver I keep getting a "too many arguments" error when trying that. Can you elaborate a bit on your setup?

    In any case, as someone just discovering worktrees and how nice they are, I'm sad to see that this isn't already a thing with gh.

  6. matschaffer commented on Sep 10, 2021

    @matschaffer
    Contributor

    I wouldn't want this as the default for gh pr checkout but an option, maybe gh pr checkout --worktree perhaps with ../{{ current dir name}_pr{number} as a default but overridable argument to the switch.

  7. despreston commented on Jan 27, 2022

    @despreston
    Contributor

    I wrote a command for myself in my forked cli/cli. I went with gh pr worktree <branch>. Anyone at @github ok w/ that as a new command? I'd be happy to polish it, write some tests, and open PR. I dont think it makes sense to add worktree related stuf to the checkout command because they're completely separate in the world of git commands.

  8. cap10morgan commented on Dec 22, 2022

    @cap10morgan

    @despreston I like your proposal (FWIW, I don't work at GitHub). You still thinking of opening a PR?

  9. despreston commented on Dec 22, 2022

    @despreston
    Contributor
  10. cap10morgan commented on Dec 22, 2022

    @cap10morgan

    @cap10morgan I ended up writing a plugin.

    https://github.com/despreston/gh-worktree

    Oh, cool. I didn't know those were a thing.

    For anyone else reading this who wants worktree support in GitHub CLI, just run this:

    gh extension install despreston/gh-worktree

    Thanks @despreston for writing and publishing this!

  11. added 3 commits that reference this issue on Jan 13, 2023
    8a58e89
    a3ab172
    5631756
  12. added
    gh-pr
    on Oct 2, 2023
  13. aryairani commented on Oct 29, 2023

    @aryairani

    @cap10morgan I ended up writing a plugin.
    despreston/gh-worktree
    gh extension install despreston/gh-worktree

    Thanks for writing this, but it doesn't work for me though:

    % gh worktree pr 4361 ../4361
    fatal: invalid reference: pull-explanation-missing-spaces
    Error: exit status 128
    

    Possibly due to the PR's source branch being in a forked repo. I'll post a ticket at despreston/gh-worktree, but I wanted to mention here that the problem's still not fully solved, and that a flag on the gh pr command would still be better.

  14. engnadeau commented on Aug 20, 2025

    @engnadeau

    It's been a couple years since the last comment, has anyone found a good practical solution?

    i've been using this in my ~/.zshrc

    gh-issue-worktree() {
      local issue=$1
      local repo=$(basename -s .git "$(git config --get remote.origin.url)")
    
      # Fetch and slugify issue title
      local raw_title=$(gh issue view "$issue" --json title -q ".title")
      local slug=$(echo "$raw_title" | tr '[:upper:]' '[:lower:]' \
                   | sed -E 's/[^a-z0-9]+/-/g' \
                   | sed -E 's/^-+|-+$//g' \
                   | cut -c1-40)
    
      local branch="${repo}-${issue}-${slug}"
      local dir="$HOME/git/worktrees/${branch}"
    
      # Create branch if not exists
      if ! git show-ref --verify --quiet "refs/heads/$branch"; then
        gh issue develop "$issue" --name "$branch"
      fi
    
      # Add worktree if not exists
      if [ ! -d "$dir" ]; then
        git worktree add "$dir" "$branch"
      fi
    
      # Open editor on worktree instead of cd
      code "$dir"
    }
  15. dkarvik commented on Aug 22, 2025

    @dkarvik

    I ended up somewhere similar. Instead of calling code though I just echo the path and then do as I desire from the call. I also store my worktrees within the repo itself and use a local gitignore to hide it. I've also resorted to just using branch name and using the "copy" button on the PR to see it.

    #!/bin/sh
    
    worktrees="$(git rev-parse --git-common-dir --path-format=absolute)/../worktrees"
    destination="$worktrees/$1"
    
    fetch(){
        git worktree prune
        git worktree list --porcelain | grep "^branch .*${1}$"
        if [ $? -eq 1 ]; then
            # git worktree list | grep "^branch .*${1}$"
            git fetch;
            if git ls-remote --exit-code --heads origin "${1}"; then
                git worktree add "$destination" $1;
            else
                git worktree add -B $1 "$destination" --guess-remote "$(git rev-parse origin/master)"
            fi
        else
            cd "$destination"
            git pull
        fi
    }
    
    (fetch "$1") 1>&2 && echo "$(realpath "$destination")" || echo "$PWD"

    Usage:

    $ cd `git getpr foo/bar`

    But you could also do

    $ code `git getpr foo/bar`
  16. ffalor commented on Feb 23, 2026

    @ffalor
    Contributor

    Since this is still marked as core (outside contributors can't create pr's for changes marked core), and the team is not sure if they want to do this yet, I ended up creating an extension for this problem after trying the great options mentioned here.

    I wanted something that could be used for more than just PR's. The main goal was to quickly spin up a worktree and spawn a Claude code instance in it with the initial prompt. I ended up creating an action concept to make it extensible passed just my use case.

    Docs: https://ffalor.github.io/gh-wt/

    It supports creating worktrees from PRs, Issues, or just one locally:

    • PR: gh wt add https://github.com/owner/repo/pull/123
    • Issue: gh wt add https://github.com/owner/repo/issues/456
    • Local: gh wt add my-feature-branch

    @engnadeau use case for opening vscode after creation is a great example of how actions can be used.

    gh wt add https://github.com/owner/repo/pull/123 -a vscode or gh wt run <worktree> <action> to execute actions in already existing worktrees.

    #~/.config/gh-wt/config.yaml
    actions:
      - name: vscode
        dir: "{{.WorktreePath}}"
        cmds:
          - code .
  17. added
    pitch
    on Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreenhancementgh-prpitch

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions