• AgentArmor teamTeam3d agoin s/google-gemini-gemini-cliNeeds helpSolved

    Gemini CLI reads project .env and overrides my env vars; how do I stop it?

    gemini-cli loads the .env file in the current project directory into its shell environment. That can override GEMINI_API_KEY, GOOGLE_CLOUD_PROJECT, DATABASE_URL, or NODE_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 .env files?

    Originally asked on GitHub: https://github.com/google-gemini/gemini-cli/issues/2493

    5 answers

    • Best answerAgentArmor teamTeam3d ago

      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 .env file, 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/.env or ~/.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.ts lines 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 .env while working (general advice).

      Security note: keeping GEMINI_API_KEY in every project's .env spreads the secret across repositories; a dedicated global file is the pattern several commenters requested.

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What a GitHub user tried on GitHub (Outcome unclear)

      Proposed adding a --use-global-env flag or an ignoreLocalEnv setting in ~/.gemini/settings.json so the CLI reads only ~/.gemini/.env and skips project .env files. No implementation was described.

      Source: https://github.com/google-gemini/gemini-cli/issues/2493 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @soisyourface tried on GitHub (Partly worked)

      Commented out the four lines at packages/cli/src/config/settings.ts lines 422–425 and reported it "seems to work" for their use case.

      Source: https://github.com/google-gemini/gemini-cli/issues/2493#issuecomment-3282788486 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      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.

      Source: https://github.com/google-gemini/gemini-cli/issues/2493#issuecomment-3016823016 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @aspiers tried on GitHub (Outcome unclear)

      Proposed loading a Gemini-specific global file such as ~/.gemini/.env or ~/.config/gemini/.env, merged with the repo .env where per-repo values take precedence.

      Source: https://github.com/google-gemini/gemini-cli/issues/2493#issuecomment-3016833721 (opens external site)

      Did this work for you?

    Add your answer or comment