You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error when creating a new branch for an empty repository #577
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=truedelete_branch_on_merge=true
}
resource"github_branch""branch" {
repository=github_repository.repository.namebranch="main"
}
Expected Behavior
Create the repository and the branch
Actual Behavior
The branch is not created because the repository is empty
Regards
Reacted by Chris Kacerguis, Jonathan Maye-Hobbs, Mikhail Filipchuk, Farid Ahmadian, Gabriel Becerra Gottlieb, Marcel, Maksym Fedchenko, Markus Pesch, Zach Cohen, Chris Pinola and 3 more
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 :/
Reacted by Anwar, Jeremy Udit, Pablo Ignacio Ocampo, Daniel Rejniak, Mitsukuni Sato, José E. Félix and Nicolás V
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.
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?
Reacted by Anwar, Ben Abrams, Basile and Dawid Urbański
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.
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.
Reacted by Ben Abrams, Jeremy Udit, Dimitris Moraitidis, Kyrylo Merzlikin, Jackson, dudicoco and James Harris
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.
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.
👋 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!
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.
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
@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?
Activity
onurg commentedon Nov 9, 2020
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 :/
HorizonNet commentedon Nov 12, 2020
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.
jcudit commentedon Nov 24, 2020
The
auto_initparameter may be helpful for creating a branch when the repository is created. We also have a newdefault_branchresource that may be helpful?p99ops commentedon Nov 24, 2020
I think that source_branch argument should default to
mainmajormoses commentedon Dec 19, 2020
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.
jspiro commentedon Mar 18, 2021
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_destroybut 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.
jspiro commentedon Mar 18, 2021
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.
ayk33 commentedon Sep 13, 2021
Can we get an update on this? This issue is a blocker.
majormoses commentedon Sep 14, 2021
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.
berserker5000 commentedon Apr 26, 2022
Hi there, any update on this?
nukezzzz commentedon May 19, 2022
Hi got the same issue , Added
source_branch = organization_default_branchto resource "github_branch" , hope it helps !seanorama commentedon Feb 7, 2023
None of the above workarounds have worked for us.
Simplified without variables:
18 remaining items
github-actions commentedon Jun 29, 2025
👋 Hey Friends, this issue has been automatically marked as
stalebecause it has no recent activity. It will be closed if no further activity occurs. Please add theStatus: Pinnedlabel if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!moltar commentedon Jun 29, 2025
Stalebot Response
Not stale.
GitHub stale bot considered harmful
deiga commentedon Feb 11, 2026
The
default_branchfield is deprecated in favour of thegithub_branch_defaultresource.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.
moltar commentedon Feb 11, 2026
I'm using
github_branch_default, yet I'm still getting the same problem.deiga commentedon Feb 11, 2026
@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
frmadem commentedon May 21, 2026
Found the same error creating a branch:
deiga commentedon May 21, 2026
@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?