How does an API key end up in a git push, and what keeps it out?
In short
A saved permission rule can hold a command with its key, and .claude/settings.json is meant to be committed. Where the key's value can live instead.
Contents · 9 sections
An API key pasted into a chat with a coding agent such as Claude Code can end up in a git push. Nobody types it into a commit: it travels from the chat into a command, from the command into a saved permission rule, and from the rule into a file the team commits. So what keeps it out?
This post follows that path through Claude Code's own documentation, then a second path that looks like the fix and leaks the same key. The developer, the key
abc123 and the address are made up. The settings files below were written by hand in a test repository and committed with git; Claude Code itself was not run for this post, and the output shown comes from that git run.
Why the question exists
A coding agent runs shell commands for you. Say the key sits in the chat, and the agent builds a request with it:
curl -H "Key: abc123" https://api.example.com
Claude Code asks before it runs a Bash command, apart from a built-in set of read-only commands. One answer on that prompt is "Yes, and don't ask again", which for Bash commands lasts "Permanently per repository and command", so it is written down somewhere.
Step 1: the answer becomes a rule in a local file
According to "Configure permissions", when you choose "Yes, and don't ask again" for a Bash command, "Claude Code saves the rule to
.claude/settings.local.json at the root of the git repository". A Bash rule "matches the command text Claude writes", and "A rule with no
* matches one exact command". A rule for the command above, written out in full, would hold the key:
{ "permissions": { "allow": [ "Bash(curl -H \"Key: abc123\" https://api.example.com)"] } }
Whether the prompt saves the whole command or a shorter prefix with a *, the documentation does not say for this case. It does say that the option names what it saves: "a rule you save from a prompt covers only what its option named". So the label on the option is the place to look.
So far the key has not left. Of the local file, the settings page says: "Claude Code keeps it out of git when it creates the file", by adding it to your global git excludes. In the test repository a
.gitignore line stood in for that exclude:
committed files:.gitignoreabc123 in committed files: none
Step 2: the rule moves to the shared file
The same page lists a second file, .claude/settings.json, for "Everyone working in the folder that contains it. In a git repository, commit it so teammates get it". Team permissions belong there, so a useful rule tends to move.
In the command line, Claude Code "writes only to your local file", so the move is made by hand. In the VS Code extension, the approval card "lets you pick the destination file, including the project's shared file".
Once the rule sits in the shared file and is committed, the key is in a commit:
== case 1b: the same rule moved to the shared fileabc123 in committed files:HEAD:.claude/settings.json:1
The git manual describes a push as "Update remote refs along with associated objects". The commit, and the file in it, go along.
The second case: the rule holds a name, the value goes to env
The obvious fix is to keep the key out of the command. The command names a variable instead:
curl -H "Key: $API_KEY" https://api.example.com
The Bash manual describes what the shell does with that: "The basic form of parameter expansion is ${parameter}, which substitutes the value of parameter." The command text holds only the name, so a rule saved from it holds only the name.
But the value still has to live somewhere. The shared settings file has a place for it: the settings page says the file holds, among other things, "the environment variables the project needs", in a key called
env. Put the value there, and it is in the file that gets committed:
{ "permissions": { "allow": [ "Bash(curl -H \"Key: $API_KEY\" https://api.example.com)" ] }, "env": { "API_KEY": "abc123" } }
The test repository gave the same answer as in step 2:
== case 2: the rule names the variable, the value goes into envabc123 in committed files:HEAD:.claude/settings.json:1
Two different routes, one result. Whatever sits in the shared file leaves with the push, whether it is a rule or a value.
The test: move only the value
The third version changes one thing. The rule still names $API_KEY, and the shared file has no
env entry for it. The value is exported in the shell, outside the repository. Git now finds the key in no committed file, and the shell still fills it in when the command runs:
== case 3: the value outside the repository, exported in the shellabc123 in committed files: nonewhat the shell makes of the command text:curl -H "Key: abc123" https://api.example.com
The name travels with the repository. The value stays on the machine.
The same run shows what the fix does not undo. The earlier commits still hold the key (hashes shortened):
9cda86d name only: none8c14b22 name in rule, value in env: 8c14b22…:.claude/settings.json:17706872 shared rule: 7706872…:.claude/settings.json:13b9e0d8 local rule: none
Removing the value in a later commit leaves it in every commit before.
Check it yourself
Three commands show where your settings stand, without typing a key into your terminal:
git ls-files .claudegit check-ignore -v .claude/settings.local.jsongit grep -n '"env"' -- .claude/settings.json
The first lists the committed settings files. The second names the rule that keeps the local file out, or prints nothing if none does. The third shows whether the shared file has an
env section to read through.
What this does not cover
- Whole command or prefix. The documentation does not say whether "Yes, and don't ask again" saves the full curl command or a prefix here, hence "can save" in the reel. Read the option's label before you pick it.
- The push is an inference. That a committed file leaves with the next push follows from git's description of push and from the shared file being meant for committing. The test repository had no remote, so no push was run.
- How the agent's commands see your shell's variables. The pages opened for this post do not say in so many words that commands Claude Code runs receive variables exported in the shell it was started from. Test it with a harmless variable before relying on it.
-
Trust is not a guard. Allow rules and most
envvalues from the shared file apply for a teammate only after they trust the folder, says the settings page. That does not stop the file from being pushed. - History. None of the sources covers how to remove a key from commits already pushed.
Get the next explainer
Every reel on this site gets a post like this one: the example worked through, with sources and limits. Until the next one: let the command name the variable, and keep the value outside the repository.
If you would like the next post by email, sign up below. You will first get an email with a confirmation link; you are only on the list after you click it, and every email has a link to unsubscribe.
Sources
- Claude Code Docs, "Configure permissions": what "Yes, and don't ask again" saves and where, exact-command rules, what a saved rule covers.
-
Claude Code Docs, "Settings files and precedence": the local and the shared file, the global git exclude,
env, the VS Code destination choice, folder trust. - GNU, Bash Reference Manual, "Shell Parameter Expansion": how the shell fills in a variable's value.
- Git, "git-push Documentation": what a push sends.
All sources last opened on 1 December 2026. The git run used git 2.55.0 on 1 December 2026 in a local test repository with no remote; the settings files were written by hand in the shape the documentation describes.
More posts
-
How can ten tests find one bad commit in a thousand?
git bisect tests the middle commit and drops the half that cannot hold the bug. Ten halvings cover a thousand commits, if the test answers the same way.
-
What does npm install run before you open a single file?
npm install runs the scripts listed in the folder's own package.json. npm 12 blocks those of downloaded packages until that same file approves them.
-
The package didn't exist, so why was the failed install the better one?
Coding models make up package names, and many come back every time. Once someone registers one, the install and a list check both pass.