Version Control with Git & GitHub

Git is a tool that records the history of your project so you can undo mistakes, collaborate without emailing files back and forth, and back up your work. GitHub is the most popular place to host Git projects online, and a public GitHub profile doubles as a portfolio that employers and collaborators can browse.

Left: commits are labeled snapshots on a line of history, and a feature branch is an independent line that diverges from main to let you experiment safely before merging back. Right: the everyday loop moves changes across four places — the working directory, the staging area, the local repo, and the remote (origin) on GitHub — via git add, git commit, git push, and git pull.
Figure 1. Left: commits are labeled snapshots on a line of history, and a feature branch is an independent line that diverges from main to let you experiment safely before merging back. Right: the everyday loop moves changes across four places — the working directory, the staging area, the local repo, and the remote (origin) on GitHub — via git add, git commit, git push, and git pull.

Why Version Control?#

Core Mental Model#

The two diagrams in Figure 1 capture the whole model: a history of commits with branches on the left, and the everyday add–commit–push–pull loop on the right.

One-Time Setup#

Shell
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Start a repo in a new project, or grab an existing one:

Shell
git init                 # turn the current folder into a Git repo
# ...or clone an existing repo from GitHub:
git clone https://github.com/username/project.git

The Everyday Loop#

This four-step cycle is the core of daily Git use:

Shell
git status                 # 1. see what has changed
git add analysis.R data/   # 2. stage the changes you want to record
git commit -m "Add case-count cleaning step"   # 3. record a snapshot
git push                   # 4. upload commits to GitHub (the remote)

Before starting work (especially when collaborating), pull the latest changes from the remote:

Shell
git pull                   # fetch and merge others' changes into your copy

.gitignore#

A .gitignore file lists paths Git should never track, secrets, large data, and generated output.

Shell
# .gitignore
.env
.Renviron
*.csv
output/
.DS_Store

Do commit your code and small text files. Don’t commit secrets, huge datasets, or generated artifacts, they bloat the repo and can leak sensitive information.

Commit Early, Commit Often, With Good Messages#

Small, frequent commits make history easy to read and mistakes easy to isolate.

Shell
# Good: specific, present-tense, explains the change
git commit -m "Fix off-by-one in age-group binning"

# Bad: vague and unhelpful
git commit -m "stuff"

Do write a message that finishes the sentence “This commit will…”. Don’t lump a week of unrelated changes into one giant commit.

GitHub as a Professional Showcase#

Learn More#

For a gentle, R-focused introduction that walks through installation and RStudio integration, see Happy Git with R.