1. Create a new repository in GitHub
- Log in to your GitHub account at https://github.com/.
- Create a new repository either by clicking the green
New button or from the dropdown menu on the top
- Name the repository Exercise1.
- Select the checkbox to Add a README file, then click Create
2. Edit the README.md document
- Add a line with some extra information.
- Commit the changes.
1. Configure Git and RStudio.
If you haven’t already configured RStudio to use git, see the initial
set up document. Skip this step if you’ve already done this.
2. Import your own existing repository from GitHub in to
- Go to the main page of your Exercise1 repository in GitHub.
- Click on the green Code button.
- Copy the HTTPS URL. The little clipboard to the right of the URL
will copy the URL text.
- In RStudio, create a new project. Select Version Control -
Checkout a project from a version control
- Select the Git option and paste the URL from GitHub
in to the Repository URL text field. The other text
fields should then populate themselves. You can change the directory
location if necessary.
- When you click Create project, the repository from
GitHub should be cloned to your computer and you should see the
README.md file in the Files tab. RStudio will also have created a .Rproj
file, and a .gitignore file if you didn’t already have one of
3. Commit and push the new file(s).
- Make sure the Git tab is selected in the Environment/History/Git/…
panel. You should see an Exercise1.Rproj file with a yellow square next
to it. The yellow square with a question mark shows that the file is
- Select the Staged checkbox to add the file to git, this should
change the status to a green A, showing the file has been added.
- Do the same for the .gitignore file if it’s showing there.
- Click the Commit button from the git menu. This should bring up a
new window. Add a Commit message “Initial commit from RStudio” or
similar. Then click Commit.
- Close the window. The files should no longer be showing in the git
panel, but there should be a message saying “Your branch is ahead of
origin/master by 1 commit”.
- Click the Push button to push the changes to the
- Go to GitHub in your browser and check that these files can now be
seen in the repository, you may have to refresh the browser.
4. Edit the README.md file.
- Back in RStudio, click on the README.md file in the Files tab. It
should open in the panel where you’ve been writing scripts.
- Add another line of text to this file and save it.
- In the Git panel you should see that the README file now has an
M in a blue square next to it, showing that the file
has been modified.
- Select the Staged checkbox, click commit, add a commit message, and
commit the changes.
- Push the changes to the remote repository.
- Go to GitHub, refresh the page and check that the README file has
- Edit the README file from GitHub this time, add another line of text
and commit the changes.
- In RStudio, pull the latest changes using the Pull
button. Check that the README file now shows the last line of text added
5. Create a new script
- Create a new R script. Use the iris dataset (this comes with base R
so if you type iris in the console you will see the data) and
plot the Sepal.Length vs Sepal.Width.
- Save the script. Make sure it’s in the same folder as the other
files for this project.
- The saved file should appear in the Git tab with yellow squares next
to it. Add and commit the file, then push it to the remote repository.
Check that it has appeared in GitHub.
Forking a repository
- Fork the repository by clicking the Fork button in
the top right hand corner in GitHub.
- Clone the repository in RStudio - follow the same steps as in
Exercise 2.2, from clicking on the green code button, through to the end
- You should be able to see all the files from the
March_example_R_project repository in your Files panel.
- Open the custom_boxplot.R script by clicking on it in the Files
panel. Make some changes to the iris_boxplot function, you could add
some colour to it, give it a title, or even change what is being
- Save the file, commit the changes making sure you include a
descriptive comment of what you’ve changed, and push the changes to the
- Check in GitHub that the custom_boxplot.R file has been
- Submit a pull request.
Exporting a project from RStudio to GitHub
You may have a project, or just some code in R, that didn’t start out
under version control but that you would like to bring under version
control and put into GitHub.
This exercise uses an existing R project that can be downloaded here.
Alternatively, you could use an R project of your own.
1. Open the project and bring it under version control.
- In RStudio, open the project named Exercise4 by clicking on the
Exercise4.Rproj file. This should contain a few files.
- Go to Tools -> Project options -> git/SVN.
The drop down box for Version control system should say
“None”, but have a Git option. Select Git.
- A pop up should appear asking whether you want to initialise a new
git repository for this project. Select yes. RStudio may need to
restart. Make sure you’ve saved any files you might have open in RStudio
before doing this.
- There should now be a Git tab present in the Environment etc panel.
Move to the Git tab and commit the files. The push and pull options
should be greyed out as you’ve only set up a local git repository so
2. Exporting the project to GitHub.
If you are using a Windows machine make sure your Terminal is using
git bash. If you’re using a Mac or Linux, skip this step.
- Go to Tools -> Global Options -> Terminal. In
the drop down box for New terminals open with, select
- Go to GitHub and create a new repository with the same name as your
R project - Exercise4.
Do not select Add a README file or a .gitignore
- You should see a few options with lines of code. Go to ..or
push an existing repository from the command line and copy the
text using the clipboard button on the right.
- In RStudio, go to the Terminal tab (next to the Console). if you
don’t have a terminal open, go to Tools -> Terminal -> New
- Paste in the lines of code from GitHub.
- In GitHub, go to the main project page and check that the files from
RStudio are now there.
Exercise 5 [Optional]
Create a new branch for one of your projects.
- Use the git menu option in RStudio (icon with purple rectangles) to
create a new branch for one of the projects you have created or used in
- Make some changes to the code, commit the changes, push to
- In GitHub, merge the new branch into the main branch by submitting a
pull request and merging.
Exercise 6 [Optional]
Create a GitHub repository for your existing scripts.
- Create a new repository called R_courses (or similar).
- Pull the repository to your RStudio session (follow the steps in
- Move or copy any useful files and scripts from the courses that you
have done so far into the project folder.
- Commit and push the changes.
Cleaning up your GitHub repositories.
You can always delete your repositories for these exercises, select
the Settings option, scroll down to the bottom of the
page, and there should be an option to delete the repository.