Lecture 02: Exercises

On this page

Lab: build your portfolio repository

Check your work with python check_repo.py ~/ds1-portfolio from lectures/02-dev-environment-git/code.

AI rule: no AI-generated commands for the core steps; typing each command yourself is the point. AI may explain an error message. For stretch and challenge you may use AI, but explain every command you run.

Core (everyone, all 9 checks must pass)

  1. Create ~/ds1-portfolio, run git init -b main, set user.name and user.email.
  2. Create .venv with python -m venv .venv and activate it.
  3. Write .gitignore with .venv/ and __pycache__/, and requirements.txt with numpy, pandas, matplotlib, scikit-learn, pytest. Commit both.
  4. Add README.md with a title. Commit.
  5. Create branch feature/readme, add a description line to the README, commit.
  6. Switch to main, change the same README line differently, commit.
  7. Merge feature/readme into main. Resolve the conflict, commit the merge.
  8. Create an empty GitHub repo ds1-portfolio, add it as origin, push main.

Stretch

  1. On a new branch lab/01, copy your Lecture 01 lab.py into labs/01/, commit, push the branch and open a pull request on GitHub. Merge it after reading your own diff.
  2. Rewrite one of your commit messages into the imperative form before pushing (git commit --amend), and explain why amending is safe only before a push.

Challenge

  1. Add .github/workflows/tests.yml so GitHub Actions runs pytest labs/01 on every push. Make it fail once on purpose, then fix it.
  2. Delete README.md, commit, then bring it back from history with git log -- README.md and git restore --source <commit> README.md.

Exit ticket (last 3 minutes)

E.1 Which command shows staged but uncommitted changes? E.2 Why is .venv/ in .gitignore?

In-class practice

P.1 Parsons problem

Order these commands to finish a merge that stopped with a conflict in README.md. One line is a distractor.

Text
git commit
git push --force
(edit README.md: keep the intended text, delete the markers)
git status
git add README.md

P.2 Half-complete

Fill the blanks so the repo ignores the virtual environment and Python caches.

Text
# .gitignore
______/
______/

P.3 Blank problem

You edited notes.md, staged it, then edited it again. Write the one command that shows only the second edit, and the one that shows only the staged edit.

Questions

Recall

R.1 to R.3: the recall questions at the start of notes.md.

1. Environments

1.1 pip install pandas succeeds, then python script.py says No module named pandas. Give two likely causes. 1.2 Why should .venv/ never be committed? Give two reasons. 1.3 Difference between numpy>=1.26 and numpy==1.26.4; when to use each.

2. Data model

2.1 Two learners create a file with identical content on different laptops. Same blob id? 2.2 Why can a commit never point to a later commit? 2.3 Predict git status after: edit a file, git add it, edit it again.

3. Branches and merges

3.1 In the commit-graph figure, what is the merge base of D and C? 3.2 Without commit D, what does git merge feature/readme do? Any conflict? 3.3 Side A changes line 2, side B changes line 10 of one file. Conflict?

4. GitHub

4.1 An API key is committed, then deleted in the next commit. Is it safe? 4.2 git push is rejected with "fetch first". What happened, what next? 4.3 Why open a pull request instead of pushing straight to main? Two reasons.

Solutions

Solutions are released after the session.