Git Course 0%

Set up a safe playground

Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI ≈ 12 min

What you will learn

  • What the playground project contains and why it is safe
  • How to turn a folder into a repository (the same three commands every project starts with)
  • How to run the program and its tests
  • How to reset the playground when you break it

After this lesson you can

  • I have a trailguide repository on my computer with one commit
  • I can run the program and the tests
  • I know how to start over in one minute

Why this matters

You cannot learn Git by reading, and you must not learn it on your team's real project. The playground is a small, realistic project made for breaking: documentation to edit, data to change, a program to run, tests that can fail. Every "Try it yourself" and every lab uses it. When something goes wrong, you delete the folder and start again in one minute.

What the playground is

Trailguide is a tiny command-line program that prints hiking trails for the fictional Northwind Trails company. It has everything a real project has, in miniature:

Path What it is You will
README.md What the project is and how to run it edit it in Lab 2
CONTRIBUTING.md The team's rules for branches, commits and merge requests follow it from Section 6 on
docs/getting-started.md, docs/faq.md Documentation in Markdown edit, review and resolve conflicts in
data/trails.csv The trails: name, region, distance, difficulty add rows to
src/trailguide.py The program (Python, no extra packages) run, and make one small fix in Lab 6
tests/test_trailguide.py Three automated tests run, and watch fail when something breaks
.gitlab-ci.yml, .github/workflows/ci.yml The automated checks that run on every push read in Section 14
.gitignore Files Git must never track read in Section 4

Get the files

  1. Download trailguide.zip (about 10 KB).
  2. Create a practice folder in your home folder, called git-practice, if you did not create it in Lab 1.
  3. Unzip the archive into it: on macOS double-click the file in Finder and move the trailguide folder into git-practice; on Windows right-click → Extract All… and choose git-practice; on Linux unzip trailguide.zip -d ~/git-practice.
  4. Check that you now have ~/git-practice/trailguide/README.md (on Windows: C:\Users\you\git-practice\trailguide\README.md).

Turn it into a repository

The folder is just files. Three commands make it a repository with one commit; you will run these same three commands at the start of every new project of your own.

Terminal
$ cd ~/git-practice/trailguide
$ git init
Initialized empty Git repository in /Users/you/git-practice/trailguide/.git/
Terminal
$ git add .
$ git commit -m "Initial import of trailguide"
[main (root-commit) 216f956] Initial import of trailguide
 12 files changed, 324 insertions(+)
 create mode 100644 .github/workflows/ci.yml
 create mode 100644 .gitignore
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .markdownlint.json
 create mode 100644 CONTRIBUTING.md
 create mode 100644 README.md
 create mode 100644 data/trails.csv
 create mode 100644 docs/faq.md
 create mode 100644 docs/getting-started.md
 create mode 100644 docs/images/logo.svg
 create mode 100644 src/trailguide.py
 create mode 100644 tests/test_trailguide.py

Your hash (216f956 here) will differ. If git init mentions a branch called master, run git branch -m main before committing, and set the default for the future with git config --global init.defaultBranch main.

Run the program and the tests

Make sure the playground works before you start changing it. From a terminal inside the trailguide folder:

Terminal
$ python3 src/trailguide.py --list
Ridge Loop             North      7.5 km  moderate
Silver Lake Walk       North      4.2 km  easy
Eagle Summit           North     14.8 km  hard
Harbour Path           Coast      6.1 km  easy
Lighthouse Trail       Coast      9.3 km  moderate
Old Mill Way           East       5.4 km  easy
Pine Valley Circuit    East      11.7 km  moderate
Sunset Crag            West      12.9 km  hard
Meadow Stroll          West       3.6 km  easy
Twin Falls Track       South      8.8 km  moderate
10 trail(s)
Terminal
$ python3 -m unittest discover -s tests -v
test_format_trail_shows_distance_with_one_decimal (test_trailguide.TrailguideTests.test_format_trail_shows_distance_with_one_decimal) ... ok
test_loads_ten_trails (test_trailguide.TrailguideTests.test_loads_ten_trails) ... ok
test_longest_is_eagle_summit (test_trailguide.TrailguideTests.test_longest_is_eagle_summit) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

If python3 is not found on Windows, try python instead; Git Bash and PowerShell both work. If Python is missing entirely, install it from python.org (tick "Add to PATH" on Windows).

When you break it

You will, on purpose and by accident. Two levels of reset:

  • Undo uncommitted edits (keeps your commits): git restore . throws away every change since the last commit, and git clean -fd deletes new files and folders you created. Both are irreversible for uncommitted work — which is the point in a playground.
  • Start completely over: delete the whole trailguide folder and unzip the archive again, then repeat "Turn it into a repository".

Try it yourself

Goal: make one change to the playground, see Git notice it, and put it back.

  1. Open docs/faq.md in your editor and add a line at the end, for example Last updated by me.
  2. In the terminal, run git status -s.
  3. Put the file back exactly as it was: git restore docs/faq.md, then run git status -s again.

Expected result: after step 2 the output is M docs/faq.md (modified, not staged). After step 3 the output is empty: the working tree is clean again.

Show solution

Terminal
$ printf '\nLast updated by me.\n' >> docs/faq.md
$ git status -s
 M docs/faq.md
Terminal
$ git restore docs/faq.md
$ git status -s
The second git status -s prints nothing: no differences, nothing to report.

Check yourself

1. Why does the GitLab (or GitHub) project have to be created without a README?
2. Which three commands turn an ordinary folder into a repository with its first commit?
3. You ran a lesson's exercise, made a mess, and want the playground back to its last commit. What is the fastest safe move?

Key terms

Repository (repo) Working tree Commit Remote