Support non-main default branch on repo creation#57
Merged
Conversation
When a new repository is created with auto_init, GitHub initialises it with a
single `main` branch. Setting `github_branch_default` to any other branch (e.g.
`master`) then fails at apply with:
422 Validation Failed - The branch master was not found.
Use the provider's `rename` option to rename the auto-init branch to the desired
default in that case. Renaming is skipped when the target is `main` (already the
auto-init branch) or an explicitly managed `github_branch`, and `rename` is
ignored after creation so existing/imported repos do not show a perpetual diff.
Fixes #14
pavlovic-ivan
approved these changes
Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a YAML config for a new repository sets
default_branchto anything other thanmain, Terraform fails at apply (issue #14).auto_initcreates the repo with a singlemainbranch, thengithub_branch_defaulttries to point the default at e.g.master, which does not exist yet:The repo is left created but with the wrong default branch.
Fix
Set
rename = trueongithub_branch_defaultso the provider renames the auto-init branch (main) to the requested default instead of expecting it to already exist.Renaming is applied only when needed:
main→ no rename (it's already the auto-init branch)github_branch→ no rename (that branch is created separately)master) → rename the auto-init branch to the targetrenameis wrapped inlifecycle { ignore_changes = [rename] }so existing and imported repos whose default already matches do not show a perpetual diff.Testing
Reproduced and verified against a test org via local
terraform apply:default_branch: master→ repo created, apply errors with the 422 above; repo left with defaultmain.master, sole branch renamed frommaintomaster.Closes #14