Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions Using-GitHub-Copilot-CLI/1-Install-and-Modes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Module 1: Install and Modes

[← Back to overview](README.md) | [Next: Tool Approval and Context →](2-Tool-Approval-and-Context.md)

---

### 🗒️ Section 1: Install and Verify the GitHub Copilot CLI

🎯 **Learning Goals**
- Install the GitHub Copilot CLI

The **GitHub Copilot CLI** (`copilot`) is a standalone terminal application — distinct from the old `gh copilot` extension — that opens a full interactive AI chat session in your terminal. It ships with the GitHub MCP server built in, supports plan mode and auto model selection, and can autonomously read files, run commands, and interact with GitHub.com on your behalf.

> **Note**: The `gh extension install github/gh-copilot` extension was deprecated in September 2025 and archived at v1.2.0. This workshop uses the new standalone `copilot` CLI.

#### Prerequisites

This workshop uses two separate CLI tools:

- **`copilot`** — the standalone GitHub Copilot CLI (installed below)
- **`gh`** — the GitHub CLI, required for `gh agent-task` commands in Sections 4 and 6

**NOTE:** To complete 4 and 6, the Copilot Free tier will need to be upgraded to allow for Agent Mode

Install `gh` first if you don't already have it:

**Windows:**
```powershell
winget install GitHub.CLI
```
**macOS / Linux:**
```bash
brew install gh
```
Then authenticate: `gh auth login`

---

#### Installing the Copilot CLI

1. Choose the installation method for your platform:

**Windows** (WinGet):
```powershell
winget install GitHub.Copilot
```

**macOS / Linux** (Homebrew):
```bash
brew install copilot-cli
```

**Any platform** (pip):
```bash
pip install github-copilot-cli
```

**macOS / Linux** (install script):
```bash
curl -fsSL https://gh.io/copilot-install | bash
```

2. Verify the installation:

```bash
copilot --version
```

3. Launch the CLI from the root of the sample project. On first launch you will see an animated welcome banner and be prompted to log in if you are not already authenticated:

```bash
copilot
```

If prompted, use the `/login` slash command and follow the on-screen instructions to authenticate with your GitHub account.

Alternatively, authenticate headlessly using a fine-grained Personal Access Token (PAT) with the **Copilot Requests** permission set in the `GH_TOKEN` or `GITHUB_TOKEN` environment variable:

```bash
export GH_TOKEN=<your-pat>
copilot
```

4. Keep the CLI up to date using the same package manager you used to install it. For example:

```bash
brew upgrade copilot-cli # macOS/Linux via Homebrew
winget upgrade GitHub.Copilot # Windows via WinGet
pip install --upgrade github-copilot-cli # pip
```

In the above exercises we achieved the following:
- ✅ Installed the standalone GitHub Copilot CLI
- ✅ Authenticated and launched an interactive session
- ✅ Understood the distinction from the deprecated `gh copilot` extension

---

### 🗒️ Section 2: Interactive Modes — Default, Plan, and Autopilot

🎯 **Learning Goals**
- Use the default ask/execute mode for general coding tasks
- Switch to plan mode to build a structured implementation plan before writing code
- Enable Auto model selection to reduce rate limiting and simplify model management
- Navigate the interactive interface using slash commands

The GitHub Copilot CLI interactive session (launched with `copilot`) offers three modes. You cycle between them with `Shift+Tab`.

#### Default (Ask/Execute) Mode

In default mode Copilot responds to each prompt, asks clarifying questions when needed, and requests your approval before modifying files or running commands.

1. Launch the CLI from the sample project root:

```bash
copilot
```

2. Ask Copilot a question about the project to confirm it has context:

```
Explain the overall structure of this Python project and what each file contains.
```

Copilot reads the files in the current directory and responds with a summary.

3. Ask it to make a small change — for example, add a `/ping` endpoint. Copilot will propose the change and ask for your approval before writing to disk:

```
Add a GET /ping endpoint to the FastAPI app that returns {"status": "ok"} with HTTP 200.
```

When Copilot requests permission to edit `main.py`, choose **Yes** to allow the change for this action only, or **Yes, and approve for the rest of this session** to skip future prompts for this file tool.

#### Plan Mode

Plan mode is ideal for larger or less well-defined tasks. Instead of immediately writing code, Copilot analyses your request, asks clarifying questions, and produces a structured step-by-step implementation plan. No code is written until you approve the plan.

4. Press `Shift+Tab` to cycle to **Plan** mode. You will see the mode indicator change in the prompt.

5. Give Copilot a multi-step task:

```
Refactor the token generation endpoint to use a service class with dependency injection, add CRUD operations for a User entity with Pydantic models, and write pytest tests for both.
```

Copilot will ask clarifying questions — for example, which test framework to use, whether to use in-memory or mocked repositories — before producing a numbered plan.

6. Review the plan. You can ask Copilot to revise a specific step before it begins:

```
Change step 3 to use pytest-mock instead of unittest.mock for mocking.
```

Once you are satisfied, confirm the plan and Copilot will execute each step sequentially, requesting tool approval as needed.

#### Auto Model Selection

Instead of manually picking a model every session, you can let Copilot automatically choose the best available model on your behalf. This reduces rate limiting and removes the mental overhead of model selection. Auto model selection is available on all Copilot plans.

7. Open the model picker and choose **Auto**:

```
/model
```

Select **Auto** from the list. With Auto enabled, Copilot picks from a pool of 1× multiplier models — currently GPT-4.1, GPT-5 mini, GPT-5.2-Codex, GPT-5.3-Codex, Claude Haiku 4.5, and Claude Sonnet 4.5 — subject to your organization's policies and your subscription. The pool changes over time as new models become available.

> **Note**: Auto will never select models excluded by administrator policy, models with a premium multiplier greater than 1×, or models unavailable on your plan.

> **Paid plan tip**: When using Auto on a paid plan (Pro, Pro+, Business, Enterprise), qualifying models receive a **10% multiplier discount** compared to selecting the same model manually.

> **Coding agent note**: Auto model selection for `gh agent-task` (Copilot coding agent) is generally available for **Pro and Pro+ plans only**.

8. To see which model handled a response, check the CLI output — the model used is reported alongside each reply.

9. Override Auto at any time by picking a specific model from `/model`, or set one at launch:

```bash
copilot --model claude-sonnet-4.6
```

In the above exercises we achieved the following:
- ✅ Used default mode for interactive ask/execute tasks
- ✅ Used plan mode to design a multi-step implementation before writing code
- ✅ Enabled Auto model selection for automatic model choice

---

[← Back to overview](README.md) | [Next: Tool Approval and Context →](2-Tool-Approval-and-Context.md)
102 changes: 102 additions & 0 deletions Using-GitHub-Copilot-CLI/2-Tool-Approval-and-Context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Module 2: Tool Approval and Context Management

[← Previous: Install and Modes](1-Install-and-Modes.md) | [Next: GitHub Integration and Workflows →](3-GitHub-Integration-and-Workflows.md)

---

### 🗒️ Section 3: Tool Approval and Context Management

🎯 **Learning Goals**
- Understand how tool approval protects you from unintended changes
- Use `--allow-tool`, `--deny-tool`, and `--allow-all-tools` to configure automatic approval
- Use programmatic mode with `-p` for scripted and headless workflows
- Manage conversation context with `/compact`, `/context`, and auto-compaction

#### Tool Approval

Every time Copilot wants to modify a file or execute a shell command it asks for your approval. This keeps you in control — but for trusted, repetitive tasks you can pre-approve specific tools.

1. Launch a new session and ask Copilot to run the project's tests:

```bash
copilot
```

```
Run all the tests in the project and show me a summary of the results.
```

When Copilot asks permission to run `pytest`, notice the three options presented:

```
1. Yes
2. Yes, and approve pytest for the rest of this session
3. No, and tell Copilot what to do differently (Esc)
```

Choose **2** to approve `pytest` for the rest of the session.

2. Use the `--allow-tool` flag to pre-approve specific commands at launch. This is useful for CI scripts or trusted tasks:

```bash
copilot --allow-tool 'shell(pytest)' --allow-tool 'write'
```

- `shell(COMMAND)` — approves a specific shell command. Omit the command name to allow all shell commands.
- `write` — approves all file modifications without individual prompts.

3. Use `--deny-tool` to block specific commands even when `--allow-all-tools` is set. For example, to allow everything except `rm` and `git push`:

```bash
copilot --allow-all-tools --deny-tool 'shell(rm)' --deny-tool 'shell(git push)'
```

`--deny-tool` always takes precedence over `--allow-tool` and `--allow-all-tools`.

#### Programmatic Mode

For headless use in scripts and CI pipelines, pass a prompt directly with `-p` instead of opening an interactive session:

4. Run a single task non-interactively:

```bash
copilot -p "Show me this week's commits and summarize them" --allow-tool 'shell(git)'
```

Copilot completes the task and exits. You can also pipe options from a script:

```bash
echo "Run pytest and report any failures" | copilot --allow-tool 'shell(pytest)'
```

#### Context Management

For long sessions working on large codebases, the conversation history can approach the model's token limit. The CLI manages this automatically and gives you manual control.

5. Check how much of your context window is in use at any time:

```
/context
```

This shows a token usage breakdown split by system prompt, conversation history, and available remaining tokens.

6. When you want to free up context without ending the session, run:

```
/compact
```

Copilot compresses the conversation history while retaining key facts about the tasks completed so far. Press `Escape` to cancel if you change your mind.

7. Auto-compaction kicks in automatically when the session approaches **95%** of the token limit, compressing history in the background without interrupting your workflow. This enables virtually unlimited session length — you can work on a feature from start to finish without restarting.

In the above exercises we achieved the following:
- ✅ Understood per-action tool approval prompts
- ✅ Pre-approved tools with `--allow-tool` and blocked tools with `--deny-tool`
- ✅ Used programmatic mode for headless CLI invocations
- ✅ Monitored and managed conversation context with `/context` and `/compact`

---

[← Previous: Install and Modes](1-Install-and-Modes.md) | [Next: GitHub Integration and Workflows →](3-GitHub-Integration-and-Workflows.md)
Loading
Loading