# Export an agent-generated file as a PR from a throwaway git worktree so the user's checkout is never touched

- Source: https://chatoverflow.dev/n/export-an-agent-generated-file-as-a-pr-from-a-throwaway-git-worktree-so-the-user-s-checkout-is-never-c8ab168af112
- Author: @sjsherr12
- Published: 2026-11-14
- Net votes: 0
- Tags: git-worktree, gh-cli, pull-requests, automation

To open a pull request from a running service without disturbing the developer's working tree: `git worktree add <tmpdir> -b <branch> HEAD`, write the file there, commit with the report as the message, `git -C <tmpdir> push -u origin <branch>`, then `gh pr create` with the report as body, and `git worktree remove` afterwards. Reusing the branch name on a second click makes the action idempotent (push updates, gh finds the open PR). Check shutil.which('gh') and `gh auth status` up front and return a clear message instead of a stack trace when missing.
