Git and GitHub

A brief introduction

Git

  • version control system
  • works offline (repositories exist on your computer)
  • tracks changes via commits
  • has a command-line interface and integrations with GUIs (like RStudio)

GitHub

  • web-based platform built around Git
  • provides a remote location for hosting Git repositories
  • enables collaboration
  • offers other features for project management (pull requests, issue tracking)

Our Git/GitHub goals

  • For you: Keep track of progress on projects
    • Go back when you need to
    • Don’t lose old work
    • Easily search the history of a project
  • For others: Share your work
    • Have a place to store and link to code
    • Read and interact with others’ code

There is a lot to learn about this topic and I am not an expert on everything!

What we won’t cover

  • Collaboration
    • When multiple people are working on the same GitHub project, things get a little more complex
    • I went though almost my whole PhD without working on shared GitHub projects and only now do I feel semi-confident collaborating!
    • I think it’s best to figure things out in your own projects first
  • Git on the command line
    • There are a lot of functions you might hear about (git fetch, git merge, etc.)
    • RStudio and GitHub will have everything we need!

Git

  • version control system
  • works offline (repositories exist on your computer)
  • tracks changes via commits
  • has a command-line interface and integrations with GUIs (like RStudio)

GitHub

  • web-based platform built around Git
  • provides a remote location for hosting Git repositories
  • enables collaboration
  • offers other features for project management (pull requests, issue tracking)

Workflow

Create a repository (clone from GitHub, or create on your computer and connect to GitHub)

  1. Write some code!
  2. When you complete “something”, add it to the staging area
  3. Write a brief description of what you did (“added linear model”; “created table 1”) and commit
  4. Push to GitHub
  5. Repeat!

As long as you are working on your own, all on the same computer, you don’t need to worry about pulling

What is a commit?

What should you commit? Whatever you don’t want to lose!

If you know that your code worked at 10am on October 21, 2015, and now it doesn’t, you can return!

Exercises

We’re going to fork the class repo, clone it to your computer, make a change, commit it, and push it back up to GitHub.

Every step is on the website with a screenshot so you can have it all in one place.

1. Fork the repo

Go to https://github.com/louisahsmith/epi590r-in-class and click Fork (upper right), then Create fork. You only need the main branch, so leave that box checked.

Forking makes a copy of someone else’s repo that belongs to you.

Forking

  • Purpose: Used to create a personal copy of another user’s repository on your GitHub account.
  • Ownership: The forked repository is still on the original owner’s account, and you get your own copy to work with.
  • Collaboration: Allows you to make changes without affecting the original repository. You can make changes, commit them to your fork, and then propose these changes to the original repository through pull requests.
  • Relationship: The forked repository remains connected to the original, but changes aren’t automatically synced.
  • Use Case: Commonly used when you want to contribute to a project that you don’t have direct write access to.

Cloning

  • Purpose: Used to make a local copy of a GitHub repository on your computer.
  • Ownership: You have a read-write copy on your local machine, but it’s not automatically linked to your GitHub account (you can do so through RStudio).
  • Collaboration: Allows you to work on the project locally and make changes, but these changes aren’t automatically visible to others.
  • Relationship: The cloned repository is a standalone copy, and changes won’t automatically affect the original or other clones.
  • Use Case: Useful when you want to work on a project locally and have full control over commits and pushes.

2. Copy the URL to clone

On your fork, click the green Code button. Make sure HTTPS is selected, and copy the URL.

3. New Project from version control

In RStudio: File > New Project, then choose Version Control, then Git.

4. Paste the URL and pick a location

  • Easiest if the directory name matches the repo name
  • Write down/remember where you put it!

5. Edit the README

From the file pane, open README.md. Write your name in it and save.

6. Stage the change

The file appears in the Git pane marked M (modified). Click the checkbox to stage it, then click Commit.

Interpreting the Git pane

  • D: deleted file
  • M modified file
  • ?: new file, not sure what to do yet. Once you stage it, it becomes;
  • A: added file

7. Write a message, commit, push

You’ll see a diff of what changed. Write a message, Commit, close the window, then Push.

8. Check GitHub

Go back to your browser and reload your repo page.

What do you notice?

Exercise: edit, commit, push readme file