Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Last active October 30, 2021 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maurolepore/a4a627cbdb1d76eea2011204ce41a65d to your computer and use it in GitHub Desktop.
Save maurolepore/a4a627cbdb1d76eea2011204ce41a65d to your computer and use it in GitHub Desktop.

Here: https://git.io/JPRtG

# This is how I renamed master to main in all my repos, in bulk on GitHub

library(purrr)
library(glue)
library(gh)

rename_master_to_main <- function(repos, owner) {
  # From the source code of `usethis::git_default_branch_rename()`
  doit_once <- function(repo, owner) {
    post <- glue::glue("POST /repos/{owner}/{repo}/branches/master/rename")
    gh::gh(post, new_name = "main")
  }

  purrr::walk(repos, purrr::safely(doit_once), owner)
}

# From https://github.com/r-lib/gh#usage
repos <- gh::gh(
  "GET /users/{username}/repos",
  username = "maurolepore",
  .limit = Inf
)
repos <- vapply(repos, "[[", "", "name")

rename_master_to_main(repos, owner = "maurolepore")
# Then in each local clone run `usethis::git_default_branch_rediscover()`
# -- https://www.tidyverse.org/blog/2021/10/renaming-default-branch/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment