ogt-cli-gemini

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Gemini CLI Skill

Gemini CLI 技能

Run Google's Gemini CLI to leverage Gemini 3 models directly from the terminal.
运行Google的Gemini CLI,直接在终端中调用Gemini 3模型。

When to Use

适用场景

  • Code understanding and generation
  • Multi-file analysis and refactoring
  • Debugging with natural language
  • Research with Google Search grounding
  • Automation and scripting tasks
  • Tasks benefiting from 1M token context window
  • 代码理解与生成
  • 多文件分析与重构
  • 自然语言调试
  • 基于Google搜索的研究工作
  • 自动化与脚本任务
  • 需要100万token上下文窗口的任务

Quick Start

快速开始

Interactive mode

交互模式

bash
gemini
bash
gemini

One-shot query (non-interactive)

单次查询(非交互模式)

bash
gemini -p "Your prompt here"
bash
gemini -p "Your prompt here"

With specific model

指定模型

bash
gemini -p "Complex task" -m gemini-2.5-pro
gemini -p "Quick task" -m gemini-2.5-flash
bash
gemini -p "Complex task" -m gemini-2.5-pro
gemini -p "Quick task" -m gemini-2.5-flash

JSON output (for parsing)

JSON格式输出(用于解析)

bash
gemini -p "Your prompt" --output-format json
bash
gemini -p "Your prompt" --output-format json

Stream JSON (real-time events)

流式JSON输出(实时事件)

bash
gemini -p "Run tests and deploy" --output-format stream-json
bash
gemini -p "Run tests and deploy" --output-format stream-json

Installation

安装方法

bash
undefined
bash
undefined

npm (recommended)

npm(推荐方式)

npm install -g @google/gemini-cli
npm install -g @google/gemini-cli

Homebrew (macOS/Linux)

Homebrew(macOS/Linux)

brew install gemini-cli
brew install gemini-cli

Run without installing

无需安装直接运行

npx @google/gemini-cli
undefined
npx @google/gemini-cli
undefined

Key Options

核心选项

OptionDescription
-p, --prompt
Non-interactive mode, print response and exit
-m, --model
Model:
gemini-2.5-flash
,
gemini-2.5-pro
--output-format
text
(default),
json
,
stream-json
--include-directories
Include additional directories
--yolo
Auto-approve tool calls (use with caution)
--sandbox
Run in sandbox mode (safer)
选项说明
-p, --prompt
非交互模式,输出响应后退出
-m, --model
模型选项:
gemini-2.5-flash
,
gemini-2.5-pro
--output-format
text
(默认),
json
,
stream-json
--include-directories
包含额外的目录
--yolo
自动批准工具调用(谨慎使用)
--sandbox
在沙箱模式下运行(更安全)

Authentication

认证方式

Option 1: Google OAuth (Recommended)

方式1:Google OAuth(推荐)

bash
gemini
bash
gemini

Follow browser auth flow on first run

首次运行时按照浏览器中的认证流程操作

- **Free tier:** 60 req/min, 1,000 req/day
- No API key needed
- **免费层级:** 每分钟60次请求,每天1000次请求
- 无需API密钥

Option 2: Gemini API Key

方式2:Gemini API密钥

bash
export GEMINI_API_KEY="YOUR_API_KEY"
gemini
bash
export GEMINI_API_KEY="YOUR_API_KEY"
gemini

Option 3: Vertex AI (Enterprise)

方式3:Vertex AI(企业版)

bash
export GOOGLE_API_KEY="YOUR_API_KEY"
export GOOGLE_GENAI_USE_VERTEXAI=true
gemini
bash
export GOOGLE_API_KEY="YOUR_API_KEY"
export GOOGLE_GENAI_USE_VERTEXAI=true
gemini

Working with Files

文件操作

Gemini CLI can read and work with files in the current directory:
bash
undefined
Gemini CLI可以读取并处理当前目录下的文件:
bash
undefined

Analyze codebase

分析代码库

cd /path/to/project && gemini -p "Explain this codebase architecture"
cd /path/to/project && gemini -p "Explain this codebase architecture"

Include multiple directories

包含多个目录

gemini --include-directories ../lib,../docs -p "Review the API"
gemini --include-directories ../lib,../docs -p "Review the API"

Pipe file content

管道传输文件内容

cat myfile.py | gemini -p "Review this code"
undefined
cat myfile.py | gemini -p "Review this code"
undefined

Built-in Tools

内置工具

Gemini CLI has built-in capabilities:
  • Google Search: Ground responses with real-time web data
  • File Operations: Read, write, edit files
  • Shell Commands: Execute terminal commands
  • Web Fetching: Retrieve content from URLs
Gemini CLI具备以下内置功能:
  • Google搜索: 结合实时网络数据生成响应
  • 文件操作: 读取、写入、编辑文件
  • Shell命令: 执行终端命令
  • 网页抓取: 获取URL中的内容

For Sub-Agent Delegation

子代理任务分发

When spawning Gemini CLI for background work:
bash
undefined
当启动Gemini CLI执行后台任务时:
bash
undefined

Run with timeout

设置超时时间运行

timeout 300 gemini -p "Complete this task..." 2>&1
timeout 300 gemini -p "Complete this task..." 2>&1

Capture structured output

捕获结构化输出

gemini -p "Generate a report" --output-format json > result.json
gemini -p "Generate a report" --output-format json > result.json

Auto-approve for automation (careful!)

自动化场景下自动批准操作(谨慎使用!)

gemini -p "Run the build" --yolo
undefined
gemini -p "Run the build" --yolo
undefined

Script: Run Gemini Task

脚本:运行Gemini任务

Use the bundled script for reliable sub-agent execution:
bash
node {baseDir}/scripts/run-gemini-task.cjs "Your task prompt" [options]
Options:
  • --model <model>
    — Model to use (default: gemini-2.5-flash)
  • --timeout <secs>
    — Timeout in seconds (default: 300)
  • --json
    — Output in JSON format
  • --workdir <path>
    — Working directory
The script handles:
  • Timeout protection
  • Error capture and formatting
  • Clean output for parsing
  • Exit code propagation
使用内置脚本实现可靠的子代理执行:
bash
node {baseDir}/scripts/run-gemini-task.cjs "Your task prompt" [options]
选项说明:
  • --model <model>
    — 使用的模型(默认:gemini-2.5-flash)
  • --timeout <secs>
    — 超时时间(秒,默认:300)
  • --json
    — 以JSON格式输出
  • --workdir <path>
    — 工作目录
该脚本支持:
  • 超时保护
  • 错误捕获与格式化
  • 便于解析的纯净输出
  • 退出码传递

Model Selection

模型选择

ModelBest For
gemini-2.5-flash
Fast, cost-effective, everyday tasks
gemini-2.5-pro
Complex reasoning, large context, nuanced tasks
模型最佳适用场景
gemini-2.5-flash
快速、高性价比的日常任务
gemini-2.5-pro
复杂推理、大上下文、精细化任务

Context File (GEMINI.md)

上下文文件(GEMINI.md)

Create a
GEMINI.md
file in your project root to customize behavior:
markdown
undefined
在项目根目录创建
GEMINI.md
文件,自定义Gemini的行为:
markdown
undefined

Project: My App

Project: My App

Guidelines

Guidelines

  • Use TypeScript for all new code
  • Follow existing patterns in src/
  • Run tests before committing
  • Use TypeScript for all new code
  • Follow existing patterns in src/
  • Run tests before committing

Architecture

Architecture

  • Frontend: React + Vite
  • Backend: Node.js + Express

Gemini will automatically read this for project context.
  • Frontend: React + Vite
  • Backend: Node.js + Express

Gemini会自动读取该文件获取项目上下文信息。

Tips

使用技巧

  1. Use
    -p
    for scripts
    — Non-interactive mode for automation
  2. Set timeouts for long-running tasks
  3. Use
    --output-format json
    when parsing results programmatically
  4. Working directory matters — Gemini sees files relative to cwd
  5. Use
    --yolo
    carefully
    — Auto-approves all tool calls
  6. Google Search grounding — Great for real-time info queries
  1. 脚本中使用
    -p
    选项
    — 非交互模式适用于自动化场景
  2. 为长时间运行的任务设置超时时间
  3. 编程解析结果时使用
    --output-format json
    选项
  4. 注意工作目录 — Gemini会基于当前工作目录识别文件
  5. 谨慎使用
    --yolo
    选项
    — 会自动批准所有工具调用
  6. Google搜索 grounding — 非常适合实时信息查询

Rate Limits (Free Tier)

免费层级速率限制

  • 60 requests per minute
  • 1,000 requests per day
  • 1M token context window
  • 每分钟60次请求
  • 每天1000次请求
  • 100万token上下文窗口

Comparison with Claude CLI

与Claude CLI的对比

FeatureGemini CLIClaude CLI
Free tier✅ 1000/day✅ Limited
Context window1M tokens200K tokens
Google Search✅ Built-in
Shell execution✅ Built-in✅ Built-in
MCP support
AuthGoogle OAuthAnthropic OAuth
功能Gemini CLIClaude CLI
免费层级✅ 每天1000次✅ 有限额度
上下文窗口100万token20万token
Google搜索✅ 内置
Shell命令执行✅ 内置✅ 内置
MCP支持
认证方式Google OAuthAnthropic OAuth

Troubleshooting

故障排查

Authentication Issues

认证问题

bash
undefined
bash
undefined

Clear cached auth

清除缓存的认证信息

rm -rf ~/.gemini-cli
rm -rf ~/.gemini-cli

Re-authenticate

重新认证

gemini
undefined
gemini
undefined

Command Not Found

命令未找到

bash
undefined
bash
undefined

Check installation

检查安装情况

which gemini
which gemini

Reinstall

重新安装

npm install -g @google/gemini-cli
undefined
npm install -g @google/gemini-cli
undefined