Git and GitHub

Install Git

In this course we will use Git, a version control system that allows you to track changes and share your code, and you will submit most of your assignments through GitHub, a website that implements Git and allows you to share code with others. Most software developers use these tools, and a large number of R packages are hosted and can be downloaded from GitHub.

  • Windows

    • choose defaults for everything
  • Mac

    • You may get an error message that you cannot install this because it is an unidentified developer. Change your settings so you can install it.

Check that RStudio Knows Git is installed

  1. Go to Global Options > Git SVN
  2. Check that Git executable is not empty
  3. Click enable version control interface for RStudio projects if it is not already checked.

Create a GitHub account

Here

- Choose available Username (keep it professional)
- Choose the free plan

Now let us know your GitHub account name is. Go here, click on “New Issue”, make the title “Add me to the organization”, and click on “Sumbit new issue”.

Submitting assignments

Fork the repository.

For each assignment we will create a new repository in https://github.com/UW-POLS503. First you need to make a copy of that repository to your GitHub account. We call that to create a “fork”.

  1. Go to https://github.com/UW-POLS503/Assignment_00
  2. Click on “Fork”. Now you will see: https://Github.com/<your_account>/Assignment_00.

Clone your new repository

You created a copy of the assignment repository in your GitHub online account. Now you need make a copy of it in your local machine. We call that to “clone” a repository.

  1. Open up RStudio
  2. Go to File > New Project > Version Control > Git
  3. Enter the URL of your repository: https://Github.com/<your_account>/Assignment_00. If you don’t give it a “Project directory name”, it will create a directory named “Assignment_00” that will have the original files in the repository plus an .Rproj and .gitingore file.
  4. Select the directory you want to have the repository in. We suggest you create a directory to store all future assignment repositories. Ideally this would be within your directory for the class.

Edit the README.md file

  1. Open the README.md file and edit it: add your name.
  2. Save the README.md file.

Commit and Push your changes (for the first time)

  1. Access a terminal from within RStudio: Tools > Shell…

  2. In the terminal enter the following commands:

git add *
git commit -m 'my first commit'
git push
  1. Git is going to ask for you GitHub account. Type it and Press enter.
  2. Then it will ask for your GitHub password. Type it. When typing it you are not going to see any number being typed in the terminal but it’s actually happening. Press enter after typing the password and you should be good to go!
  3. Next time you want to commit and push you can do it from within RStudio. You don’t need to do it from the console anymore.

Commit and Push your changes (after first time)

  1. Use the Git pane to commit the changes:
    • Select the files you changed
    • Click on “Commit”
    • Add a commit message and commit
  2. Push your changes to the repository

Pull Request

Now that you pushed your assignment to your online fork of the repository, the last step is to send a pull request to add your changes to the main repository.

  1. Go to your online version of the repository: https://Github.com/<your_account>/Assignment_00
  2. Click on “New Pull Request”
  3. Click on “Create new pull request”
  4. The instructors will be informed that you are done with the assignment.

Install Github Desktop (optional)

Another graphical interface for Git and GitHub, not directly tied to RStudio, is GitHub Desktop. You may want to install that as an alternative to working with GitHub through RStudio.

Tips

Using Git with R Markdown

If you are using git with R markdown, you will want to add a few patterns to your https://help.github.com/articles/ignoring-files/ file to ignore some temporary files.

# Ignore temporary files created by R markdown
*.utf8.md
*.knit.md
# Ignore knitr cache
*_cache/

Using Git with LaTeX

If you are using git with R markdown, you will want to add a few patterns to your https://help.github.com/articles/ignoring-files/ file so that git ignores the many intermediate files that LaTeX creates when compiling a document.

For those more comfortable with git and LaTeX see this Stack Overflow post on a git+LaTeX workflow. Copy the patterns from GitHubs collection of useful gitignore patterns for TeX.

References

Note that although for this course we use git through either the GitHub website or client, or RStudio’s interface, git is often used in a shell. If you are unfamiliar with a shell, here are some places to get started:

Parts of this module are abstracts from Hadley Wickham’s module on Git and GitHub