Git and GitHub for your projects
Git saves snapshots of your project, called commits, so you can go back, branch off and merge; GitHub stores those repositories online.
Why it matters
Without version control you end up with files like model_final_v3_REALLY_final.py, and no idea which version gave your best result. One bad edit can break working code with no way back. Git records every change with a message, lets you try ideas safely on a branch, and GitHub backs it all up and shows employers your work.
The idea
Git saves snapshots of your project, called commits, so you can go back in time. GitHub stores them online and shows your work to employers.
Save points in a video game. You can try something risky, and reload if it goes wrong.
Game saves are for one player. Branches let you, or a team, try several ideas side by side and then merge the good ones.
How it works
- git init turns a folder into a repository (a repo). From now on, Git tracks changes in it.
- You edit model.py. git status shows it as modified: the change exists only in your working folder.
- git add model.py puts the change in the staging area: the list of changes for your next snapshot.
- git commit -m "Add training loop" saves the snapshot, with its message, in your local repository. Each commit gets a unique id (a hash) and points back to the one before.
- git switch -c experiment creates a branch: a separate line of commits for a risky idea. main stays untouched.
- git switch main, then git merge experiment, brings the idea back. If both lines changed the same lines, Git stops and asks you to fix a merge conflict by hand.
- git push sends your commits to GitHub; git pull brings down commits made somewhere else.
Key points
- Four places: working folder → (git add) → staging area → (git commit) → local repo → (git push) → GitHub.
- A commit is a snapshot of the whole project plus a message saying why. Small, focused commits are the easiest to understand and undo.
- A branch is only a movable label on a commit, so making one is instant.
- git log --oneline shows the history; git diff shows changes you have not staged yet.
- A .gitignore file lists what Git must never track: datasets, model checkpoints, .venv, __pycache__ and secrets such as API keys.
- GitHub is not Git. Git runs on your computer; GitHub is a website that keeps copies of repos and adds pull requests and issues.
Common mistakes
- Committing large data files or model weights. They stay in the history forever and make the repo huge. Ignore them with .gitignore.
- Committing secrets such as API keys or .env files. Deleting them later does not remove them from the history, so change the key.
- Messages like "update" or "fix". Say what changed and why.
- Doing every experiment directly on main, so one broken idea breaks everything.
Not true: A leaked secret key: deleting it in a later commit removes it from the history.
In fact: The earlier commit still contains it, and anyone with the repository can read it. A leaked key must be changed, not just deleted.
Not true: Experiments: do each one directly on main; branches are for finished work.
In fact: One broken idea then breaks everything. Try each idea on its own branch, and merge it into main only when it works.
Part of Unit 0: Your toolkit in the free ML Foundations course.