Gemini CLI Tutorial

Gemini CLI Tutorial: Command-Line AI Coding Tool for Developers

Gemini CLI is Google's open-source command-line tool that brings the power of Gemini AI models directly into your terminal. Designed for developers who prefer working in the command line, it enables AI-assisted coding, file analysis, shell command generation, and project-wide code understanding without leaving your terminal workflow.

Installation

Gemini CLI requires Node.js 18 or later. Install it globally using npm:

# Install Gemini CLI globally
npm install -g @anthropic-ai/gemini-cli

# Or use npx to run without installing
npx @google/gemini-cli

After installation, run gemini in your terminal to start the interactive session. On first launch, it will guide you through authentication with your Google account.

Authentication Options

Gemini CLI supports multiple authentication methods:

  1. Google Account (default): Sign in with your personal Google account for free tier access. The CLI opens a browser window for OAuth authentication.
  2. API Key: Set the GEMINI_API_KEY environment variable for headless environments or CI/CD pipelines.
  3. Vertex AI: For enterprise users, configure with your Google Cloud project credentials.
# Use an API key
export GEMINI_API_KEY="your-api-key-here"
gemini

# Or pass it directly
GEMINI_API_KEY="your-key" gemini
The free tier through Google Account authentication provides 60 requests per minute with Gemini 2.5 Pro, which is significantly more generous than the standard AI Studio free tier. This makes Gemini CLI an excellent choice for individual developers.

Core Usage Patterns

Interactive Mode

Start an interactive session by running gemini without arguments. You can ask questions, request code changes, analyze files, and more:

$ gemini
> Explain the architecture of the current project
> Find potential bugs in src/auth.js
> Refactor the database module to use connection pooling
> Write unit tests for the user service

Single Command Mode

For one-off tasks, pass your prompt directly:

# Ask a question
gemini "How do I implement JWT authentication in Express?"

# Analyze a file
gemini "Review src/server.js for security issues"

# Generate code
gemini "Create a Python script that monitors disk usage and sends email alerts"

Piping Input

Gemini CLI accepts piped input, making it easy to integrate with other command-line tools:

# Explain a git diff
git diff | gemini "Explain these changes"

# Analyze logs
tail -100 /var/log/app.log | gemini "What errors are occurring?"

# Review staged changes
git diff --staged | gemini "Review this code for issues"

Working with Files and Projects

Gemini CLI has built-in file system awareness. It can read, modify, and create files in your project:

# Let Gemini explore your project structure
> What is the file structure of this project?

# Ask it to modify files
> Add error handling to all API routes in src/routes/

# Create new files
> Create a Dockerfile for this Node.js application

# Multi-file analysis
> How does data flow from the API endpoint to the database?

Shell Command Generation

One of the most practical features is generating and explaining shell commands:

> What command finds all JavaScript files modified in the last week?
> How do I set up a cron job to run a backup script daily at 3 AM?
> Generate a docker-compose.yml for a Node.js app with Redis and PostgreSQL

Configuration with GEMINI.md

Create a GEMINI.md file in your project root to provide persistent context to the CLI. This file can contain project-specific instructions, coding standards, and architectural decisions:

# GEMINI.md
This is a Node.js Express API using TypeScript.
- Follow REST conventions for route naming
- Use Zod for input validation
- All database queries go through the repository pattern
- Tests use Jest with supertest for API testing

Useful Commands and Shortcuts

Gemini CLI can modify files on your system when you approve changes. Always review the proposed changes before accepting, especially when working on production code. Use version control to maintain a safety net.

Using Gemini CLI with a Custom API Endpoint

If you are using an API relay service like claude4u.com, you can configure Gemini CLI to route requests through the relay. This provides benefits like load balancing, usage tracking, and access from regions with restricted connectivity. Set the base URL in your environment or configuration to point to the relay endpoint, and use your relay API key for authentication.

Tips for Effective Use

  1. Be specific with your prompts. Instead of "fix this file," say "fix the null pointer exception on line 42 of src/parser.js."
  2. Use project context. Create a GEMINI.md file to help the CLI understand your project's conventions.
  3. Leverage piping. Combine Gemini CLI with grep, find, and other tools for powerful workflows.
  4. Review changes carefully. AI-generated code should always be reviewed before merging.
  5. Start small. Begin with code review and explanation tasks before moving to code generation.

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free