In this set of exercises, you can continue to work on the Git directory you created in the previous session (we’ll be following the Existing project, GitHub last path). Alternatively (e.g., if you encounter any issues), you can also follow the New project, GitHub first approach and start from a new GitHub repository. Regardless of which option you choose, Exercises 2 to 5 should work the same.

In order to do the following exercises, you should have gone through the steps described in the setup information for the workshop. Specifically, you should…

Some important notes for these exercises:

For adding some R content to your repository, you can either use or create your own scripts or use/build upon the demo_script.R file from the exercises folder.

NB: Keep in mind that you should never put real research data on GitHub. While the data we have provided for use in this workshop are synthetic, we still recommend that you do not upload the data to the repositories you create (also to establish “data protection”-compatible practices for yourself).

Exercise 1

After you’ve made sure that everything works, create a new RStudio project in the existing directory with Git initialized that you should have created yesterday. Next, create a GitHub repository and connect it to your local project.

You first need to create a local RStudio project and associate it with the existing directory you created before (Git should be initialized there and, ideally, all files that you want to be tracked and connect to the remote GitHub repository should already be staged and committed). Next, you need to create a GitHub repository and connect it to your local project. If you need some help (or further information), check the Existing project, GitHub last section in Happy Git and GitHub for the useR.

If you encounter any issues that you cannot solve in the setup, you can also follow the New project, GitHub first route and create a new (and empty) project. The following exercises will essentially work the same either way.

You can create a new RStudio project in an existing directory via File -> New Project -> Existing Directory. As we will start working on the project right away, also check “Open in new session”.

If you have initialized Git in that directory, the Git tab should be visible in the newly opened RStudio instance (ideally, all files that you want to be tracked should have been added and committed).

Next, create a new GitHub repository (if possible, this should be private) and connect it to the existing project/directory. The easiest way to do this is running usethis::use_github(private = TRUE) in R/RStudio (but you can also go through the steps manually).

Exercise 2

If everything has worked, a new instance of RStudio should have opened with the working directory set to the location of your project, the files included in the project folder visible in the Files tab, and an active Git tab. To make extra sure that everything worked, let’s check the Git status of our project via the Terminal in RStudio.
You may want to check which shell the Terminal in RStudio uses via Tools -> Global Options -> Terminal. If you use Windows, you should choose Git Bash (which you should have installed with Git for Windows).
git status

Exercise 3

Now that the project is set up and in sync with the remote repository, we can start working on it. Modify the README file (just add, remove or edit a few words).

Also add (stage & commit) an R script to the repository. You can use the demo_script.R that we have created (and modify it) or use/create your own script.

After modifying the file(s), save the changes (via the Save icon in the RStudio menu or the keyboard shortcut your OS uses for saving files) and stage them in Git.

You can use the RStudio GUI for staging changes (for modified or added files). As a reminder: When you modify existing files and/or create new ones and save the changes, these should be displayed in the Git tab in RStudio and their status will be indicated as modified (M) or untracked (??).

Remember that you should not upload research data to GitHub.

You can stage changed files in the RStudio GUI by checking the boxes in the Staged column in the Git tab.

Do not check the data files/folder.

Exercise 4

After staging your changes, let’s create a commit and push that to the remote repository. Again, remember not to commit and push any research data.
You can do both (committing and pushing) via the RStudio GUI. Remember to write a meaningful commit message.

After staging the changes, simply click on the Commit button in the Git tab in RStudio, write a commit message in the menu that opens up, and then click the Push button in the same menu.

Note: To avoid (accidentally) pushing research data to GitHub, you can add the file or the folder in which it is stored to the .gitignore or you can store the data in a different folder outside the project directory.

Exercise 5

As a final exercise for this session, let’s do the opposite of pushing and pull changes from the remote repository to your local project. Before we can do this, we first need to make some edits in the remote repository. Go to the website of your remote GitHub repository (while being logged in) and edit the README file in the browser (again, just add, remove, or edit a few words). Once you have edited the file (and committed the changes directly on GitHub), pull the changes to your local project.

You can edit the README via the small pen icon next displayed above the content of your README file on the website of your GitHub repository.

Remember that if you edit a file directly via the GitHub web interface, you also need to make a commit (and add a commit message).
After making and committing the edits to the README file via the GitHub web interface, you can simply click the Pull button in the Git tab in RStudio to update your local project.