5 Git and GitHub
5.1 Prerequites
5.2 Setting Up Git and GitHub
There are two main approaches for setting up a new R project that you want on GitHub:
- Approach 1: Create project using Git, then download it to your machine
- Approach 2: Begin with pre-existing
RStudioproject, and then connect to GitHub
The first approach tends to be easier (and is new to these notes).
5.2.1 Approach 1: Create project using Git, then download it to your machine
Within here, there are two approaches. One of them uses GitHub Desktop, a standalone program, and the other uses github.com itself, the website.
We suggest using GitHub Desktop, as it will make managing your Git repositories easier after setup.
5.2.1.1 Use GitHub Desktop
GitHub Desktop is a standalone program for working with Git and GitHub. It is very beginner-friendly.
Here is a video describing how to use it: https://www.youtube.com/watch?v=8Dd7KRpKeaE
I have not watched it myself, but apparently it makes it very easy. Note that it contains more information than you need, so you can stop when you are set up.
5.2.1.2 Use GitHub
You can also create a new repo directly on GitHub, and then clone it (Git-speak for download it). Here are the instructions: New project, GitHub first.
5.2.2 Approach 2: Begin with RStudio project on your machine, and then connect it to GitHub
See the video git.mp4 for this process illustrated.
In RStudio, open your project.
Then, run the following code.
5.2.2.1 Install usethis
Firstly, ensure usethis is installed:
if (!requireNamespace("usethis", quietly = TRUE)) {
install.packages("usethis")
}5.2.2.2 Introduce yourself to Git
Then, if you have not set Git up before, you’ll need to run the following. Note that by “not set up Git”, I do not mean whether you’ve installed Git, but rather whether you’ve given Git basic authorship information. If you’re not sure, you can run the following:
gert::git_config_global()I am not exactly sure what it looks like if you’ve not set up Git authorship information, but the key thing is that it will not have a row where the first column is user.name and the second column is your name, and a row where the first column is user.email and the second column is your email address.
If you can see you’ve not set user.name and user.email, then run the following, swopping NULL with your name and email address:
# set your username and your email address
username <- NULL # replace NULL with e.g. "Miguel Rodo" (be sure to include the quotes)
email <- NULL # replace NULL with e.g. "miguel.rodo@uct.ac.za"
stopifnot(!is.null(username)) # just checking you set this
stopifnot(!is.null(email))
gert::git_config_global_set("user.name", username)
gert::git_config_global_set("user.email", email)5.2.2.3 Initialise Git for the project
Finally, you are ready to initialise git for that project:
usethis::use_git()When it asks you whether you want to commit the extra files, say yes. Be aware that you don’t type “Absolutely” or “Definitely” or whatever corresponds to “yes”, you just type the number that corresponds to yes.
After running the above, to get the Git pane to appear in the top right corner (alongside Environment and History), you need to close and re-open RStudio.
You will not need to install usethis and set Git authorship information for each project, but you will need to initialise Git for each project.
5.2.2.4 Connect Git to GitHub
Run the following code:
usethis::create_github_token()Name the token, set the expiry date and click on Create. Then copy the token (it looks like ghp_akerjlwk4j5qlk45j).
Then, run the following code:
gitcreds::gitcreds_set()and paste your token.
5.3 Further resources
- Happy Git with R:
- Set up guidance:
- Description of how Git works and how to use it:
- Some Git Basics, and then the next few sections