1 min read

R project first then git

The primary goal of this blog is to remind me how to link my local R project to a newly created git repo. The content are mainly from the chapter 17 of the book Happy Git and GitHub for the useR by Jennifer Bryan.

Step 1

Get a local R project and its folder ready.

Step 2

Create github repo

Step 3

Connect the local R project folder to the remote github repo

  • git init or usethis::use_git()
  • make a commit
  • in terminal git remote add origin <my-repo.git>
  • change the local branch name from master to main if applicable

if error fatal: refusing to merge unrelated histories:

  • git pull origin main --allow-unrelated-histories
  • deal with any merge conflicts, then commit again

push, make sure working tree clean when git status

  • git push --set-upstream origin main

Some notes

  • the name of the local folder doesn’t need to be the same as the name of the repo
    • git remote add origin <my-repo.git> is able to connect them
    • however, this might be the cause of the error fatal: refusing to merge unrelated histories. I’m not sure about this.
    • you can read more from this stackoverflow post, which I didn’t read through :)