Copilot CLI Sessions, Scripts and Files
30-second answer
GitHub Copilot CLI runs two ways. An interactive session (copilot, or copilot -i "…" to start with a prompt) asks before each new tool, and a yes can last for the session; /exit saves it and copilot --continue or --resume brings it back. A non-interactive run (copilot -p "…") does one prompt and exits, so it can't ask: it only uses tools you pre-approve with --allow-tool (such as 'write' or 'shell(git)'), and --deny-tool always wins. Use it to generate scripts and tidy files, then read the result before you run it.
Exam objective: 2.2.4 Use GitHub Copilot CLI interactively and in sessions. 2.2.5 Generate scripts and manage files with GitHub Copilot CLI. (Skills measured as of August 7, 2026)
Simulations
Feel it before you read it. No account needed, nothing leaves this page.
<code>copilot -p "…"</code> runs <strong>one prompt</strong> and exits: good for scripts and CI. It can’t stop to ask, so tools it hasn’t been allowed are skipped.
👆 Click the glowing item on screen to continue
Read the steps as text
- copilot -p "…" runs one prompt and exits: good for scripts and CI. It can’t stop to ask, so tools it hasn’t been allowed are skipped.
- Allow exactly what it needs with --allow-tool and block what it must never do with --deny-tool. A deny always wins. --allow-all-tools exists, but use it only for tasks you trust.
- Read the script before you run it. Line 5 deletes old archives: is that what you want? Copilot’s output is a draft.
- In an interactive session Copilot asks before each new tool, and a yes lasts for the session. /exit saves the session; copilot --continue (or --resume) picks it up again.
🧪 Simulation for learning. Screens are simplified versions of Visual Studio Code and github.com with GitHub Copilot; names, files and suggestions are examples. Not affiliated with GitHub or Microsoft, and nothing here touches your real account.
🖱️ Drag to rotate · scroll or pinch to zoom
Agent mode
Agent works out which files to touch, edits them, runs commands and tests, reads errors and keeps going until the task is done. It asks before running terminal commands unless you’ve approved them.
🤔 What happens if…
Read this tour as text
- Ask mode. Ask answers questions about code and concepts and can suggest code blocks. It doesn’t change your files by itself.
- Edit mode (Copilot Edits). Edit applies changes to the files you choose, across several files at once. You see each diff and Keep or Undo it.
- Agent mode. Agent works out which files to touch, edits them, runs commands and tests, reads errors and keeps going until the task is done. It asks before running terminal commands unless you’ve approved them.
- MCP servers add tools. The Model Context Protocol (MCP) lets Copilot use external tools: the GitHub MCP server, a browser, a docs search or your own API. Configure servers in
.vscode/mcp.json(VS Code) or with/mcp(CLI). Organizations can restrict which servers are allowed. - Sessions and sub-agents. Long tasks fill the context window. Agent sessions keep work separate, and a sub-agent can do a side task in its own context and hand back only a summary.
- Copilot coding agent. Assign an issue to Copilot on github.com (or
/delegatefrom the CLI). The coding agent works in a GitHub Actions environment on its own branch and opens a draft pull request with its changes. - Review stays human. You review the agent’s PR like any other. Copilot code review can comment and suggest changes, but its review doesn’t count as an approval. A person merges.
- Copilot CLI. The same agent in the terminal:
copilotfor a session with slash commands,copilot -p "…"for one prompt, and--allow-tool/--deny-toolto control what it may run.
Let Copilot write a script and tidy files, but on your terms. Run one prompt non-interactively, see why it stops, give it exactly the permissions it needs and block the ones it doesn't.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 THE PROBLEM
You want Copilot in a script, but you don’t want it running anything it likes.
😊 THE SOLUTION
Run one-shot prompts with exact tool permissions, and resume sessions when you need them.
- No human to approve in CI✓ copilot -p with --allow-tool
- Fear of rm -rf✓ --deny-tool always wins
- Lost conversation✓ copilot --continue
Start with a question
Would you give a new assistant your house keys and say 'do whatever you think is needed' while you're away?
Probably not. You’d say “you can water the plants and take in the post, but don’t touch the car”. And if you’re at home, you’d rather they ask before each new job.
Copilot CLI works exactly like that. In an interactive session you’re at home: it asks before each new tool, like writing a file or running mv. In a non-interactive run (copilot -p) you’re away, so you hand over a written list: --allow-tool 'write', --deny-tool 'shell(rm)'. Anything not on the list, it doesn’t do.
Explain it like I’m 10
The technical explanation
Interactive sessions
$ copilot # open a session
$ copilot -i "organize the old log files into logs/archive" # open one with a first prompt
In a session, Copilot asks before it uses a tool it hasn’t been allowed:
? Allow Copilot to use shell(mv)? (y/n) y
✔ Moved 2 files to logs/archive/
The real prompt lets you approve that tool once or for the rest of the session, or say no and tell Copilot what to do instead (the exact wording changes between versions). Useful session commands:
/exitleaves and saves the session.copilot --continueresumes the most recent one;copilot --resumelets you pick. Inside a session,/resumeswitches and/sessionshows details./contextshows what’s filling the context window,/compactsummarises history to free space, and/clearstarts over./usageshows what the session used, including premium requests.!git statusruns a shell command yourself without leaving.
Non-interactive runs with -p
copilot -p "…" (or --prompt) runs one prompt and exits. That’s what you want in shell scripts, cron jobs and CI. It can’t stop to ask, so any tool you haven’t pre-approved is refused:
$ copilot -p "write a bash script that backs up logs/ and keeps 7 archives"
✖ Create scripts/backup-logs.sh: needs permission for "write".
Give it exactly what it needs:
copilot -p "write a bash script that backs up logs/ and keeps 7 archives" \
--allow-tool 'write' \
--deny-tool 'shell(rm)'
Tool patterns and the deny rule
| Pattern | Means |
|---|---|
'write' | Create or change files |
'shell' | Any shell command |
'shell(git)' | Only git commands |
'github(list_issues)' | One tool from an MCP server (server name, then tool) |
Two rules matter. --deny-tool always wins over an allow, so --allow-tool 'shell' --deny-tool 'shell(rm)' runs anything except rm. And --allow-all-tools approves everything: fine in a throwaway container you trust, risky on your laptop or in a CI job that holds secrets.
-
1 Denied?
If a --deny-tool pattern matches, the tool is refused. Full stop.
-
2 Allowed?
If --allow-tool or a session approval matches, it runs.
-
3 Interactive?
In a session, Copilot asks you.
-
4 Programmatic
With -p there is nobody to ask, so the tool is refused.
Scripts and files: review the draft
Here’s the kind of script you might get:
#!/usr/bin/env bash
set -euo pipefail
mkdir -p backups
tar -czf "backups/logs-$(date +%F).tar.gz" logs/
ls -1t backups/logs-*.tar.gz | tail -n +8 | xargs -r rm --
It looks fine, but the last line deletes files. Read it, check it keeps seven archives as you asked, run it once against a test folder, then commit it so someone reviews it. The same goes for file moves and renames: check git status afterwards and make sure nothing went missing.
Architect level Configuration, custom agents and MCP in scripted runs
Copilot CLI keeps its settings in ~/.copilot/: settings.json, mcp-config.json for MCP servers, and agents/ for personal custom agents. A repository can add its own agents in .github/agents/*.agent.md and instructions in .github/copilot-instructions.md or AGENTS.md, which the CLI reads. In a script you can combine them: copilot --agent=test-writer -p "write tests for src/discount.js" --allow-tool 'write' --model <model>. MCP tools follow the same permission rules, so allow them by name, for example --allow-tool 'github(list_issues)', rather than opening everything.
Real-world business scenario
Contoso (fictional)
- The problem
- Contoso wanted a nightly job that writes release notes from merged pull requests. The first attempt used --allow-all-tools on a runner holding deployment secrets, and security blocked it.
- What they used
- A scheduled job running copilot -p with a fine-grained token, --allow-tool 'write' and 'shell(git)', and --deny-tool 'shell(curl)' and 'shell(rm)'. The output goes into a pull request, not straight to main.
- Why that choice
- The job gets exactly the tools it needs, a deny always wins, and a person reviews the notes before they're merged.
- The result
- Release notes now arrive every morning, and the security team approved the job because its permissions fit on one line.
When to use it and when not to
✓ Use it when…
- Interactive sessions for exploring a repo, tidying files and multi-step work you want to watch.
- copilot -p in scripts and CI with the smallest set of --allow-tool patterns.
- --continue or --resume to pick up a long task without explaining it again.
✕ Think twice when…
- --allow-all-tools on a machine with secrets or production access.
- Running a generated script or bulk file move without reading it first.
- One endless session for unrelated work. Use /compact, /clear or a new session.
↔ Alternatives to consider
- /delegate to hand a bigger task to Copilot coding agent, which works on its own branch and opens a draft PR.
- Agent mode in VS Code when you want to see diffs in the editor.
Test yourself with exercises
5 original practice questions in the GH-300 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.
Interview preparation
Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.
Beginner What's the difference between copilot and copilot -p?
Model answer
Plain copilot opens an interactive session where I chat, approve tools as they come up and keep going. copilot -p runs one prompt and exits, which suits scripts and CI. Because it can't stop to ask, it only uses tools I've allowed on the command line.
Intermediate You want Copilot CLI to write a file in a CI job but never delete anything. How?
Model answer
copilot -p with --allow-tool 'write' and --deny-tool 'shell(rm)'. The deny wins even if something broader is allowed. I avoid --allow-all-tools in CI because then it can run anything the job's account can.
Interviewer follows up
What happens if you forget --allow-tool 'write'?
You
It can't write the file. In programmatic mode there's nobody to ask, so the tool is refused and it tells you it needed permission.
Experienced developer A long Copilot CLI session is getting slow and the answers drift. What are your options?
Model answer
/context shows what's filling the window. /compact summarises the history so I keep the thread without the bulk. If the topic has changed, /clear or a fresh session is cleaner. For a side task, a sub-agent or /delegate keeps that work out of my main context.
Scenario Copilot CLI wrote a backup script that deletes old archives. Do you run it?
Model answer
Not before I read it. I check what it deletes, whether paths are quoted, and that set -euo pipefail is there. I'd run it once against a test folder, then commit it so the change is reviewed like any other code.
Quick revision
Key points
- copilot starts an interactive session; copilot -i "prompt" starts one with a first prompt.
- copilot -p "prompt" runs once and exits; it can only use pre-approved tools.
- --allow-tool and --deny-tool take patterns: 'write', 'shell', 'shell(git)', '<mcp-server>(tool)'. Deny wins.
- --allow-all-tools approves everything. Use it only where you trust the task and the environment.
- In a session, approve a tool once or for the rest of the session.
- /exit saves the session; --continue resumes the latest, --resume picks one; /resume and /session work inside.
- /context and /compact manage the context window.
- Generated scripts and file moves are drafts. Read and test them.
Important terminology
- Interactive session
- A Copilot CLI conversation started with copilot or copilot -i, where it can ask you to approve tools as it goes.
- Programmatic mode
- Running one prompt with copilot -p "..." that exits when done and can't stop to ask for approval.
- --allow-tool
- A flag that pre-approves a tool, for example 'write', 'shell(git)' or an MCP server's tool.
- --deny-tool
- A flag that blocks a tool. A deny always beats an allow.
- --continue
- Resumes the most recent Copilot CLI session. --resume lets you pick an earlier one.
- /compact
- Summarises the conversation so far to free space in the context window without starting over.
Common mistakes
- Expecting copilot -p to pause and ask for permission.
- Using --allow-all-tools in CI.
- Running a generated script before reading it.
Exam tips
- "Run Copilot CLI in a script with no prompts" = copilot -p plus --allow-tool.
- "Block a command even if other tools are allowed" = --deny-tool (deny wins).
- "Pick up yesterday's conversation" = copilot --continue or --resume.
Five quick revision questions
-
1.Which flag runs a single prompt non-interactively?Show
-p (or --prompt)
-
2.Which wins, --allow-tool or --deny-tool?Show
--deny-tool
-
3.Which tool pattern allows only git commands?Show
shell(git)
-
4.How do you resume the most recent session?Show
copilot --continue
-
5.Which slash command summarises history to free context space?Show
/compact
Official references
Finished this lesson? Tick it off. Your progress is saved in this browser.
Next up: Agent Mode, MCP Servers and Sub-Agents
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.