Skip to content

[BUG]: github_branch_default attempts to rename to the same name on import #2613

Closed

Description

@randy-coburn-zeam

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

Activity

  1. added
    Status: Triage
    Type: Bug
    on Apr 7, 2025
  2. moved this from 🆕 Triage to 🔥 Backlog in 🧰 Octokit Activeon Jun 3, 2025
  3. jmalloc commented on Oct 16, 2025

    @jmalloc

    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 when rename is true, the resource only renames the branch, and doesn't set it as the default at all, see

    if rename {
    repository, _, err := client.Repositories.Get(ctx, owner, repoName)
    if err != nil {
    return err
    }
    if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil {
    return err
    }
    } else {
    repository := &github.Repository{
    DefaultBranch: &defaultBranch,
    }
    if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
    return err
    }
    }

  4. deiga commented on Feb 11, 2026

    @deiga
    Collaborator

    @jmalloc
    RenameBranch renames the repository.DefaultBranch to defaultBranch that 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!

  5. added this to the v6.12.0 Release milestone on Feb 11, 2026
  6. removed
    Status: Triage
    on Feb 20, 2026
  7. self-assigned this
    on Feb 21, 2026
  8. deiga commented on Feb 21, 2026

    @deiga
    Collaborator

    @randy-coburn-zeam since you know what the values are for the default branch that you are importing, why would you be setting rename to true?

    It sounds like you might be expecting Terraform to behave so that it "fixes" the config error you've created

  9. moved this from Backlog to In Progress in Terraform Provideron Feb 21, 2026
  10. randy-coburn-zeam commented on Feb 23, 2026

    @randy-coburn-zeam
    Author

    @randy-coburn-zeam since you know what the values are for the default branch that you are importing, why would you be setting rename to true?

    It sounds like you might be expecting Terraform to behave so that it "fixes" the config error you've created

    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.

  11. deiga commented on Feb 23, 2026

    @deiga
    Collaborator

    @randy-coburn-zeam since you know what the values are for the default branch that you are importing, why would you be setting rename to true?

    It sounds like you might be expecting Terraform to behave so that it "fixes" the config error you've created

    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.

    @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.

  12. randy-coburn-zeam commented on Feb 23, 2026

    @randy-coburn-zeam
    Author

    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?

  13. deiga commented on Feb 23, 2026

    @deiga
    Collaborator

    @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

  14. randy-coburn-zeam commented on Feb 23, 2026

    @randy-coburn-zeam
    Author

    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:

    Indicate if it should rename the branch rather than use an existing branch.

    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.

  15. moved this from In Progress to Done in Terraform Provideron Jun 24, 2026
  16. moved this from 🔥 Backlog to ✅ Done in 🧰 Octokit Activeon Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type: Bug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions