How to give Claude access to your GitHub repository safely

CodeMD · 2026-11-14 · A how-to guide

An AI coding agent that can read your repository, open pull requests or run gh commands needs a credential. The safest way to hand it one is a fine-grained token for a single repository, saved in an environment variable, never pasted into the chat. Here is how, step by step, on Windows with PowerShell.

The rule to keep: a secret goes into your own terminal, not into a conversation. Anything you paste into a chat can be stored, logged or shown back. An environment variable keeps the token out of the conversation and out of your repository.

Why an environment variable

  1. Out of your code. A token written in a file can be committed by accident. An environment variable lives on your machine, outside the repository.
  2. The tools already look for it. The GitHub command line (gh) reads GH_TOKEN and uses it ahead of any stored login. Programs an agent starts, such as gh and git, inherit the variable.
  3. Easy to take back. Delete the token on GitHub and the access ends, with nothing to hunt down in code.

Step by step

  1. Create a fine-grained token. On GitHub, open Settings → Developer settings → Personal access tokens → Fine-grained tokens → Generate new token.
  2. Limit it to one repository. Under Repository access, choose Only select repositories and pick the one repo the agent needs. For an organization's repository, the organization may need to approve the token.
  3. Grant the least it needs. Under Permissions, start with Contents: Read-only if the agent only reads code. Add Contents: Read and write and Pull requests: Read and write only if it must push or open pull requests. Metadata read access is added automatically.
  4. Set a short expiry. Choose 7 or 30 days. An expired token is a token nobody can misuse.
  5. Copy the token once. GitHub shows it a single time. Keep the browser tab open until step 6 is done.
  6. Save it as an environment variable without leaving it in your history. In PowerShell, this prompt hides what you type:
    $t = Read-Host "GitHub token" -AsSecureString
    [Environment]::SetEnvironmentVariable("GH_TOKEN",
      [Runtime.InteropServices.Marshal]::PtrToStringAuto(
        [Runtime.InteropServices.Marshal]::SecureStringToBSTR($t)), "User")
    Typing the token directly into a command would leave it in your shell history, so use the prompt.
  7. Restart your terminal and editor. A program only sees environment variables that existed when it started.
  8. Test it without printing the token.
    gh auth status
    gh repo view OWNER/REPO
    "Token set: $([bool]$env:GH_TOKEN), length $($env:GH_TOKEN.Length)"
    You should see the repository's details and a token length, not the token.
  9. Tell the agent what it may do. Ask it to start with read-only commands, and keep your tool's permission prompts on so you approve any command that changes something.
  10. Take it back when you're done. Delete the token under Developer settings → Fine-grained tokens, then remove the variable:
    [Environment]::SetEnvironmentVariable("GH_TOKEN", $null, "User")

What this does and doesn't protect

Check what the agent's changes reach

Once an agent is changing code, the next question is what a change touches. CodeMD's GitHub app adds checks to each pull request: new risky call paths from outside input, the tests connected to the change, and the feature the change belongs to. Access to your code is read-only, and your tests run in your own GitHub Actions. CodeMD gives your team information about risk paths; it doesn't block attacks or replace a security program.