Don’t Lose your HEAD over Default Branches

[This article was first published on rOpenSci - open tools for open science, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Read on for a little trick to always get the default branch…

While git has no “default” branch, online code platforms such as GitHub need to define a default for the branch that someone sees when they look at code online. While the community standard for the default branch is now becoming main instead of master1, and while the switch for any repo is rather easy and is well supported by usethis, for a while (forever) there will still be repos with different default branches in the wild.2 Especially as nothing prevents you from using any other name for the default branch (although this could trip up collaborators).3

So, what to deal with the existence of different possible default branches?

gert::git_remote_ls() |>
head() # another useful thing named head
#> ref symref
#> 1 HEAD refs/heads/main
#> 2 refs/heads/1password <NA>
#> 3 refs/heads/2fa <NA>
#> 4 refs/heads/email <NA>
#> 5 refs/heads/fix_author_links <NA>
#> 6 refs/heads/footnotes <NA>
#> oid
#> 1 2ef29132be682883d1c12a957c38e1fa6599898f
#> 2 e4f3ef36c453dd2ede5a091c7d3f5f32c609caca
#> 3 7461f15e81651d5bbf6dcab078801cfa35e2f039
#> 4 6f8c849ef325dc597e819bffbd8829afa05ae414
#> 5 2f328db8fc6ce94caa50ddde7a0b021178818d8f
#> 6 f552c2dcac79228e929e65975c52484f5655577c

gert::git_remote_info() shows the head for a given remote:

#> $name
#> [1] "origin"
#> $url
#> [1] "[email protected]:ropensci/roweb3.git"
#> $push_url
#> $head
#> [1] "refs/remotes/origin/main"
#> $fetch
#> [1] "+refs/heads/*:refs/remotes/origin/*"
#> $push
#> character(0)
  • When linking to a file, or trying to download the same path from a gaggle of repositories, think of the HEAD reference! I.e. instead of linking to a file via<owner>/<repo>/tree/<default-branch>/<path> where you need to know what the default branch is, you can link to<owner>/<repo>/tree/HEAD/<path> that will always point to the latest version of the path in the default branch. Similarly, for raw content (to download), you can use links à la<owner>/<repo>/raw/HEAD/<path>.

In conclusion, master or main? Hit the nail on the HEAD. 😉

  1. For more context on Terminology, Power and Oppressive Language see for instance ↩︎

  2. Actually, this post was the occasion to change the default branch for the source of this website! ↩︎

  3. Some repos only have a branch called gh-pages (e.g. everything under, in which case that is the default branch. ↩︎

To leave a comment for the author, please follow the link and comment on their blog: rOpenSci - open tools for open science. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)