---
title: The terminal & git
summary: The two tools every agent uses constantly, and the minimum you need in order to supervise them.
section: foundations
phase: build
order: 2
last_reviewed: 2026-11-20
---

# The terminal & git

Agents live in the terminal and leave their work behind in git. If you can read both, you can see exactly what the agent did and undo it.

## You'll be able to
- Read the commands an agent runs and know which ones are dangerous
- Use git as a safety net: checkpoint, review, revert
- Manage dependencies and scripts confidently

## The mental model

The terminal is a text interface for telling the computer what to do. Every command runs with your permissions, in a particular folder. Git is a timeline of snapshots of your project, like save points in a game. Put them together and you get the whole agent workflow: the agent runs commands and edits files, and git shows exactly what changed and lets you return to a save point you made on purpose.

## Key concepts

### Terminal
- **Shell:** The program that interprets commands (zsh, bash). The **working directory** is where commands run.
- **Environment variables:** Key-value settings for a process (`PATH`, `DATABASE_URL`).
- **Package managers:** `npm`/`pnpm`/`bun` (JS), `pip`/`uv` (Python), `cargo` (Rust), `brew` (macOS). The **lockfile** pins exact versions. Commit it.
- **Scripts:** `package.json` → `"scripts"` (or a `Makefile`/`justfile`) define standard commands: `dev`, `test`, `build`, `lint`.

### Git
- **Commit:** A snapshot of the project. **Branch:** A parallel line of work. **Remote:** The copy on GitHub.
- **Diff:** What changed. *This is how you review an agent's work.*
- **Pull request (PR):** A proposed change, reviewed before it's merged into `main`.
- **Worktree:** A second working copy of the same repo, so you can run parallel agent sessions without them stepping on each other.
- **.gitignore:** Files that should never be committed (`node_modules`, `.env`, build output).

## Decisions you make (not the agent)
| Decision | Sensible default | Choose otherwise when… |
|---|---|---|
| Branching | One branch per feature, PR into `main` | Solo prototype (committing straight to main is fine) |
| Checkpointing | Commit before every agent session | Never skip it |
| Agent permissions | Allow reads and tests freely; require approval for installs, deletes, pushes | Sandboxed or containerized environment |

## What good output looks like

- Commands are narrow and reversible: one file or folder, not a wildcard over the whole project
- The agent checks `git status` and `git diff` before it commits, and commits small, focused changes
- Commit messages explain why, not just what
- Nothing secret is staged, and it works on a branch rather than directly on `main`

## Red flags (commands to pause on)
- `rm -rf`, `git reset --hard`, `git push --force`, `git clean -fd`
- `curl … | sh` from an unknown URL
- `chmod 777`, or anything using `sudo`
- Installing a package you've never heard of → check it on the registry first
- Committing `.env` or credentials
- **Common agent mistakes:** `git add -A` or `git add .` stages everything that isn't ignored, including a `.env` file if it isn't in `.gitignore`. `git reset --hard` overwrites your files with the last commit, so uncommitted work is gone. `git clean -fdx` also removes ignored files such as `.env` and `node_modules`. Force-pushing to "fix" a conflict rewrites shared history.

## Worked example

**Task:** You ask an agent to fix a typo bug in two files, and you start the session without committing first.

When it finishes, `git status` shows 14 changed files. The agent also "tidied" formatting across the project and, when its own changes broke the build, ran `git reset --hard` to start over. That command also overwrote the edits you had made by hand before the session.

**What a good result contains:** you committed (or branched) before the session. The agent's work sits on its own branch, the diff touches the two files that matter, and `git diff --stat` matches the size of the task.

**The wrong turn:** two things went wrong. There was no checkpoint, and a command that overwrites the working tree was allowed to run unattended.

**How review catches it:** look at `git status` and `git diff --stat` before reading any code. A two-file task with 14 changed files is a red flag. Keep the good changes with `git add -p`, which lets you pick the hunks to stage, and discard the rest with `git restore <file>`, which replaces your working copy of that file with the committed or staged version. Treat that as final, because git doesn't promise to get discarded edits back. Next time, commit first, give the agent its own branch or worktree, and require approval for any command that discards work.

## Sample prompts

```text
Summarize the uncommitted changes in this repo: list each file, what changed and
why it might have changed, and flag anything risky (deleted files, config
changes, new dependencies, secrets).
```
*Why it works:* A summary of the uncommitted changes shows you the whole footprint of a session before you commit any of it.

```text
Write a clear commit message for the staged changes using Conventional Commits
(feat/fix/chore…). Subject under 72 chars, body explains why.
```
*Why it works:* A message that explains why leaves a history you can actually use when something breaks later.

### Set up a safety net before the session
```text
Before you change anything, run git status. If there are uncommitted changes, stop and tell me. Otherwise create a branch named agent/<task> and do all your work there. Do not commit to main, and do not run any command that discards changes or rewrites history without asking me first.
```
*Why it works:* It makes the checkpoint and the boundary part of the task, so a bad session is one branch to delete.

### Ask before anything destructive
```text
Before running any command that can discard work (git reset --hard, git restore, git checkout -- <file>, git clean, git push --force, rm -rf), tell me exactly what it would remove. Use a dry run where one exists (for example git clean -n) and show me the output. Wait for my OK.
```
*Why it works:* A dry run turns a guess about what a command will delete into a list you can read.

### Audit the commands that were run
```text
List every command you ran in this session that deleted files, overwrote files, or rewrote history. For each, explain what it did, why you ran it, and whether the result can be undone. Be explicit about anything that could have affected changes I made before the session.
```
*Why it works:* It makes the agent account for the risky steps. Compare its answer with the shell history and `git reflog`.

### Recover after a mistake
```text
I ran <command> and lost work. Do not run anything yet. Explain what state the repo is in now, what git reflog could show about where HEAD and my branches used to point, and the safest next steps, one command at a time, each explained before I run it.
```
*Why it works:* The reflog records where branches and HEAD pointed, which can help you find a lost commit. It can't bring back edits that were never committed, which is why committing first matters. Going slowly avoids making the recovery worse.

## Check your understanding

```quiz
Q: What is a git worktree useful for?
- [ ] Backing up your project to the cloud
- [x] A second working copy of the same repository, so you can have another branch checked out at the same time, for example for a parallel agent session
- [ ] Removing untracked files
> Why: A linked worktree has its own checked-out branch and index while sharing the rest of the repository.

---

Q: Which command can delete files that git is ignoring, such as a .env file or node_modules?
- [ ] git status
- [ ] git diff
- [x] git clean -fdx
> Why: git clean removes untracked files, and the -x flag removes ignored files as well. Use -n first to see what it would delete.

---

Q: Why commit before an agent session?
- [ ] The agent can't start without a commit
- [x] It gives you a snapshot to return to, so a bad session is easy to discard
- [ ] It speeds up the agent
> Why: Committed work is the only work git reliably protects. Uncommitted edits can be overwritten by commands like git reset --hard.
```

## Checklist
- [ ] `.gitignore` covers secrets, dependencies and build output
- [ ] Lockfile is committed
- [ ] You review `git diff` before every commit an agent prepares
- [ ] You committed (or branched) before the session started
- [ ] The agent works on its own branch or worktree
- [ ] Commands that discard work need your OK, ideally after a dry run

## Go deeper
- [Pro Git (free book)](https://git-scm.com/book)
- [The Missing Semester of Your CS Education (MIT)](https://missing.csail.mit.edu)
- [git restore](https://git-scm.com/docs/git-restore), [git reset](https://git-scm.com/docs/git-reset), [git clean](https://git-scm.com/docs/git-clean), [git reflog](https://git-scm.com/docs/git-reflog), [git worktree](https://git-scm.com/docs/git-worktree) and [git add](https://git-scm.com/docs/git-add): the official manual pages for the commands above
- [Working with coding agents](/docs/start-here/working-with-coding-agents.md): a git-first agent workflow
- [Reviewing and verifying AI output](/docs/ai-agents/reviewing-ai-output.md): checking diffs
