In this lab, we will learn how to use GitHub for version control.
Goal: by the end of this lab, you will be able to commit, push, pull, and send pull requests.
What is version control?
Version control is a mechanism for collaborative software development that preserves histories. The major objective is to keep track of all the different changes that get made, so that nothing is lost and you can always go back to any previous state.
Version control systems have been in use for a long time, and many different systems have been used. Currently, git is the dominant verson control system. git is a standalone command line application. Interfaces to git include an official git GUI, and a built-in tab in RStudio.
Most of the things we do with GitHub can be done in RStudio, but it is occassionally necessary to use the command line (and you should embrace that!).
For questions about git and GitHub, please see Jenny Bryan’s excellent book on the subject: Happy Git and GitHub for the useR. In particular, please read the troubleshooting chapter when you run into trouble!
In this lab, we will focus on the use of two functions, as detailed here:
If you see errors in your output, investigate them!
Setting up your GitHub token
First, you need to obtain a token from GitHub, if you don’t have one already. Try running:
gitcreds::gitcreds_get()
<gitcreds>
protocol: NA
host : NA
username: NA
password: <-- hidden -->
If you get an error or other unreasonable output, you need to tell RStudio about your GitHub token. The token is a long random string of characters that start with ghp_. If you have the token already, run gitcreds::gitcreds_set() to set it.
gitcreds::gitcreds_set()
If you don’t have the token already, run usethis::create_github_token() to create one, and then use gitcreds::gitcreds_set() to set it.
usethis::create_github_token()
Please see this article for comprehensive documentation.
The gh_token_help() function is also helpful for diagnosing issues with your token. Note the “Token scopes” in the output below.
gh_token_help()
• GitHub host: "https://github.com"
• Personal access token for "https://github.com": <discovered>
✖ Can't get user information for this token.
ℹ GitHub API error (403): Resource not accessible by integration
Making a contribution
In this first group exercise, each student will work individually to send a pull request to the maintainer (me) of a single repository. When you make a contribution to someone else’s repo, this is how you will do it. (See also https://happygitwithr.com/fork-and-clone.html)
Setting up the local repo
Run usethis::create_from_github("sds270-s24/ourpackage", fork = TRUE)
Caution
READ THE MESSAGES!!
STOP. If Step 1 worked, proceed to remote verification. If Step 1 failed, pursue the following steps as necessary. THINK before you act!
Run usethis::pr_init("<NAME>"), where in place of <NAME> you write your one-word name for the new branch (all lowercase, no spaces or punctuation).
Add your first and last name, with a link to your GitHub user page, to README.md.
Commit your changes.
Push.
Run usethis::pr_push() to send a pull request.
Caution
STOP
I will resolve all pull requests.
Collaborating on a project
Sync your fork
You need to sync your fork regularly (like every time you finish a pull request). Entering these commands in the Terminal should do it.
git pull upstream main
Note that usethis::pr_init() attempts to perform this operation, so if you are able to use pr_init(), you may not have to sync your fork manually via the command line.
Merge conflicts
If two or more people commit changes to the same part of the same file, a merge conflict is inevitable. With good git hygiene and clear project roles, the probability of a merge conflict can be minimized. But they will happen and you need to know how to resolve them.
A side-by-side comparison of the set of changes is helpful. A diff is a way to view these changes. Several editors will perform this comparison. I use meld. Another program is opendiff. You can use whatever you want!
Getting credit
Please respond to the following prompt on Slack in the #questions channel:
Prompt: What would help improve your comfort level with GitHub?