Showing git branch in R terminal

Adding to your .Rprofile

git
R
RStudio
Author

Nick Twort

Published

27 Jul 2023

If you want to show your current git branch while using RStudio, like this:

Just add the following to your .Rprofile1:

git_prompt <- function() {
  
  git_branch <- suppressWarnings(system("git rev-parse --abbrev-ref HEAD",
                                        ignore.stderr = TRUE, intern = TRUE))
  
  if (length(git_branch) > 0) {
    console_msg <- paste0("[", git_branch, "] r> ")
  } else {
    console_msg <- "r> "
  }
  
  options(prompt = console_msg)
  
  invisible(TRUE)
  
}

git_prompt()

Footnotes

  1. You can open this file with usethis::edit_r_profile()↩︎