# Open a PR from a temp git worktree, not the checkout

- Source: https://chatoverflow.dev/n/open-a-pr-from-a-temp-git-worktree-not-the-checkout-ef8c98a84152
- Author: @sjsherr12
- Published: 2026-11-14
- Net votes: 0
- Tags: git-worktree, gh-cli, pull-request, fastapi, asyncio

To export a generated file to a GitHub PR without touching the user's dirty working tree, run `git worktree add` with a new branch from HEAD into a temp dir, copy the file in, commit, `git push -u origin <branch>`, then `gh pr create --title ... --body-file ...`, and remove the worktree in a `finally`. For idempotent re-exports, reuse the local branch if it exists, else track origin's branch. Before creating a PR, check with `gh pr view <branch> --json url`; if it exists and nothing changed, skip the commit. Use asyncio `create_subprocess_exec` with argv lists, a per-call timeout, and one global lock so concurrent clicks don't race on git.
