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

  1. git init turns a folder into a repository (a repo). From now on, Git tracks changes in it.
  2. You edit model.py. git status shows it as modified: the change exists only in your working folder.
  3. git add model.py puts the change in the staging area: the list of changes for your next snapshot.
  4. 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.
  5. git switch -c experiment creates a branch: a separate line of commits for a risky idea. main stays untouched.
  6. 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.
  7. git push sends your commits to GitHub; git pull brings down commits made somewhere else.

Key points

Common mistakes

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.