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

Windows using WSL

If you are following the recommended WSL workflow, install Git inside Ubuntu:

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 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 compact commit history
git branch List branches
git switch BRANCH Switch branches
git switch -c NAME Create and switch to a branch

6. Branches

A branch provides an independent line of development.

Most individual course assignments will not require you to create branches, but branches are useful for experiments and collaborative projects.

Create and switch to a new branch:

git switch -c experiment

List branches:

git branch

Return to main:

git switch main

Push a new branch the first time:

git push -u origin experiment

Merge a branch into the branch you currently have checked out:

git merge experiment

Older documentation often uses:

git checkout -b experiment

That command still works, but git switch -c is clearer for creating and switching branches.

7. GitHub authentication

For public repositories, cloning normally requires no authentication. Pushing to GitHub or accessing private assignment repositories requires you to sign in.

GitHub documentation:

https://docs.github.com/en/authentication

If Git or VS Code opens a browser window asking you to authenticate with GitHub, follow the browser sign-in flow.

Warning

Never commit passwords, access tokens, API keys, or other secrets to a Git repository.

8. GitHub Classroom

When an assignment uses GitHub Classroom, the workflow is generally:

  1. Open the assignment link.
  2. Accept the assignment.
  3. GitHub creates a repository for you or your team.
  4. Clone that repository to your computer.
  5. Work locally.
  6. Commit and push your work.

Always check that your latest commit appears on GitHub after pushing.

9. GitHub Desktop

If you prefer a graphical interface, GitHub Desktop is available:

https://desktop.github.com/

It can visualize changed files, commits, branches, pulls, and pushes.

The command-line Git workflow is still worth learning because it works consistently on local computers, WSL, servers, and remote machines.

10. Common problems

“Nothing to commit”

Run:

git status

You may not have changed a tracked file, or you may already have committed the changes.

Push rejected because the remote has newer commits

Start with:

git pull

Resolve any conflicts if Git reports them, then commit if necessary and push again.

You are not sure what Git is about to do

Use:

git status

This is usually the best first command when troubleshooting Git.

Good habits

  • Run git status often.
  • Pull before starting work on a shared repository.
  • Write short, meaningful commit messages.
  • Commit logical pieces of work rather than one enormous end-of-project commit.
  • Push regularly.
  • Do not commit large datasets, generated files, model weights, or secrets unless explicitly required.