# Version control with GitHub

Link a project to a GitHub repository, push the canonical IR as diff-friendly commits, pull, browse history and restore any commit as a new revision.

Source: https://glamorous-boar.cloudvent.net/github/

---

Projects are versioned inside AgenticMind as **revisions**. GitHub sync adds what teams expect from Git: code review, history and blame, CI, sharing and off-site backups.

## What gets committed

Under an optional folder in the repository:

- `project.agenticmind.json`: the **canonical IR**, with sorted keys and stable formatting, so diffs show real changes;
- `README.md`: generated from the project's name, description and nodes, with how to run it locally (`agenticmind run`);
- `app/` (optional, `include_app`): the exported standalone FastAPI app.

Secrets stay references; no secret value is ever written.

## Set it up

1. Create a fine-grained GitHub token with **Contents: read and write** on the repository (plus **Administration: write** if AgenticMind should create the repository). Save it as the `GITHUB_TOKEN` secret (Settings → Secrets or `.env`).
2. Click empty canvas → **Project** panel → **GitHub**. Fill: repository (`owner/name`), branch, folder, token secret name, and options: **auto-push** on save, **include app**, **create repo** if missing (private, initialised).
3. **Check access** verifies reachability and push permission.
4. **Push** with a commit message.

## Push, pull, history, restore

| Action | What happens |
|---|---|
| **Push** | Sends the **latest saved revision** as one atomic commit (Git Data API: one tree with `base_tree`, one commit, a fast-forward-only ref update). Unrelated files in the repo are kept. If the file is unchanged on GitHub (even if formatted differently), nothing is pushed. |
| **Remote changed** | If someone changed the file on GitHub since your last sync, push stops with `GIT_REMOTE_CHANGED` (409). **Pull** first, or **Push anyway** to add your version as a new commit on top. History is never rewritten. |
| **Branch moved mid-push** | Stops with `GIT_BRANCH_MOVED`; retry. |
| **Pull** | Saves the branch's file as a new revision (earlier revisions remain). |
| **History** | Commits that touched the project file, with links. |
| **Restore** | Loads any commit as a new revision. The sync point does not move, so a later push still detects remote changes. |
| **Auto-push** | After each save, a push runs in the background; a refused or failed auto-push is recorded, never a failed save. |

New branches are created from the default branch; empty repositories are seeded automatically. GitHub Enterprise works through an API base (`https://host/api/v3`).

## Typical team workflow

1. Each person works in AgenticMind; auto-push commits every save to a feature branch.
2. Open a pull request on GitHub; reviewers read the canonical IR diff (and the generated README).
3. CI can run `agenticmind validate project.agenticmind.json` (and even the exported app's tests).
4. After merge, teammates **Pull** to get the merged graph as a new revision.

## API

`GET/PUT/DELETE /api/projects/{id}/git`, `POST …/git/check`, `POST …/git/push` (`message`, `force`), `POST …/git/pull`, `GET …/git/history`, `POST …/git/restore` (`sha`).

## Limits

GitHub only (no GitLab or Bitbucket yet). Pulls replace the project as a new revision; there is no three-way merge of graph changes. No webhook-driven pulls from GitHub.
