RStudio may just seamlessly detect git once you have it installed. Check if this is the case.
If not, try restarting RStudio again just in case. If that doesn’t work, follow the steps below:
RStudio may need to be told to use git and where to find it. This should only need to be done once.
where git
if you’re on a Windows machine, or which git
on a Mac.The first time that you use git, you will need to set your user name and email in the git config file.
This can be done from within RStudio.
This should only be necessary for Windows machines.
Go to Tools -> Global Options -> Terminal. In the drop down box for New terminals open with, select Git Bash.
Use your GitHub user name for the user.name, and the email address linked to your GitHub account for the user.email.
git config --global user.email "my_email@x.com"
git config --global user.name "github_user_name"
To push commits to GitHub from a repository in RStudio, it’s a good idea to set up a personal access token (PAT) to allow secure access.
There are packages in R to facilitate this. Enter the code below into the R console:
install.packages("usethis")
usethis::create_github_token()
This should take you directly to the GitHub option for creating a PAT (once you’re signed in to GitHub). Accept the defaults, and copy and save the generated PAT. Back in the R console enter the following:
gitcreds::gitcreds_set()
and enter the personal access token from GitHub.
If you run gitcreds::gitcreds_set()
again you should see something similar to this:
Enter 1
in the console to keep the credentials.
You may get an error message that looks something like this:
If you do, just ignore it - it’s an overenthusiastic error message.
The next time that you push changes to GitHub you should not be prompted to enter your user name and password.
(You can just about get away with entering a user name and password instead of a token but GitHub is deprecating the use of passwords in favour of token methods of authentication.)
More extensive instructions can be found at https://happygitwithr.com/credential-caching.html