02 — Git and GitHub

Setup
Git
GitHub
Learn the Git workflow used to download, track, save, and share course code.

Git vs. GitHub

Git is a version-control system that tracks changes to files on your computer.

GitHub is an online service that hosts Git repositories and makes it easier to share code, collaborate, and distribute projects.

In this course, GitHub may also be used through GitHub Classroom to distribute starter code for assignments.

Why use Git?

Git helps you:

  • keep a history of your code;
  • recover earlier versions after a mistake;
  • work on the same project from multiple computers;
  • collaborate without constantly emailing files;
  • back up committed work by pushing it to GitHub.
Tip

Commit regularly and push important work to GitHub. A file that only exists on your laptop is not a backup.

1. Install Git

Raspberry Pi OS

Git is usually preinstalled on Raspberry Pi OS. Check first:

git --version

If it is not installed:

sudo apt update
sudo apt install git -y

Ubuntu/Linux

sudo apt update
sudo apt install git -y

macOS

Try:

git --version

If Git is not installed, macOS may offer to install the Xcode Command Line Tools.

Git downloads and documentation:

https://git-scm.com/downloads

Verify the installation:

git --version

2. Configure your identity

Git records an author name and email with each commit.

Set them once:

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

Check the configuration:

git config --global --list

Using the email associated with your GitHub account makes it easier for GitHub to associate commits with your profile.

3. Clone a repository

git clone downloads an entire repository and its version history.

For example:

git clone https://github.com/ariarobotics/cv.git

Then enter the repository:

cd cv

The course materials are maintained on the repository’s main branch, which is the default branch. A normal clone therefore downloads the correct version:

git clone https://github.com/ariarobotics/cv.git

For assignments distributed through GitHub Classroom, use the repository link provided for that assignment.

4. The basic Git workflow

The commands you will use most often are:

git status
git pull
git add -A
git commit -m "Describe what changed"
git push

A typical workflow is:

Step 1 — Check the repository

git status

This shows:

  • modified files;
  • new/untracked files;
  • staged files;
  • your current branch.

Step 2 — Pull recent changes

Before beginning work on a shared repository:

git pull

Step 3 — Edit your files

Use VS Code, Thonny, or another appropriate editor.

Step 4 — Review what changed

git status
git diff

git diff is especially useful before committing because it shows the exact line-by-line changes.

Step 5 — Stage changes

Stage everything:

git add -A

Or stage a specific file:

git add path/to/file.py

Step 6 — Commit

git commit -m "Implement image filtering"

A commit saves a snapshot in the local Git repository.

Step 7 — Push

git push

This sends your local commits to GitHub.

Important

git commit and git push are different operations. A commit saves the change locally; a push sends committed changes to GitHub.

5. Essential commands

Command Purpose
git clone URL Download a repository
git status Show repository status
git pull Download and integrate remote changes
git add FILE Stage a file
git add -A Stage all changes
git diff Inspect unstaged changes
git diff --staged Inspect staged changes
git commit -m "message" Create a commit
git push Upload commits
git log --oneline Show commit history

6. Branches

A branch is an independent line of development. Working on a branch lets you make changes without affecting the main branch until you are ready to merge them.

Create and switch to a new branch:

git checkout -b feature-name

List branches:

git branch

Switch back to main:

git checkout main

Merge a branch into main:

git checkout main
git merge feature-name

Push a new branch to GitHub:

git push -u origin feature-name
Note

Individual assignments can usually be done directly on main. For team projects such as the capstone, use a separate branch per feature and open a pull request on GitHub before merging into main.

7. GitHub authentication

GitHub no longer accepts your account password for Git operations over HTTPS. Use one of the following instead.

Personal access token (HTTPS):

  1. On GitHub: Settings → Developer settings → Personal access tokens → Generate new token
  2. When Git prompts for a password while pushing or pulling, paste the token instead

SSH key (recommended if you push often):

ssh-keygen -t ed25519 -C "your_email@example.com"

Add the contents of ~/.ssh/id_ed25519.pub to GitHub under Settings → SSH and GPG keys, then test the connection:

ssh -T git@github.com

Clone using the SSH URL (git@github.com:owner/repo.git) instead of the HTTPS URL to use the key automatically.

8. GitHub Classroom

Assignments distributed through GitHub Classroom come with an invitation link posted on Ed Discussion or the Schedule page.

  1. Open the invitation link and accept it — this creates your own copy of the assignment repository
  2. Clone your repository the same way as any other repository (Step 3 above)
  3. Commit and push your work before the deadline
Important

Grading is based on the state of the main branch at the deadline. Push early and often rather than only once at the end.

9. GitHub Desktop

If you prefer a graphical interface over the command line, GitHub Desktop provides the same clone/commit/push/pull workflow through buttons:

https://desktop.github.com/

It is especially useful for visually reviewing a diff before committing. The terminal commands in this tutorial still work identically and are what TAs will assume during office hours.

10. Common problems

“Nothing to commit”

This means Git sees no staged changes. Confirm the file was saved, then check status and stage it:

git status
git add -A

Push rejected because the remote has newer commits

Pull first to integrate the remote changes, resolve any conflicts, then push again:

git pull
git push

You are not sure what Git is about to do

Run git status and git diff before git add or git commit. When in doubt, ask on Ed Discussion or in office hours rather than running commands you do not understand.

Good habits

  • Commit small, logical changes with clear messages
  • Pull before you start working, push when you finish a session
  • Never commit large datasets or trained model weights — add them to .gitignore instead
  • Never commit passwords, tokens, or API keys