Skip to content

Error when creating a new branch for an empty repository #577

Closed

Description

@p99ops

Hello,

We are facing this error while trying to create a new branch for a new repository :

github_branch.branch: Creating...

Error: Error querying GitHub branch reference my-organization/first-repository (refs/heads/master): GET https://api.github.com/repos/my-organization/first-repository/git/refs/heads/master: 409 Git Repository is empty. []

  on main.tf line 15, in resource "github_branch" "branch":
  15: resource "github_branch" "branch" {

Terraform Version

Terraform v0.12.29
+ provider.github v2.9.2

Affected Resource(s)

  • github_branch

Terraform Configuration Files

provider "github" {
  version = "~> 2.9"
  token        = "***"
  organization = "my-organization"
}

resource "github_repository" "repository" {
  name        = "first-repository"
  description = "This is the first repository"
  private     = true

  delete_branch_on_merge = true
}

resource "github_branch" "branch" {
  repository = github_repository.repository.name
  branch     = "main"
}

Expected Behavior

Create the repository and the branch

Actual Behavior

The branch is not created because the repository is empty

Regards

Activity

  1. onurg commented on Nov 9, 2020

    @onurg

    The branch is not created because the repository is not "initialized" -
    unfortunately we hit the same behavior but this is something coming from the github api - when the repo is created first, you need to initialize it to be able to create a branch.

    it's a feature, not a bug :/

  2. HorizonNet commented on Nov 12, 2020

    @HorizonNet

    It's actually the way how Git works. When a repository is created it is initialized with an empty Git repository. In an empty Git repository no commit exists in the beginning. A branch is actually a pointer to a commit, which also cannot exist as there is no commit yet. Only with the first commit to a repository you will get a branch, which is named after the value in your Git configuration.

  3. added
    Type: Support
    on Nov 24, 2020
  4. jcudit commented on Nov 24, 2020

    @jcudit
    Contributor

    The auto_init parameter may be helpful for creating a branch when the repository is created. We also have a new default_branch resource that may be helpful?

  5. p99ops commented on Nov 24, 2020

    @p99ops
    Author

    I think that source_branch argument should default to main

  6. majormoses commented on Dec 19, 2020

    @majormoses
    Contributor

    I think that source_branch argument should default to main

    Perhaps we should remove the default or make it look that up (if not specified) since that is a per repository setting. While I think switching to match githubs new defaults are a good thing. That said because of the wide impact of all these changes many orgs have opted to still keep using "master" until all the tooling better supports it (not just terraform). Either way making any change to a default behavior should be considered a "breaking change" and only made in a major version.

  7. jspiro commented on Mar 18, 2021

    @jspiro
    Contributor

    Yes, you need to look up the organization default.

    Also, you have a further impact/problem. If you try to create a branch for what would be the default branch (which makes sense), then it will fail, forcing you to do an import (which is a nightmare). This should be smarter, perhaps always looking up if the branch exists or not and not holding state–just declaring the branch SHOULD exist if it doesn't.

    And then an end of life problem. We now have archive_on_destroy but no equivalent for branches. So when the repo is neatly archived, all the branches will be gone (or at least all the non-default branches depending on the above implementation).

    Then there's the problem of changing default branches. If you don't track your default branch, then it won't create a new one, it'll just destroy the old one. If you do track it, then it will destroy the old one and create a new one, moving the HEAD of that branch. Nasty problem. (Edge case, but nasty.)

    It seems like adding the new branch and default branch resources was not thought out terribly well. They're of very limited, and largely dangerous use, when all they do is prevent an error upon creation of a new repo and branch protection of a branch not existing. That is easily solved by creating the branch and then re-applying.

  8. jspiro commented on Mar 18, 2021

    @jspiro
    Contributor

    Perhaps a partially better solution would be for the default branch resource to track and create a branch, and perhaps the github API supports the renaming like it does in the UI? At the least renaming it would know which branch to rename and retain the existing SHA.

  9. ayk33 commented on Sep 13, 2021

    @ayk33

    Can we get an update on this? This issue is a blocker.

  10. majormoses commented on Sep 14, 2021

    @majormoses
    Contributor

    Can we get an update on this? This issue is a blocker.

    While this bug is annoying it's not hard to work around using modules with good defaults for your org/team. Defaults are hard because people have different needs, you can make a decision for your team/org much easier than we can as a community.

    Even outside of this issue I highly suggest taking the time to build good modules for your github org. For scale, maintenance, and for protection from the edges of the apis+providers.

  11. berserker5000 commented on Apr 26, 2022

    @berserker5000

    Hi there, any update on this?

  12. nukezzzz commented on May 19, 2022

    @nukezzzz

    Hi got the same issue , Added source_branch = organization_default_branch to resource "github_branch" , hope it helps !

  13. seanorama commented on Feb 7, 2023

    @seanorama

    None of the above workarounds have worked for us.

    github_branch.default: Creating...
    ╷
    │ Error: error querying GitHub branch reference myorg/github (refs/heads/main): GET https://github.ourdomain.tld/api/v3/repos/myorg/github/git/ref/heads/main: 409 Git Repository is empty. []
    │
    │   with github_branch.default,
    │   on main.tf line 55, in resource "github_branch" "default":
    │   55: resource "github_branch" "default" {
    

    Simplified without variables:

    terraform {
      required_providers {
        github = {
          source  = "integrations/github"
          version = "~> 5.17"
        }
      }
    }
    
    provider "github" {
      owner    = var.github_owner
      base_url = var.github_base_url
    }
    
    
    resource "github_repository" "default" {
      name        = "github"
      description = "description here"
    
      auto_init              = true
      visibility             = "public"
    }
    
    resource "github_branch" "default" {
      repository    = "github"
      branch        = "master"
      source_branch = "main"
      depends_on = [
        github_repository.default
      ]
    }
    
    GitHub Enterprise Server 3.6.3
    
  14. 18 remaining items

  15. github-actions commented on Jun 29, 2025

    @github-actions

    👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

  16. added
    Status: Stale
    on Jun 29, 2025
  17. moltar commented on Jun 29, 2025

    @moltar

    Stalebot Response

    Not stale.

    GitHub stale bot considered harmful

  18. removed
    Status: Stale
    on Jun 30, 2025
  19. moved this from Backlog to In Progress in Terraform Provideron Jan 7, 2026
  20. deiga commented on Feb 11, 2026

    @deiga
    Collaborator

    The default_branch field is deprecated in favour of the github_branch_default resource.

    There has been no actionable activity in a while and we'll close this as resolved. If you are still facing this issue, please comment and provide details of your situation.

  21. moved this from 🔥 Backlog to ✅ Done in 🧰 Octokit Activeon Feb 11, 2026
  22. moved this from In Progress to Done in Terraform Provideron Feb 11, 2026
  23. moltar commented on Feb 11, 2026

    @moltar

    I'm using github_branch_default, yet I'm still getting the same problem.

  24. deiga commented on Feb 11, 2026

    @deiga
    Collaborator

    @moltar I'm sorry to hear that!

    Please provide a TF config example and DEBUG logging of the apply run so that we can determine what the problem is

  25. frmadem commented on May 21, 2026

    @frmadem

    Found the same error creating a branch:

    2026-05-21T09:54:54.159Z [DEBUG] State storage *remote.State declined to persist a state snapshot
    2026-05-21T09:54:54.159Z [ERROR] vertex "github_repository_file.this[\".github/CODEOWNERS__main\"]" error: unexpected status code: 404 Not Found
    2026-05-21T09:54:54.159Z [DEBUG] states/remote: state read serial is: 19; serial is: 19
    2026-05-21T09:54:54.159Z [DEBUG] states/remote: state read lineage is: 61f0fbf2-ae4c-0f45-462a-24df24cf0492; lineage is: 61f0fbf2-ae4c-0f45-462a-24df24cf0492
    
    Image
  26. deiga commented on May 21, 2026

    @deiga
    Collaborator

    @frmadem your logs are about the repo file repo file resource and the screenshot about team repos resource. Neither of which seem to relate to the original issue.

    Could you open a new issue and fill out the template so that we can help you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Up for grabsType: Supportr/branch

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions