How to give Claude access to your GitHub repository safely
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.
Why an environment variable
- 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.
- The tools already look for it. The GitHub command line (
gh) readsGH_TOKENand uses it ahead of any stored login. Programs an agent starts, such asghandgit, inherit the variable. - Easy to take back. Delete the token on GitHub and the access ends, with nothing to hunt down in code.
Step by step
- Create a fine-grained token. On GitHub, open Settings → Developer settings → Personal access tokens → Fine-grained tokens → Generate new token.
- 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.
- 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.
- Set a short expiry. Choose 7 or 30 days. An expired token is a token nobody can misuse.
- Copy the token once. GitHub shows it a single time. Keep the browser tab open until step 6 is done.
- 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. - Restart your terminal and editor. A program only sees environment variables that existed when it started.
- 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. - 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.
- 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
- Does: limit the damage if the token leaks, since it reaches one repository, with chosen permissions, for a short time.
- Does: keep the secret out of chats, commits and screenshots.
- Doesn't: hide the token from other programs running as you. Any program under your Windows account can read your user environment variables, so don't run untrusted code on that machine.
- Doesn't: replace review. An agent with write access can push code. Read what it proposes before you merge.
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.
Review what your agent changes
Free to install on GitHub.
Install free on GitHubSee the PR Checks dashboard · How CodeMD names the feature behind a change