Introduction to Git and GitHub

Jacob Gladfelter

What is Git/GitHub?

Git

  • A distributed version control system.
  • Tracks changes in source code.
  • Enables collaboration among developers.

GitHub

  • A web-based hosting service for Git repositories.
  • Facilitates collaboration and code sharing.
  • Git is the tool, GitHub is the platform.

Why should I use it?

Key Benefits

  • Track changes: See who changed what, when, and why.
  • Revert to previous versions: Easily undo mistakes.
  • No more “final_final_v2.R”: Keep a clean project history.
  • Collaborate: Work on the same project with others seamlessly.*
  • Reproducibility & Transparency: Core principles in data science.

* Using git collaboratively still requires communication!

Getting Started

1. Create a GitHub Account

  • Go to github.com and sign up.
  • Use your home institution’s .edu email
    • This should help with getting free stuff, like Copilot!

2. Install Git

  • Download from git-scm.com.
  • Accept default options during installation.
  • Verify in terminal:
git --version

3. Configure Git

  • Tell Git who you are. This is important for tracking changes.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Why SSH?

  • Securely connect to GitHub without entering your username/password each time.
  • GitHub no longer supports password auth over HTTPS.

Generating and Adding Your Key

  1. Generate key. Run in terminal and accept the defaults:
ssh-keygen
  1. Copy your public key:
# On macOS
pbcopy < ~/.ssh/id_ed25519.pub
# On Linux/Windows git bash
cat ~/.ssh/id_ed25519.pub
  1. Go to GitHub settings > SSH and GPG keys > New SSH key.
  2. Paste your key and save.

The Basic Workflow

1. Clone a Repository

  • “Cloning” means making a local copy of a remote repository.
  • Use the SSH URL from the GitHub repository page.
git clone git@github.com:user/repo.git

2. Make Changes

  • Create, edit, or delete files in your project folder.

3. Save Changes (Commit)

  • A “commit” is a snapshot of your changes.
  1. Stage your changes. Tell Git which files to include.
git add <file1> <file2>
# or stage all changes
git add .
  • This is useful if you made many changes, but only want to commit a select few.
  1. Commit the staged changes.
git commit -m "A short, descriptive message about your changes"

4. Push Changes to GitHub

  • “Pushing” sends your committed changes to the remote repository on GitHub.
git push

Branches

What is a Branch?

  • A separate line of development.
  • Allows you to work on new features or fixes without affecting the main codebase (main branch).

Branching Workflow

  1. Create and switch to a new branch for your task:
git checkout -b new-feature-branch
  1. Make your changes, add, and commit on this branch.
  2. Push your new branch to GitHub:
git push --set-upstream origin new-feature-branch

Pull Request (PR)

What is a Pull Request?

  • A request to merge your changes from your branch into another branch (usually main).
  • It’s the primary way to collaborate and ask for a code review on GitHub.

PR Workflow

  1. Push your branch to GitHub.
  2. Go to the repository on GitHub.
  3. Click “Compare & pull request”.
  4. Write a clear title and description, and request reviewers.
  5. Once approved, it can be merged.

Q&A