# Connect a GitHub repository

> Add an app that works directly in your own GitHub repository, so saving commits straight to your branch

Connecting a repository is one of three ways to add an app to a project, alongside creating a new app and [importing existing code](https://docs.draftbit.com/intro/building-with-draftbit/importing-from-github). An import copies your code into Draftbit. A connected app works in your repository itself: Draftbit keeps your files as they are, and saving in the builder commits straight to the branch you choose.

Connected apps get chat, code editing, and a preview when your app has one. There’s no migration step, and Draftbit doesn’t rewrite your project to fit its conventions.

## Connect a repository

To start a new project from your repository, click **Create New Project** on the Dashboard, choose **Connect**, then click **Connect a repository**. To add a connected app to a project you already have, open the project and click **Connect repo** next to **Import app** and **Add app**. You need the Editor role or higher.

First choose how Draftbit reaches your repository:

* **GitHub App** (recommended): Draftbit uses the Draftbit GitHub App, the same app used to import private repositories. There’s no token to create or renew.
* **Access token**: you paste a GitHub fine-grained personal access token.

### Connect with the GitHub App

1. Click **Connect GitHub**. A GitHub window opens where you choose which account or organization to install the Draftbit GitHub App on, and which repositories it can see.
2. Pick the account, then search for and select the repository.
3. Choose the **Main branch** from the repository’s branches. It starts as the repository’s default branch.
4. Choose the **App folder**. The list shows the folders on that branch that have a `package.json`, and starts with the top of the repository when it has one.

If the repository is empty, or the branch has no `package.json`, Draftbit says so and offers **Create a new app** instead.

To save your changes, the GitHub App needs the **Contents: Read and write** permission on the repository. If GitHub asks you to approve updated permissions for the app, approve them before you connect. To use a repository the app can’t see yet, click **Add account** to reopen GitHub and add the repository to the installation.

If you aren’t an owner of the GitHub organization, GitHub sends your install request to the organization’s owners instead of installing the app. Draftbit shows that it’s waiting for their approval. Once an owner approves, click **Refresh** to see the organization’s repositories.

If Draftbit is already installed on the account you pick, for example because a teammate installed it on your organization, GitHub shows the installation’s settings and doesn’t send you back to Draftbit. Close the GitHub window and click **Link existing installation**.

### Connect with an access token

Fill in the repository details:

* **Repository address**: for example `https://github.com/owner/repo`.
* **App folder**: leave this empty when your app’s `package.json` is at the top of the repository. For a monorepo, give the folder that contains it, such as `apps/mobile`. Draftbit works only inside this folder.
* **Main branch**: the branch your saves commit to. Draftbit keeps your in-progress work on a separate working branch until you save.
* **Fine-grained access token**: a GitHub [fine-grained personal access token](https://github.com/settings/personal-access-tokens/new) (it starts with `github_pat_`). Give it access to only this repository, with **Contents: Read and write**. If your app changes GitHub Actions workflows, also grant **Workflows: Read and write**. If your organization uses SAML single sign-on, authorize the token for it. Classic tokens aren’t supported.

### Finish connecting

Click **Continue**. Draftbit runs the same checks as an import, and also confirms it can reach the repository and branch and push to them. If the branch is protected, you’ll see a warning: saving fails unless the GitHub App or token is allowed to push to that branch.

Once the checks pass, name the app and click **Connect repository**. The app opens in the builder when it’s ready.

## How connected apps look

On the project page, a connected app’s card shows **GitHub** and the repository it works in, such as `owner/repo/apps/mobile`, under the app’s name. When the connection’s access token expires within a week, the card shows **Token expires soon**. Editors and above can select it to open **Repository Connection** and update the token.

## Working in a connected repository

* **Saving** commits your changes to the main branch you chose. Because your code already lives on GitHub, the **Export** menu doesn’t offer GitHub Export for connected apps.
* **Publishing**: connected apps built with Expo, React Router on Vite, or Astro can publish from the **Publish** popover. See [Imported and connected apps](https://docs.draftbit.com/features/publishing#imported-and-connected-apps) for what each framework needs.
* **Device and simulator previews**: Expo apps can be previewed on your phone with the [Draftbit Preview app](https://docs.draftbit.com/features/ai-app-builder#on-device-previews) and in [iOS and Android simulators](https://docs.draftbit.com/features/simulators).
* **Syncing**: when someone pushes new commits to the main branch, click **Sync** in the top bar to bring them into your working branch. You need to sync before you can save. If your changes and the new commits touch the same lines, a dialog opens so you can choose which changes to keep. For each file, keep your version, use the main branch’s version, or pull in individual spots and edit the result. Click **Let the agent sort it out** to hand the remaining files to the agent in a new thread. When every file is done, click **Finish sync**. If you close the dialog, click **Changes to review** in the top bar to open it again.
* **Changing the connection**: open the app card’s menu and choose **Repository Connection** (Editors and above) to update the repository, branch, app folder, or how the app connects. You can switch between the GitHub App and an access token at any time. Click **Check Access** to test it before saving. Changing the repository or branch rebuilds the app on a new working branch, and unsaved changes aren’t carried over. With a token, leave the token field empty to keep the current one.

## When access stops working

If Draftbit loses access to the repository, the app’s card shows **Repository access lost**, and the app won’t open until the connection works again. Open **Repository Connection** to fix it. The dialog tells you what went wrong, such as an archived repository, a branch that no longer exists, or a token that can’t push.

* **GitHub App**: access stops when the app is uninstalled from the account, the repository is removed from the installation, or the installation hasn’t approved the permissions Draftbit needs. Reinstall the app or add the repository back on GitHub, then save the connection again. Removing Draftbit from an account in the GitHub account list, or disconnecting GitHub, also stops every app connected through it.
* **Access token**: access stops when the token expires, is revoked, or loses access to the repository. **Token expires soon** appears on the card in the week before the token’s expiration date. Paste a new token and save.

## Limits

* The repository needs your app’s code already. Draftbit can’t connect an empty repository or start a new app inside one. Create a new app in Draftbit instead.
* One app per repository: a repository that’s already connected to another app can’t be connected again.
* The app folder needs its own `package.json` and lockfile. A repository that is itself a workspace root isn’t supported yet.
* The same frameworks as imports are supported. See [What can be imported](https://docs.draftbit.com/intro/building-with-draftbit/importing-from-github).
