usethis::use_git_config(user.name="myUserName",user.email="my.name@ilovespatialecology.ac.uk")Linking RStudio to Github
The first step in this process for linking RStudio to GitHub is to check that you have have git software installed on your machine. We can do this by typing “git –version” in the Terminal tab in RStudio
Note, if you are working on a Windows computer, the command is just “git version” (without the “–-”).
If you have git installed you will see the version you have printed after running this command (just press Return to run commands in the Terminal).
If you do not have git installed, you then have a few options. The first option (for Mac or Windows) is to download Github desktop (from here) and then go back to the Terminal and check for git again.
If you still don’t see a version of git then do one of the following.
1. For Mac users
For Mac users download the Homebrew package by typing (or pasting) the following into the terminal and running it (you may be prompted for your password and/or asked if you want to continue - you do):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"When Homebrew has finished installing, you should see a message indicating that the install was successful and some hints for getting started:
Now, type in the following and run inside the Terminal to install git:
brew install gitAnd wait for git to download and install. Then check again to see if there is now a version of git on your machine.
2. For windows users
Windows users can download the git executable (.exe installer) from here. Just download and run the installer, then check again in the terminal that a version of git has been installed.
Linking RStudio to GitHub
To link an RStudio project to GitHub we can use functions from the usethis package from inside the console (this is the tab to the left of the Terminal tab in which you may have just been working).
Install the usethis package and load the library (you can do this in a script or by typing straight into the console and pressing enter to run each line of code).
install.packages("usethis")
library(usethis)If this is your first time linking RStudio to GitHub then the first step is to configure git with your GitHub account.This function takes two arguments which are simply your GitHub user name and email address.For example (replace the arguments with your actual details):
After running this command, run the following to get a situation report on your linked GitHub account:
usethis::git_sitrep()This should return a bunch of information but the key ones to look out for are that RStudio now recognises your GitHub account. If it does then it should show something like:
Okay, with this set up we now need to generate an access token (which is basically just a temporary password that RStudion uses to check that this is definitely you and that it is safe to pass information between your project and your GitHub repository).
We do this by typing and running the following function name (we don’t add any arguments here, we just run the code as written below):
usethis::create_github_token()This should take you straight to the GitHub URL that offers an access token.
Give a description of the token on the box provided and leave all other settings as the default.
Then scroll down until you see “Generate token” and click this button:
You should copy this token (which is just a bunch of letters and numbers) to the clip board when given on the subsequent screen.
Now return to the RStudio console and type and run the following (just the function as below, without any arguments):
gitcreds::gitcreds_set()When you have run this line, you will then be prompted to enter the token that you just copied from GitHub (paste it after the line that reads “Selection”):
Then press enter.
With this step complete you can now create a project and link this to your Github repository.
Open a new project and select Version Control from the options:
Then choose Git from the next set of options:
Then copy the URL of your gitHub repository, give your project a name and select a folder on your machine where you want to save it (in the below example I have copied one of my repositories, yours will be called something else).
Now you can start coding. As a simple example I will create a new script in the project I just created, write a line of code and then save to file. Once saved I can commit and then push to the GitHub repository.
Here are the steps:
- Write some code, save to file then open the Git tab in the top-right panel:
Note that your saved script (“demo.R” here) should appear.
- Click the button under “Staged” for this file and then click “Commit”. This will take you to the following screen:
- Add in a message to the “Commit message” box as above and click “Commit”.
A git commit dialogue box should open giving the details of the commit:
- Finally, push this commit to the GitHub repository (note you don’t have to do this after every single commit, just when you are ready to update your work on GitHub).
The “push” option is the green arrow symbol in the top right and clicking this will return a message with the details of the push showing which repository has been updated:
That’s it. From now on you can commit every time you save and (perhaps every few commits) push these changes to your GitHub repo where you can keep track of all your changes (a life saver for confused coders - and we’re all confused coders sometimes!).
See here for an example on when/what to commit and how often:
https://github.com/dennisMatt/meles/blame/main/melesExercise.R