The terminal & git
The two tools every agent uses constantly, and the minimum you need in order to supervise them.
8 min read · Last reviewed November 20, 2026
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 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 aMakefile/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. - : 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)#
Your call, not the agent's
| 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 statusandgit diffbefore 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 -fdcurl … | shfrom an unknown URLchmod 777, or anything usingsudo- Installing a package you've never heard of → check it on the registry first
- Committing
.envor credentials - Common agent mistakes:
git add -Aorgit add .stages everything that isn't ignored, including a.envfile if it isn't in.gitignore.git reset --hardoverwrites your files with the last commit, so uncommitted work is gone.git clean -fdxalso removes ignored files such as.envandnode_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#
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.
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#
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#
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#
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#
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 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#
Check your understanding
Checklist#
-
.gitignorecovers secrets, dependencies and build output - Lockfile is committed
- You review
git diffbefore 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)
- The Missing Semester of Your CS Education (MIT)
- git restore, git reset, git clean, git reflog, git worktree and git add: the official manual pages for the commands above
- Working with coding agents: a git-first agent workflow
- Reviewing and verifying AI output: checking diffs