Gemini CLI Cheatsheet - Google Terminal AI Agent Full Reference
Gemini CLI is Google open-source terminal AI agent (Apache 2.0) for developers who need very long context and want the model to edit code autonomously against their own models; its 2M-token window fits whole-repository refactoring and analysis. Long context, forkable self-hosting, and GEMINI.md project memory set it apart. This sheet covers install/auth, headless automation (-p -o json), and MCP/extensions.
Typical Use Case
Gemini CLI is best for long-context work: a 2M-token window lets it read whole-repo source/docs for cross-file refactors, architecture analysis, and large batch edits most conversational tools cannot handle. For daily coding, use the interactive terminal to edit code and run tests; for long sessions or repo-wide tasks, put build/test commands in GEMINI.md so every session auto-loads team conventions. Headless via -p -o json feeds one-shot reviews and docgen into scripts or CI. It also connects to your own models (local or custom endpoints) and MCP/extensions — a good fit for teams that want model control and refuse vendor lock-in.
Install & Auth 6
npm install -g @google/gemini-cligemini --versiongeminigemini --auth-method=api-keyexport GEMINI_API_KEY="your-key"export GOOGLE_GENAI_USE_VERTEXAI=trueRun Modes 8
geminigemini "Refactor the user-auth module"gemini -p "Explain this function"gemini -i "Implement the cache first"gemini mcpgemini extensionsgemini skillsgemini gemmaGlobal Flags 15
gemini -m gemini-2.5-pro "task"gemini -y "Refactor the whole module"gemini --approval-mode plan "Explore architecture"gemini -w feat/logingemini -sgemini --skip-trustgemini --checkpointinggemini -r latestgemini --list-sessionsgemini --delete-session 2gemini --include-directories ../backend,../frontendgemini -o json "task"gemini -e context7gemini -lgemini --debugSession Slash Commands 14
/help/model/context/compress/clear/save/restore/yolo/mcp list/tools/init/memory list/memory refresh/quitMCP & Extensions 7
gemini mcp add <name> <url>gemini mcp remove <name>gemini mcp listgemini mcp enable <name>gemini mcp disable <name>gemini extensions install <name>gemini mcp add --url https://example.com/mcpGEMINI.md & Config 5
echo "Build: make build\nTest: go test ./..." > GEMINI.md@./docs/architecture.md!git statusecho ".env\nnode_modules/" > .geminiignorecat ~/.gemini/settings.jsonCommand Examples
Headless run with JSON output for scripts
gemini -p -o json "对比当前代码与最近一次 tag 的 API 差异"-p 非交互即跑即退,-o json 再配 --save 可把结果落盘,是 CI 自动化的常用组合。
Output
(模型只输出结构化 JSON 供下游脚本消费,例如新增/删除的接口清单与风险提示)
One-shot quick question
gemini "帮我解释这个函数的递归逻辑"与 -p 相比,无参数的非交互调用同样即跑即退,无需进入 TUI。
Output
(直接在当前终端返回回答后退出,适合查文档片段与做单次决策)
Bootstrap project memory
/init
echo "Build: make build\nTest: go test ./..." > GEMINI.md
/memory refreshGEMINI.md 每次会话自动加载,把项目约定沉淀其中可显著减少重复提示;敏感文件用 .geminiignore 排除。
Output
(/init 生成 GEMINI.md 骨架,补充构建/测试命令后 refresh 立刻生效,无需重启会话)
Common Pitfalls
- A 2M-long context loads fast, but first-pass latency grows as you stuff in many files — configure GEMINI.md and .geminiignore so node_modules, logs, and other bulk files stay out of context.
- -p headless runs exit immediately with no human to approve file edits and command execution; scope allowed actions in config rather than defaulting to full write access.
- GEMINI.md conventions are auto-followed every session, so editing it or dependencies changes future behavior; use .geminiignore to keep .env-style secrets out of memory files.
- Custom model endpoints may not support all native abilities (e.g., tools, long context); verify behavior consistency in a controlled repo before production.
Tips
- The personal Google account free tier is 1000 requests/day and it is open-source/forkable — the default choice for long-context (2M) and self-hosted use.
- For headless automation use `gemini -p -o json` with --approval-mode=yolo (preferred over the deprecated -y) for unattended tasks.
- Write build/test commands into GEMINI.md; Gemini auto-loads it each session — more stable than repeating prompts. Use .geminiignore to block .env etc.
- Past the 2M ceiling use /compress to keep going; use /save + /restore to preserve sessions across days.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Aug 5, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us