Git will feel foreign for a while, but you will get a lot of practice in this class
A great resource, from which I’ve heavily borrowed is Jenny Bryan’s Happy Git and GitHub for the useR
Another good reference is the GitHub Git Cheat Sheet
Git is a version control system that manages as set of files as a repository, or a repo.
For the long version of why, see Excuse me, do you have a moment to talk about version control?
For today, just know:
Here are 5 basic steps to set up Github to interface with R Studio.
If you have not already registered for a github account, do so now. Visit github.com to set up your account.
Note a github
account can serve as a statistics / data science repository for future employers. Choose an appropriate and searchable id.
If you have an account, go ahead and log in. We will use this soon.
R is currently on version 4.2.1, if your version is much older than this (3.X.X), you will want to update it here.
Updating R can be a bit painful, but keeping current is good practice.
R Studio is constantly adding improvements and updates, including a git interface.
To stay current, consider downloading the Preview version and updating at the start of every semester.
If you are lucky, git is already installed on your computer.
To check you have git, open the terminal or command line, and type git --version
. In newer versions of R Studio there is a command line tab.
If git --version
does not return an error, then you are in luck. Otherwise, git needs to be be installed.
If you have not already done so we need to initialize git with the following commands in the terminal:
git config --global user.name 'First Last'
git config --global user.email 'who@montana.edu'
Note the email should correspond to the one you used to sign up for github.
Our goal is to work locally, in R Studio, and push updates to Github.
First, go to your github page and create a new repo. To do this,
Next, we will clone this repo and create an R Studio project associated with it.
Now we want to create a local git repo associated with the one hosted on github.
First we need to set up version control in R Studio (more details here)
We now have created a local git repo.
Click on the terminal window and type git status
, you will see our current work is up to date.
Now click on README.md in the file pane and add a new line that says, “modified in R Studio” and save this file.
For security reasons, remote access to GitHub requires an authorization token - rather than a passsword. You may need to set up a token see documentation: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
Now return to your github page. If you refresh, you should seee a new commit that contains the additional info we added in R studio.
Go ahead and modify the README.md in github to add a line stating modified in github.
Commit the update along with a message. We will now see that there have been three commits.
The final step is to call a pull
command from R Studio to bring those updates to your local repo.
It is always good practice to pull
before a push
, especially as you start to collaborate with others.
We will just cover a few basic commands for now, but the github cheetsheet is a good reference:
add
: this adds or stages a file to be committed. It can also be used to track new files. Create an .Rmd file and save it within this directory as sample.Rmd . The file will not immediately show up in the Git window, we need to start tracking it. In the terminal type git add sample.Rmd
. Note: sometimes this does not seem to work the first time and you might want to try gitKraken or some other option.
status
: shows the current state of the git repo. Type git status
commit
: saves the version of all files in the repo. Only updated files that have been staged are saved in this version. In the terminal type git commit -m 'added sample.Rmd file for tracking
.
pull
: will retrieve and merge from the remote (github in this case). Type git pull
.
push
: sends updates that have been committed to the remote. Type git push
. Now inspect your repo on github.