Login
Back to Blog
Gemini CLI 用户指南 - 在终端中使用 Google AI

Gemini CLI 用户指南 - 在终端中使用 Google AI

C
Crazyrouter Team
January 24, 2026
30 views中文Tutorial
Share:

Gemini CLI 是 Google 提供的开源命令行 AI 工具,可以将 Gemini 的能力直接带到你的终端中。它提供了一种轻量级方式访问 Gemini,实现从提示到模型的最直接路径。通过配置代理,即使在网络受限的环境中你也可以使用 Gemini CLI 服务。

为什么选择 Gemini CLI?#

  • 强大的 Gemini 2.5 Pro:可使用 100 万 token 的上下文窗口
  • 内置工具:Google Search grounding、文件操作、Shell 命令、网页抓取
  • 可扩展:支持 MCP(Model Context Protocol)进行自定义集成
  • 终端优先:专为在命令行工作的开发者设计
  • 开源:Apache 2.0 License

安装#

快速安装#

使用 npx 即刻运行(无需安装)#

bash
# Use official repository
npx https://github.com/google-gemini/gemini-cli

使用 npm 全局安装#

bash
npm install -g @google/gemini-cli

使用 Homebrew 安装(macOS/Linux)#

bash
brew install gemini-cli

系统要求#

  • Node.js 版本 20 或更高
  • macOS、Linux 或 Windows

配置代理#

设置环境变量#

在使用代理之前,先配置以下环境变量:

Linux / macOS#

bash
# Set API Key
export GEMINI_API_KEY=sk-your-api-key-here

# Set API Proxy URL
export GOOGLE_GEMINI_BASE_URL=https://crazyrouter.com/

Windows CMD#

cmd
set GEMINI_API_KEY=sk-your-api-key-here
set GOOGLE_GEMINI_BASE_URL=https://crazyrouter.com/

Windows PowerShell#

powershell
$env:GEMINI_API_KEY = "sk-your-api-key-here"
$env:GOOGLE_GEMINI_BASE_URL = "https://crazyrouter.com/"

提示:将这些环境变量添加到你的 .bashrc.zshrc 或其他 shell 配置文件中,这样每次打开终端时都会自动生效。

快速开始#

基本用法#

在当前目录启动#

bash
gemini

包含多个目录#

bash
gemini --include-directories ../lib,../docs

使用指定模型#

bash
gemini -m gemini-2.5-flash

非交互脚本模式#

获取简单文本回复:

bash
gemini -p "Explain the architecture of this codebase"

获取结构化 JSON 输出:

bash
gemini -p "Explain the architecture of this codebase" --output-format json

快速示例#

启动新项目#

bash
cd new-project/
gemini
> Write me a Discord bot that answers questions using the FAQ.md file I provided

分析现有代码#

bash
git clone https://github.com/google-gemini/gemini-cli
cd gemini-cli
gemini
> Give me a summary of all changes from yesterday

主要功能#

代码理解与生成#

  • 查询并编辑大型代码仓库
  • 利用多模态能力,从 PDF、图像或草图生成新应用
  • 使用自然语言进行调试和问题排查

自动化与集成#

  • 自动化日常运维任务,例如查询 pull requests 或处理复杂 rebase
  • 通过 MCP servers 连接新的能力
  • 在脚本中以非交互方式运行,实现工作流自动化

高级功能#

  • 使用内置的 Google Search 获取实时信息
  • 会话检查点,用于保存和恢复复杂会话
  • 自定义上下文文件(GEMINI.md),根据你的项目调整行为

常用命令与示例#

探索代码仓库#

code
> Describe the main components of this system architecture
> What security mechanisms are in place?
> Provide a step-by-step onboarding document for new developers

处理现有代码#

code
> Implement a first draft for GitHub issue #123
> Help me migrate this codebase to the latest version of Java. Start with a plan

自动化工作流#

code
> Create a slideshow showing the git history from the past 7 days, grouped by feature and team member
> Create a full-screen web app for wall display showing our most interactive GitHub issues

系统交互#

code
> Convert all images in this directory to png and rename them using the date from exif data
> Organize my PDF invoices by spending month

GitHub 集成#

使用 Gemini CLI GitHub Action 将 Gemini CLI 直接集成到你的 GitHub 工作流中:

  • Pull Request 审查:基于上下文的自动化代码审查与建议
  • Issue 分诊:根据内容分析自动为 GitHub issues 打标签并设定优先级
  • 按需帮助:在 issues 和 pull requests 中 @@gemini-cli,获取调试、解释或任务分配方面的帮助
  • 自定义工作流:构建自动化、定时及按需触发的工作流,以满足团队的特定需求

故障排查#

如果在使用过程中遇到问题,可以参考以下步骤:

1. 检查环境变量#

bash
echo $GEMINI_API_KEY
echo $GOOGLE_GEMINI_BASE_URL

2. 检查网络连接#

bash
curl -I https://crazyrouter.com/

3. 检查错误信息#

错误信息通常会指明问题所在。常见问题包括:

  • API key 格式无效
  • 网络连接问题
  • 代理配置不正确

4. SOCKS 代理格式#

如果使用 SOCKS 代理,请确保格式正确:

code
socks5://<user>:<pass>@<proxy>:<port>

5. 提交 Bug#

使用内置命令报告问题:

bash
/bug

版本发布#

Preview#

新的 preview 版本会在每周二 UTC 23:59 发布:

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

Stable#

新的 stable 版本会在每周二 UTC 20:00 发布:

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

Nightly#

Nightly 版本会在每天 UTC 00:00 发布:

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

MCP Server 使用#

~/.gemini/settings.json 中配置 MCP servers,为 Gemini CLI 扩展自定义工具:

text
> @github list my open pull requests
> @slack send a summary of today's commits to #dev channel
> @database run a query to find inactive users

通过 Crazyrouter 的定价#

以折扣价格访问 Gemini 模型:

ModelInput PriceOutput Price
gemini-2.5-pro$0.625/1M tokens$2.50/1M tokens
gemini-2.5-flash$0.038/1M tokens$0.15/1M tokens
gemini-2.5-flash-lite$0.019/1M tokens$0.075/1M tokens

所有价格相较官方定价均包含 45% 的折扣。

相关资源#


需要帮助?请联系支持团队,或查阅 Crazyrouter documentation 以获取更多信息。

Related Articles