---
title: "Creating repositories — Git for Laravel | Roundly"
description: "Create repositories under a user, an organization or from a template, with an initial commit and a chosen default branch."
url: https://roundly-consulting.com/open-source/docs/git-for-laravel/creating-repositories
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Git for Laravel](https://roundly-consulting.com/open-source/docs/git-for-laravel.md)

# Creating repositories

createRepository() picks its route from the input: a template generates from a template repository (POST /repos/{template}/generate), an owner creates in that organization (POST /orgs/{owner}/repos), and neither creates under the authenticated account (POST /user/repos):

```php
use RoundlyConsulting\Git\Dto\Input\NewRepository;

$repo = Git::github()->createRepository(new NewRepository(
    name: 'widget-for-laravel',
    private: true,
    owner: 'acme',
    template: 'acme/package-template',
));
```

## Initial commit and default branch

Only the template route produces a repository that already has a commit — the other two create an empty one unless you pass autoInit: true, and cloning an empty repository with --branch fails. defaultBranch is honoured by renaming the initial branch after creation, because GitHub accepts no default\_branch at creation time; it therefore requires autoInit or a template:

```php
// An empty repository cannot be cloned with --branch — ask for an initial commit:
$repo = Git::github()->createRepository(new NewRepository(
    name: 'widget',
    private: true,
    description: 'Internal widget service',
    autoInit: true,
    defaultBranch: 'develop',   // requires autoInit or a template
));
```

## Organization permissions

Creating in an organization needs the GitHub App to hold organization administration: write — an org owner approves it on the existing installation. Mint the token for it just-in-time and let it fall out of scope:

```php
use RoundlyConsulting\Git\Dto\Credentials\GithubAppToken;
use RoundlyConsulting\Git\Dto\Input\InstallationTokenScope;

$credentials = GithubAppToken::for($appId, $installationId, $privateKey)
    ->forScope(InstallationTokenScope::administrationOnly());

Git::github($credentials)->createRepository(new NewRepository(
    name: 'widget',
    owner: 'acme',
    autoInit: true,
));
```

When GitHub refuses the creation with a 403 that carries no rate-limit signal, the package throws InvalidCredentialsException naming the missing permission instead of GitHub’s opaque “Resource not accessible by integration”.

## On GitLab and Bitbucket

- GitLab maps owner to a numeric namespace\_id (a non-numeric owner throws InvalidArgumentException), autoInit to initialize\_with\_readme, and takes defaultBranch at creation time.
- Bitbucket maps owner to the workspace — pass one. It has no initial-commit option, so autoInit or defaultBranch throws FeatureNotSupportedException rather than being dropped.
- Template generation is GitHub-only and throws FeatureNotSupportedException elsewhere.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/git-for-laravel.md)
