Learning git and GitHub

Basic Vocabulary

  • Version Control A system for maintaining current and past copies of all of your files and a record of all the changes you have made;
  • git Version control software for tracking your project on your local computer
  • GitHub A free, public remote website where your project is permanently hosted
  • Repository Any collection of files that belong to the same project, plus a few special git files that set up the version control. Also called a repo by hipster programmers.
  • Clone Create a local copy of a repository from the GitHub website to your own website. You can clone from your own repository or anyone else’s that is posted on GitHub. Once the files are on your local computer you can edit or use them as you like. However, you can only change files on GitHub for the repositories that you yourself created or for projects that you have been invited to collaborate on with others.
  • Commit (the noun) A snapshot of your file system at a particular time. Since your last commit, it keeps track of the files you currently have, the ones you have deleted or added, and any changes you have made to files.
  • Commit (the verb) When you commit, you are taking a snapshot of your files at the current time. You must add a summary of the commit that briefly describes what changes you have made (such as “add new homework file”). Note that all commits affect only the local copy of your repository. They do not affect the remote copy that is stored on GitHub
  • Push Pushing a set of changes means copying them from your local repository up to GitHub.
  • Pull(=fetch) Pulling means copying the repository in its current state on GitHub down to your local repository. This would incorporate any changes that others have made (and pushed) in a collaborative project.

Creating a webpage hosted by github for an r project

Set the location for the new project

  • from the Project pull-down (upper-right corner of RStudio window) choose Close Project
  • from the console run the command getwd()
  • from the Session menu set the working directory (choose directory) to where you want the new repository to be created
  • from the console run the command getwd() a second time (to confirm new location)

Create the new project and move to it

  • from the console run command library(devtools)
  • from console run command create_project("ProjectName") This command creates the project and opens a new RStudio environment
  • close the original RStudio environment and move to your new RStudio project window

Add git functionality to the new project

  • from the console run the command library(devtools) Remember the devtools package is not open in your new project
  • from the console run the command use_git()
  • respond affirmative to the question “Is it ok to commit them?”
  • respond affirmative to the question “Restart now?”

Clean up the new project

  • Using the Delete icon in the file panel, remove the R folder
  • click on the .gitignore file
  • add line gitignore file: *.Rproj
  • optionally add lines gitignore file: *.pdf for other file types or individual files to ignore
  • optionally add a license with use_mit_license(copyright=“YourName”)

Commit changes to the new project

  • Click on the terminal tab to open a new terminal for a git commit
  • display a list of changed files git status
  • stage the changes git add -A
  • display the staged changes git status
  • git commit -am ‘first commit’ (makes local commit)
  • git status
  • git push (push the changes up to the GitHub repo)

Build and host a website associated with your new repository

Commit changes from the terminal

  • switch from Console to Terminal panel
  • git status
  • git add -A
  • git status
  • git commit -am 'add webpage'
  • git status
  • git push

Host the “index” webpage

  • Return to GitHub public repository
  • click on your name to go to your sign in
  • Go to “Settings”
  • Scroll down and under “Pages” and “Branch” change from “None” to “main”
  • Wait a minute (! could be very slow!!) and click the new link at “Your site is live at…”
  • New webpage should display
  • Note the address: https://UserName.github.io/RepoName/
  • Example: https://ngotelli.github.io/Trial/

Optional: rename “master” branch to “main” branch

These instructions are modified from Scott Hanselman’s blog

  • from the github General page, find the box to change “master” to “main”

  • Now go to each of your local clones of this repository and run the following from the terminal prompt ($ is the prompt before the command)

$ git checkout master
$ git branch -m master main
$ git fetch
$ git branch --unset-upstream
$ git branch -u origin/main
$ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
# git config --global init.defaultBranch main

These commands need to be issued only once for each local copy of the repository. These commands do the following:

1. Go to the master branch
2. Rename master to main locally
3. Get the latest commits from the server
4. Remove the link to origin/master
5. Add a link to origin/main
6. Update the default branch to be origin/main
7. Change git configuration to recognize main (may only need to be done once)

Basic workflow for git from the terminal

Check things out before starting your work

  • git status (make sure your local repo is not ahead of the remote)
  • git pull (just in case there was a change made from another location)
  • git status (ready to start working)

Make changes

  • modify files, including knitting new versions of html pages or pdfs
  • delete files
  • add files

Stage changes

  • git status
  • git add -A
  • git status

Commit changes

  • git commit -am 'write a brief informative commit message' (no caps or punctuation, present tense)
  • git status

Push changes up to GitHub

  • git push
  • git status
  • git pull

Summary of workflow