Lecture 02: Exercises
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)
- Create
~/ds1-portfolio, rungit init -b main, setuser.nameanduser.email. - Create
.venvwithpython -m venv .venvand activate it. - Write
.gitignorewith.venv/and__pycache__/, andrequirements.txtwithnumpy,pandas,matplotlib,scikit-learn,pytest. Commit both. - Add
README.mdwith a title. Commit. - Create branch
feature/readme, add a description line to the README, commit. - Switch to
main, change the same README line differently, commit. - Merge
feature/readmeintomain. Resolve the conflict, commit the merge. - Create an empty GitHub repo
ds1-portfolio, add it asorigin, pushmain.
Stretch
- On a new branch
lab/01, copy your Lecture 01lab.pyintolabs/01/, commit, push the branch and open a pull request on GitHub. Merge it after reading your own diff. - 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
- Add
.github/workflows/tests.ymlso GitHub Actions runspytest labs/01on every push. Make it fail once on purpose, then fix it. - Delete
README.md, commit, then bring it back from history withgit log -- README.mdandgit 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.
git commit
git push --force
(edit README.md: keep the intended text, delete the markers)
git status
git add README.mdP.2 Half-complete
Fill the blanks so the repo ignores the virtual environment and Python caches.
# .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.