Version Control with Git and GitHub
Friday, October 23 — a history for your code
Friday, Oct 23, 12:10–1:00 PM, Asmundson 242. Which version of your script made Figure 3? What did you change since your committee meeting? Git keeps a record of every saved version of your code, so you can answer those questions and undo mistakes. We’ll set it up in RStudio, with no command line, and use it on your Problem Set 4 code. Bring your laptop.
Before you arrive (10 minutes)
Setting up git is the slowest part of the session, so please do these first. If something doesn’t work, come anyway and we’ll fix it in class.
- Install git.
- Mac: open the Terminal app, type
git --version, and press Enter. If git isn’t installed, macOS offers to install the command line developer tools. Accept. - Windows: install Git for Windows and accept the defaults.
- Then restart RStudio.
- Mac: open the Terminal app, type
- Make a free GitHub account at github.com. Students can also apply for free extras through GitHub Education (optional).
- Introduce yourself to git. In the RStudio console, run this with your own name and the email you used for GitHub:
# install.packages("usethis")
usethis::use_git_config(user.name = "Your Name", user.email = "you@ucdavis.edu")Part 1 — Git on your own computer (everyone)
Git tracks one folder, called a repository. In RStudio, that folder is an RStudio Project.
- Make a project for your Problem Set 4 work. Go to File → New Project → New Directory → New Project, name it (e.g.
pls206-ps4), and tick “Create a git repository”. Move your PS4 script into the new folder.- Already have a project? Run
usethis::use_git()in it instead.
- Already have a project? Run
- Commit. A commit is a saved version with a note attached. In the Git tab (top right), tick the files to include, click Commit, write a short message about why you made the change (e.g. “log-transform the response; residuals were fanning out”), and click Commit again.
- Change something and commit again. The Commit window shows the diff: removed lines in red, added lines in green.
- Look at the history. The clock icon in the Git tab lists every commit. Click one to see exactly what changed.
- Break something, then undo it. Delete a chunk of your script and save. In the Git tab, choose More → Revert… (or right-click the file) to restore the last committed version.
That’s the whole daily routine: edit → save → commit.
Part 2 — Back it up on GitHub (if your setup worked)
GitHub keeps a copy of your repository online, a bit like Dropbox but with the full history attached. It’s how you share code with your advisor, collaborate with co-authors, and publish code alongside a paper.
usethis::create_github_token() # opens GitHub in your browser; click "Generate token", copy it
gitcreds::gitcreds_set() # paste the token when asked
usethis::use_github(private = TRUE)use_github() creates a private repository on your GitHub account and uploads your project. After that, the Push button (green up-arrow) in the Git tab sends new commits to GitHub.
Keep homework repositories private. A public repository of problem set answers is visible to everyone, including next year’s class.
If something goes wrong, usethis::git_sitrep() prints a report of what is and isn’t set up. Bring that output to class or office hours.
Habits worth keeping
- Commit small and often, with a message that says why. Future-you is the main audience.
- Don’t commit large data files or passwords. List them in the
.gitignorefile in your project folder, and git will leave them alone. - One project per analysis. One folder, one repository, one history.
Extra resources
- Happy Git and GitHub for the useR — the guide for R users. Covers installation and the RStudio workflow in detail, and how to fix things when setup goes wrong.
- Pro Git — the free, complete git book, for when you want to know what’s going on underneath.
Back to the full Friday discussion schedule.