1 Git Cheat Sheet
Special thanks to Dr. Matt Higham for sharing these instructions with us.
1.1 Getting Started with Git
This section outlines the steps to get Git up, running, and connected to R Studio. There are accompanying videos at https://highamm.github.io/dataviz334/02-software.html#installing-git-and-using-github-class-prep, which may provide a little more guidance than the written steps below.
1.1.0.1 Pre-requisites
Install both
RandR Studiolocally.Install Git, following https://happygitwithr.com/install-git.html. Note that you can get to the Shell within
R Studiofrom “Tools -> Shell…”Make sure to follow the directions with your associated operating system (likely either Windows or Mac).
After installation, in the Shell, type
git --versionto verify that you get a version number
Create a GitHub account at https://github.com/ with the “Sign Up” button.
1.1.0.2 Connect GitHub to R Studio
Install the
usethispackage inR.Give Git your credentials with the
use_git_configfunction in theusethispackage: e.g.,usethis::use_git_config(user.name = "Ivan Ramler", user.email = "iramler@stlawu.edu").- Note that the email provided must match your GitHub email address.
Generate a Personal Access Token (PAT) with
usethis::create_github_token()- You can change the recommended scopes if you want but you should at least leave “repo”, “user”, and “workflow” checked.
- By default, the PAT will expire in 30 days. People have varying levels of comfort with security: I will somewhat shamefully admit that I click the “No Expiration” option.
Install the
gitcredspackage and then, inR, rungitcreds::gitcreds_set()and paste in your PAT when prompted.- Run
gitcreds::gitcreds_get()orusethis::git_sitrep()to verify that your PAT was accepted.
- Run
1.1.0.3 Create a Repo, Commit, Push, and Pull
On your GitHub account site, click the “
+” sign in the upper-right corner and select “New Repository.”Give the repository a title and make sure to have the option to create the README file checked.
If your repository will eventually contain data or other code that cannot be made public, you should make select the “Private” option for the repository.
Back in
R Studio, go to “File -> New Project”, select “Version Control” from the options, and then select “Git”. Then, copy and paste the GitHub repository web address, give the folder that will be created a name (giving it the same name as the repository is not required but is something that I find convenient), select where that folder will live on your local computer, and click “Create Project”.- If all goes well, you should have a new project with a “Git” pane in the upper-right window. If all does not go well, it may be that
R Studiocannot locate Git on your local computer and that we need to do a bit of troubleshooting.
- If all goes well, you should have a new project with a “Git” pane in the upper-right window. If all does not go well, it may be that
1.2 Command Line Cheat Sheet
1.2.1 Basics
Staging Local Changes and Making a Commit:
git add example_file.R
git commit --message "A sample commit message"Pushing Commits to GitHub and Pulling from GitHub:
git push
## which usually is short for
git push origin main
git pull
## pull but only if there is no merge conflict
git pull --ff-onlyExamining the log (recent commits and other info): git log
Locating the Web Address for a Remote Repository: git remote -v
1.2.2 Branches
Switching to Another Branch
git checkout other-branch-nameCreating a Branch:
git branch name-of-new-branch1.2.3 Merging and Merge Conflicts
Rolling Back to an Older Version:
## go back one commit
git reset HEAD^
## go back two commits
git reset HEAD^^
## go back to a specific commit by providing the SHA number
git reset SHA-numberMerging (in this example, merging branch develop to main):
git checkout main
git merge developAborting a Merge (if you cannot resolve a conflict):
git merge --abort