Repository navigation
Description
Expected Behavior
When you create a repo and assign the default branch, the code should check to see if it needs to do work.
If the default branch supplied is the same as the current branch, then nothing needs to be done.
This is visible when importing a repo settings.
Actual Behavior
I have a repo called x, x repo has a branch called main that is the primary branch. I created x repo outside of terraform.
I import the repo and assign the default branch using a github_branch_default resource which is also imported.
The import of the repo is done as expected, but the github_branch_default is imported and the rename config is set to additive and true. This then triggers a rename to happen, which then fails because the source and destination name are the same.
This is even more difficult to deal with since I happen to use the github_branch_default within a module that manages the repos for me.
Terraform Version
Terraform 1.11.3
Provider version: 6.6.0
Affected Resource(s)
- github_branch_default
Terraform Configuration Files
import {
to = module.generic_repos["junk-1"].github_repo.current
id = "randy-junk-1"
}
import {
to = module.generic_repos["junk-1"].github_branch_default.main
id = "randy-junk-1"
}
# -- Within the module 'generic_repos' ---
variable "name" {
description = "Repo name"
type = string
}
variable "description" {
description = "Repo description"
type = string
}
variable "visibility" {
description = "Repo visibility"
type = string
default = "internal"
}
variable "primary_branch" {
description = "Default branch"
type = string
default = "main"
}
resource github_repository "current" {
name = var.name
description = var.description
visibility = var.visibility
auto_init = true
# Failsafe to prevent accidental deletion of the repo.
archive_on_destroy = true
vulnerability_alerts = true
}
resource "github_branch_default" "main" {
repository = resource.github_repository.current.name
branch = var.primary_branch
rename = true
}Steps to Reproduce
Create a repo outside of terraform.
Set anything you want as the primary branch.
Create a repo using code above, making sure to set the primary branch the same as default branch in your repo. Before you apply do the imports like below.
Import both the repo and the default branch like in the code snip it.
When the apply happens you'll see that it tries to rename the primary branch to the same name that you imported.
Debug Output
Panic Output
Code of Conduct
- I agree to follow this project's Code of Conduct
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields✅ Done
- StatusShow more project fieldsDone
Activity
jmalloc commentedon Oct 16, 2025
This occurs even when not using an
import, on every update.In fact, now that I look at the implementation I'm not sure I understand the purpose of
rename, as it appears that whenrenameistrue, the resource only renames the branch, and doesn't set it as the default at all, seeterraform-provider-github/github/resource_github_branch_default.go
Lines 145 to 161 in 90c871e
deiga commentedon Feb 11, 2026
@jmalloc
RenameBranchrenames therepository.DefaultBranchtodefaultBranchthat you gave it as config.The API seems to have a bit of magic, that changing the name of the DefaultBranch still keeps that branch as the default
@randy-coburn-zeam You're right that the Import implementation could be a bit more clever to prevent any unnecessary work!
deiga commentedon Feb 21, 2026
@randy-coburn-zeam since you know what the values are for the default branch that you are importing, why would you be setting
renametotrue?It sounds like you might be expecting Terraform to behave so that it "fixes" the config error you've created
randy-coburn-zeam commentedon Feb 23, 2026
We created a module that we wanted to use to get all the hand made repos in order. Since I have bunch of hand made repos I need to import them to get them to conform to the standards set out in the module. One of those standards was the default branch.
So we have many repos being imported and some need to change and some don't. As you can see in the original bug report it's the ones that don't need a change that cause the problem.
deiga commentedon Feb 23, 2026
@randy-coburn-zeam
Yes, I understand that you want to import default branches that don't need to be renamed. Unfortunately that's not something the provider supports.
Basically you're asking us to support invalid configuration, but instead you could just remove rename from the branches that don't renaming.
randy-coburn-zeam commentedon Feb 23, 2026
What I am asking for is to have the import do what more terraform providers do and import state and then rectify if required.
What you are saying is that I can't use the import if the resource is in the correct state. Since it only works if it can change the name. The failure here is that when importing, if the name is correct, the provider tries to change the name and fails because the name is correct.
The feature is simple broken in my eyes if I need to manually toggle it if I am importing a repo. I assume the point of the flag is to fix a repo if it changes out of terraform, so you would need to check if it needs to be updated on each plan phase?
deiga commentedon Feb 23, 2026
@randy-coburn-zeam renaming the default branch and changing the default branch are 2 different api endpoints.
If you instruct terraform that you want to rename the current default branch to the one specified in the config, then it will try to do that.
It is not the providers responsibility to check if the branch name is already the given.
It is the user's responsibility to not configure a rename operation if its not needed
randy-coburn-zeam commentedon Feb 23, 2026
That's literally the point of stateful configuration? Thats like saying that terraform shouldn't have a planning phase because its the users responsibility to make sure that resource must be ready to accept changes before terraform runs. Idempotency being a key driver here.
The main issue here is that you have a rename bool flag that makes the resource act differently. If that flag wasn't there the argument would be that if the value of the branch is x and it's imported as y then it must change to x. If the branch is x and the imported value is x then nothing needs to happen.
The fact that I am importing a resource with the same name shouldn't change the fact that when the name does change I want the provider to fix it.
Here is what I want and it seems really obvious to me that it's a problem so I am having a hard time understanding why you are saying that it's a configuration problem. I import a resource that is correctly configured according to my stateful configuration management tool, it should not attempt to make changes to the resource. It especially should not attempt to fix something that is not different to what is expected.
The document states:
Yes, should I need to change the name of the default, then rename it.
The issue here is, it doesn't need to rename the branch, so, do nothing and just import the state.
Edit: I just want to tone this down a bit because reading it again I feel like I sound a bit crazy here, but I really am just failing to understand where you are coming from with your view on this.