Gemini CLI reads project .env and overrides my env vars; how do I stop it?
gemini-cli loads the
.envfile in the current project directory into its shell environment. That can overrideGEMINI_API_KEY,GOOGLE_CLOUD_PROJECT,DATABASE_URL, orNODE_OPTIONS, which breaks authentication, test runs, and MCP configuration. Is there a flag or setting to make it use a global.env(for example~/.gemini/.env) and ignore project.envfiles?Originally asked on GitHub: https://github.com/google-gemini/gemini-cli/issues/2493
5 answers
The issue was closed as COMPLETED, but this thread does not show a released flag or configuration that makes Gemini CLI ignore a project's
.envfile, so the behavior remains as reported. Commenters disagreed on the ideal fix: one proposed layering env files from the home directory down to the repo and current directory, with lower-level files overriding upper ones and CLI-passed variables on top; another proposed a Gemini-only global file such as~/.gemini/.envor~/.config/gemini/.env(not~/.env, which other programs may read) merged with the repo.env, with repo values taking precedence.The only workaround reported in the thread is a local source edit: one commenter commented out the four lines at
packages/cli/src/config/settings.tslines 422–425 and said it "seems to work" for their use case. That edits the CLI itself; treat it as a temporary local fix that may be overwritten when the CLI updates (general advice). Practical alternatives, not verified in the thread: run Gemini CLI from a directory without a conflicting.env, or temporarily rename the project.envwhile working (general advice).Security note: keeping
GEMINI_API_KEYin every project's.envspreads the secret across repositories; a dedicated global file is the pattern several commenters requested.Did this work for you?What a GitHub user tried on GitHub (Outcome unclear)
Proposed adding a
--use-global-envflag or anignoreLocalEnvsetting in~/.gemini/settings.jsonso the CLI reads only~/.gemini/.envand skips project.envfiles. No implementation was described.Source: https://github.com/google-gemini/gemini-cli/issues/2493 (opens external site)
Did this work for you?What @soisyourface tried on GitHub (Partly worked)
Commented out the four lines at
packages/cli/src/config/settings.tslines 422–425 and reported it "seems to work" for their use case.Did this work for you?What @contributor tried on GitHub (Outcome unclear)
Suggested a precedence chain without a new flag: home
.env-> repo.env-> current dir.env, each level overriding the previous one, with CLI-passed variables on top.Did this work for you?What @aspiers tried on GitHub (Outcome unclear)
Proposed loading a Gemini-specific global file such as
~/.gemini/.envor~/.config/gemini/.env, merged with the repo.envwhere per-repo values take precedence.Did this work for you?
Related questions
- gemini-cli 400: "only 'enum' and 'date-time' are supported for STRING type"
- Gemini CLI: API Error 'Model stream ended with an invalid chunk or missing finish reason' after ReadFile or Edit tool calls
- gemini-cli stuck on "This is taking a bit longer" after Esc cancel — is there a fix?
- Is there a way to get codebase indexing in gemini-cli?
- How can I use voice input instead of typing with Gemini CLI?