LESSON 3

Git and Version Control for AI-Assisted Work

Use branches, small commits, diffs, and recovery tools so that AI-assisted changes stay reviewable, attributable, and reversible.

Introduction

An assistant can change twenty files in a minute. Without discipline, you end up with a large, tangled working directory and no clean way to separate what is good from what is not.

Version control is the safety net that makes AI-assisted development practical. It lets you experiment boldly, inspect exactly what changed, and undo mistakes cheaply. This lesson focuses on the Git habits that matter most when much of the code is generated.

Learning Objectives

  • Choose a branching approach suited to a small team with frequent integration.
  • Create small, focused commits with clear messages.
  • Inspect and stage AI-generated changes selectively.
  • Use checkpoints, worktrees, and recovery commands to undo or isolate work.
  • Keep secrets and generated artifacts out of the repository.
  • Write effective pull request descriptions and decide how to record AI involvement.

1. Why Version Control Matters More With AI

  • Diffs are the review surface. You cannot review what you cannot see; the diff shows exactly what the assistant changed.
  • Changes are larger and faster. Small, frequent commits keep each change understandable.
  • Mistakes are cheap to undo, but only if you committed a clean state before the session.
  • History is documentation. Future maintainers, including you, need to know why a change was made.

2. A Simple Branching Approach

For a small team with CI, trunk-based development with short-lived branches works well:

  • main is always releasable and protected.
  • Work happens on short-lived branches (hours to a couple of days), named by purpose: feat/csv-import-validation, fix/duplicate-import.
  • Branches merge back through pull requests with review and passing checks.
  • Long-running branches are avoided; they accumulate conflicts and unreviewed change.
  • Incomplete features can be merged behind feature flags if needed.

Whatever approach you choose, write it down in the contributing guide so the team and the assistant follow the same rules.

3. The Checkpoint Habit

Before every assistant session:

1
2
git status                  # working tree should be clean
git switch -c feat/csv-import-validation

During the session, commit working checkpoints often. If an agent goes down a bad path, you can return to the last good state:

1
2
3
4
git diff                    # what changed since the last commit?
git restore path/to/file    # discard changes in one file
git restore .               # discard all unstaged changes (be sure)
git stash                   # set changes aside temporarily

Prefer git restore and git stash to ad hoc undoing in the editor. Know before you run a destructive command exactly what it will discard.

4. Reading and Staging Diffs

Never commit generated changes you have not read.

1
2
3
git diff --stat             # which files, how many lines
git diff                    # the changes themselves
git add -p                  # stage change by change, interactively

git add -p is especially valuable with AI output: it lets you accept the useful hunks and leave out unrelated reformatting, debug prints, or unrequested “improvements.” If a diff contains much that you did not ask for, treat that as a signal to tighten the prompt and rerun, not to merge it anyway.

5. Commit Hygiene

A good commit is one logical change, builds and passes tests, and explains the why.

A widely used convention is a short imperative summary with an optional type prefix:

1
2
3
4
5
6
feat(imports): reject rows with empty feedback body

Rows without a body cannot be linked to evidence later. Return error
code MISSING_BODY and continue processing the remaining rows.

Refs: FR-IMP-02

Guidelines:

  • Separate refactoring from behavior change in different commits. Reviewers (and git bisect) will thank you.
  • Separate formatting-only changes from logic.
  • Reference requirement or issue IDs.
  • Make sure each commit passes the tests. This makes git bisect reliable.

An assistant can draft commit messages from a diff, but you must check that the message describes what actually changed and why.

1
2
3
4
5
6
Write a commit message for the diff below.
Use a type prefix, an imperative summary under 72 characters, and a
body explaining why (not a line-by-line description of what).
Do not mention anything that is not in the diff.

[paste git diff --staged]

6. Recording AI Involvement

Teams differ here, and the policy is yours to set (see lesson 10). Options include:

  • No special marking: the committer is accountable regardless of how the code was written.
  • A commit trailer such as Co-authored-by or a custom trailer, where tools or policy require disclosure.
  • A note in the pull request description for significant AI-generated portions, which is useful for reviewers deciding where to look harder.

Whatever you choose, the person committing is the author of record and is accountable. Check also whether your employer, customers, or open-source projects you contribute to have rules about AI-generated contributions.

7. Parallel Work With Worktrees

Agents and experiments sometimes need isolation. git worktree creates additional working directories from the same repository:

1
2
3
git worktree add ../signaldesk-experiment -b exp/alternative-parser
# work and compare; then:
git worktree remove ../signaldesk-experiment

This lets you try an assistant’s alternative approach without disturbing your current branch, and lets you run several branches side by side. Keep the number of parallel efforts small enough that you can actually review them.

8. Secrets and Generated Files

  • Use .gitignore for environment files, build output, virtual environments, and local tool state.
  • Add secret scanning as a pre-commit hook and in CI.
  • Never ask an assistant to “hardcode the key for now.” Use environment variables and a documented example file (.env.example) with fake values.
  • If a secret is committed, rotate it immediately. Removing it from history does not make it safe, because it may already have been copied.
  • Do not commit large generated artifacts or dependency directories.

9. Recovery and Archaeology

1
2
3
4
5
git reflog                  # history of where HEAD has been; recovers "lost" commits
git revert <commit>         # create a new commit that undoes an earlier one
git bisect start            # binary-search for the commit that introduced a bug
git log -S "validate_row"   # find commits that added or removed this text
git blame -w path/to/file   # who last changed each line (ignoring whitespace)

Prefer git revert for commits already shared; avoid rewriting shared history. Rewriting local, unpushed history (squashing, amending) is fine and often helpful to produce a clean story.

10. Pull Requests

A pull request description is a review aid. Include:

  • What changed and why, with references to requirements.
  • How it was verified: commands run, tests added, manual checks.
  • Risks and areas needing attention, including significant AI-generated portions.
  • Out of scope: what this change deliberately does not do.

Keep pull requests small, ideally a few hundred changed lines at most. Large, assistant-generated changes are the hardest to review properly; split them.

11. Practical Exercise

For SignalDesk (or your project):

  1. Write a short CONTRIBUTING.md: branching rules, commit convention, pull request template, and how AI involvement is noted.
  2. Configure .gitignore, a .env.example, and a secret-scanning pre-commit hook.
  3. Run an assistant session on a small task. Use git diff and git add -p to stage only the intended changes, then commit with a message you verified.
  4. Deliberately let an experiment go wrong, then recover with git restore or git stash, and describe what you learned.
  5. Open a pull request (or write the description for one) following your template.

Verification

You are ready to continue when:

  • Branching, commit, and pull request rules are documented.
  • Secrets cannot be committed unnoticed, and .env.example exists with fake values.
  • You can stage selectively and recover from a bad session.
  • Your commits are small, verified, and explain why.

Key Takeaways

  • Git is the safety net and the review surface for AI-assisted work.
  • Start from a clean tree, commit often, and read every diff.
  • Use git add -p to accept only what you asked for.
  • Separate refactoring, formatting, and behavior changes.
  • The committer remains accountable, however the code was produced.

What’s Next?

With the workflow and safety net in place, we create the project itself. Next, we build a walking skeleton that enforces the architecture and runs checks from day one.