Skip to main content

Source Code

agent-client-tutorial/03-git-operations

What You’ll Build

An agent that inspects git history, finds commits on a detached HEAD, and merges them into the main branch. This demonstrates agents working with version control — a common real-world task.

Step 1: Set Up the Repository

Clone the tutorial and run the setup script that creates a git repo with “lost” changes:
git clone https://github.com/markpollack/agent-client-tutorial.git
cd agent-client-tutorial/03-git-operations
bash setup.sh
The setup script creates a git repository in workspace/ with a commit on a detached HEAD — simulating changes that were accidentally “lost” when switching back to master.

Step 2: Understand the Goal

The prompt describes the problem in natural language, as a developer would:
String goal = """
    I just made some changes to my personal site and checked out master,
    but now I can't find those changes. Please help me find them and
    merge them into master.""";
The agent needs to:
  1. Inspect the git reflog to find the detached commit
  2. Identify the lost changes
  3. Merge or cherry-pick them into master

Step 3: Run It

./mvnw spring-boot:run
The agent uses tools like Bash to run git reflog, git log, git merge, etc.

Step 4: Verify

cd workspace
git log --oneline -5
# Should show the recovered changes merged into master

What’s Different from Previous Lessons

  • Stateful environment — the agent works in an existing git repository, not a clean directory
  • Multi-step reasoning — finding lost commits requires inspecting reflog, understanding branch state, and choosing a merge strategy
  • Tool-heavy task — the agent runs multiple shell commands, reads their output, and decides next steps
  • STRICT mode applicable — this task genuinely requires a git repository, making it a natural fit for AgentClientMode.STRICT

The LOOSE/STRICT Connection

Lessons 1-3 work in any directory — they don’t need git. That’s why LOOSE mode (the default) is appropriate. This lesson requires a git repository. If you were building a system that only runs git-aware tasks, STRICT mode would prevent accidental execution outside a git repo. See Defaults Philosophy for more on when to use each mode.

Next Steps