Git vs. GitHub
- You don’t need GitHub to use Git
- Git = Local (on your computer); GitHub = Remote (on the web)
- GitHub allows you to:
- Share your repositories with others
- Access other users’ repositories
- Store remote copies of your repositories (on GitHub’s server) in case something happens to your local copies (on your computer)
Creating a GitHub Repository
- Two methods of creating a GitHub repository:
- Start a repository from scratch
- “Fork” another user’s repository
- This lecture will walk you through the first method
- NOTE: A repository is often referred to as a “repo”
Start a Repository From Scratch
…OR…
- Go directly to https://github.com/new (you’ll need to log into your GitHub account if you haven’t already done so)
Start a Repository From Scratch
- Create a name for your repo and type a brief description of it
- Select “Public” (Private repos require a paid [or education] account)
- Check the box next to “Initialize this repository with a README”
- Click the “Create repository” button

Start a Repository From Scratch
- Congratulations! You’ve created a GitHub repository.

Creating a Local Copy
- Now you need to create a copy of this repo on your computer so that you can make changes to it
- Open either Git Bash (for Windows users) or Terminal (for Mac users)
- Create a directory on your computer where you will store your copy of the repo:
$ mkdir ~/test-repo
- Note: The tilde (
~) symbol refers to your “home” directory, so this will create a directory called test-repo in your home directory
- Navigate to this new directory using the following command (
cd stands for “change directory”):
$ cd ~/test-repo
Creating a Local Copy
- Initialize a local Git repository in this directory
$ git init
- Point your local repository at the remote repository you just created on the GitHub server
$ git remote add origin https://github.com/your-user-name-goes-here/test-repo.git
- Tip: You can easily copy the URL of your Github repository with the copy to clipboard button

Creating a Local Copy
- Here’s what this process looks like in action:

- Note: Actual appearance may differ slightly depending on your operating system and default settings.
Fork a Another User’s Repository
- The second method of creating a respository is to make a copy of someone else’s
- This process is called “forking” and is an important aspect of open-source software development
- Begin by navigating to the desired repository on the GitHub website and click the “Fork” button shown below

https://help.github.com/articles/fork-a-repo
Clone the Repo
- You now have a copy of the desired respository on your GitHub account
- Need to make a local copy of the repo on your computer
- This process is called “cloning” and can be done using the following command (from either Git Bash or Terminal, depending on whether you are using Windows or Mac, respectively):
$ git clone https://github.com/your-user-name-goes-here/repo-name-here.git
- NOTE: This will clone the repository into your current directory. You can always tell what directory you are in by typing
pwd (for “print working directory”) from the command line.
An example workflow
- Feel free to create a test repo of your own (on your GitHub account) and follow along
- This is not meant to be an exhaustive introduction to all Git and GitHub features
- Best way to learn Git and GitHub are by using them
- If you get stuck, use the resources at the end of the last lecture
Create a new repo on GitHub

- Leave the box next to “Initialize this repository with a README” unchecked, since we will add one later
Copy repo URL to clipboard
- After clicking “create repository”, you will get a screen with some basic instructions for getting started
- They are a good starting point, but we won’t follow them exactly here
- Copy the URL displayed below to your clipboard (yours will be slightly different since it depends on your username and repo name)

Create a new local directory
- Open Git Bash or Terminal (Windows or Mac, respectively) and create a new directory on your computer
- “Step inside” of this new directory with the
cd command
mkdir stands for “make directory”, cd stands for “change directory”, and ls stands for “list”
- Since we get no output after typing
ls, we can see that the directory we created is still empty

Create a new file in directory
- Open your favorite text editor and create a new text file
- Make sure to save it in the directory you just created

Create a new repo locally
- We already created a GitHub repository, but we still need to create a Git repository locally on your computer
- We can see that our new file is now in our chosen directory with
ls
git init initializes a Git repo in our current directory
git status is a helpful command that we’ll make frequent use of
- Does exactly what is sounds like – gives us a “status report” for our local repo

Stage file for commit
- Notice that our newly created file falls under “untracked files” when we look at
git status
- Use
git add to tell Git that we want it to start “paying attention” to this file
- Could have used
git add test_file.txt for the same result, but git add . is often easier if you are okay tracking all currently untracked files

Commit changes
- Using
git commit with a -m after it tells Git that whatever follows in double quotes is the message that we want to attach to this round of changes
- Another call to
git status confirms that there is nothing new to commit (since our first commit)

Check log
git log shows us a history of all commits
- So far, there’s only one

Add link to remote repo
- We now have a remote repo on GitHub’s servers and a local repo on our computer, but they still don’t know about each other
- To establish a link between the two, we paste the URL copied earlier from GitHub as follows

git remote -v shows us that our GitHub repo is now set up as a “remote” repository for our local repo, which allows the two repos to communicate
Push changes to GitHub
- We want our GitHub repo to reflect the changes we’ve made locally (i.e. to contain our new text file)
git push -u origin master tells Git to push our changes to the “master” (or main) branch of the “origin” (or primary) remote
- You only need to include the
-u origin master once, as Git will remember this configuration for future pushes
git push then becomes sufficient, assuming you don’t want to do anything fancy

Check status
- Check status again for piece of mind
- Notice that it confirms Your branch is up-to-date with ‘origin/master’

Check GitHub
- We want to make sure that our changes made it to GitHub safely and indeed they did
- Our text file shows up in the file list

Add README file from GitHub
- How can we can “pull” changes from a remote repository to our local repository?
- To illustrate, we’ll add and edit a README file directly from the GitHub website
- A more common scenario would be that a collaborator makes changes to a shared repository and you want to incorporate those changes into your local repo
- Click on the “Add a README” button on your GitHub repo page, under the list of files

Edit README file from GitHub
- Put anything you want in the README, then press “Commit New File” to commit the file to your GitHub repo
- Note: README files written in Markdown will render in HTML on your repository’s homepage.

Fetch changes from GitHub
- Now that README.md exists on GitHub, we want to “pull” it down to our local repo
- There is a
git pull command that allows you to do this, but it’s recommended that you instead use a combination of git fetch and git merge
git fetch origin tells Git to fetch all changes from the “origin” (primary) remote repo, which we set up earlier with git remote add origin ...

Merge changes into local repo
- Git is now aware of all changes that have been made to the remote (i.e. GitHub) repo
- Still need to incorporate these changes into our local repo
git branch -a shows us that we now have two “branches” stored on our computer: master and remotes/origin/master
master represents the files on our local repo and remotes/origin/master represents the files we pulled from our remote repo
- Use
git merge origin/master to incorporate the changes from our remote repo

Check status
- A quick call to
ls confirms that the README.md file is now in our local directory
git status tells us that we have no new changes and are up-to-date with our remote repo

So you’ve got a repo…. Now what?
- Once you have a repository, you’ll probably make changes over time
- Often, you’ll want to share these changes with others (users, followers, collaborators, etc.)
- Git allows you to track your changes locally (on your computer)
- GitHub allows you to share your changes with the world (or just a few people, depending on whether your repo is public or private)
- But how to communicate between Git and GitHub?
- First we’ll review some basic commands, then we’ll look at a typical workflow
Adding
- Suppose you add some new files or make changes to a local repository (on your computer)
- You need to let Git know that you want it to pay attention to these files (i.e. “track” these files)
- From the directory where the repo is located on your computer (in Git Bash or Terminal, depending on whether you’re on Windows or Mac, respectively):
git add . adds all new files (note the period after add, which represents “all files”)
git add -u updates tracking for files that changed names or were deleted
git add -A or git add --all does both of the previous
Committing
- You want to organize and save “snapshots” of the files you’ve staged for commit
- You type the command
git commit -m "your message goes here", substituting a useful description (between the double quotes) of what changes you made since the last committed changes
- This only updates your local repo, not the remote repo on GitHub
Log
- To see a log of the commits you’ve made locally, type
git log
- Spacebar advances page by page
- Return advances line by line
- Typing the letter “Q” exits the log
Pushing
- Once you are pleased with your local commits, you would like to update the remote repo (on GitHub)
- The command
git push sends your most recent commits to GitHub, updating your remote repository for the world to see
Pull Requests
- If you fork someone else’s repo and make some changes or additions, you may want the original author to merge your changes into their code
- To do so you need to issue a pull request via GitHub
- Don’t need anyone’s permission to fork and make changes, but the original author is not obligated to accept your changes
- Pull requests offer a powerful means of contributing to open source software

What is R Markdown?
R markdown is the integration of R code with markdown
Allows one to create documents containing “live” R code
R code is evaluated as part of the processing of the markdown
Results from R code are inserted into markdown document
A core tool in literate statistical programming
What is R Markdown?
R markdown can be converted to standard markdown using the knitr package in R
Markdown can be converted to HTML using the markdown package in R
Any basic text editor can be used to create a markdown document; no special editing tools needed
The R markdown –> markdown –> HTML work flow can be easily managed using R Studio (but not required)
These slides were written in R markdown and converted to slides using the slidify package