02 — Git and GitHub
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.
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 -yUbuntu/Linux
sudo apt update
sudo apt install git -ymacOS
Try:
git --versionIf Git is not installed, macOS may offer to install the Xcode Command Line Tools.
Git downloads and documentation:
Verify the installation:
git --version2. 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 --listUsing 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.gitThen enter the repository:
cd cvThe 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.gitFor 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 pushA typical workflow is:
Step 1 — Check the repository
git statusThis shows:
- modified files;
- new/untracked files;
- staged files;
- your current branch.
Step 2 — Pull recent changes
Before beginning work on a shared repository:
git pullStep 3 — Edit your files
Use VS Code or another appropriate editor.
Step 4 — Review what changed
git status
git diffgit diff is especially useful before committing because it shows the exact line-by-line changes.
Step 5 — Stage changes
Stage everything:
git add -AOr stage a specific file:
git add path/to/file.pyStep 6 — Commit
git commit -m "Implement image filtering"A commit saves a snapshot in the local Git repository.
Step 7 — Push
git pushThis sends your local commits to GitHub.
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 experimentList branches:
git branchReturn to main:
git switch mainPush a new branch the first time:
git push -u origin experimentMerge a branch into the branch you currently have checked out:
git merge experimentOlder documentation often uses:
git checkout -b experimentThat 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.
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:
- Open the assignment link.
- Accept the assignment.
- GitHub creates a repository for you or your team.
- Clone that repository to your computer.
- Work locally.
- 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:
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 statusYou 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 pullResolve 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 statusThis is usually the best first command when troubleshooting Git.
Good habits
- Run
git statusoften. - 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.