Getting Started with Git
Today you'll set up the folder where all your CS 149 work will live, and you'll start using git, a tool that saves the history of your files and lets you move your work between the computer lab and your own laptop.
For this lab, you'll be using a lab computer, not your laptop. In the next lab, you'll set up your laptop.
Why git?
After all, there are lots of ways to save your work: you could email it to yourself, copy it to a USB drive, or upload it to Google Drive. But those methods are slow, error-prone, and don't keep a history of your work. Git is a professional tool designed to solve all those problems.
Two folders, two jobs
You're about to create two folders:
.CS149.git(the shared repository): a hidden folder that stores the saved history of your work. You'll never open or edit files here directly. It's what lets the computer lab and your laptop share the same work.CS149(your working copy): a folder where you actually write and run your programs. It's connected to the shared repository.
If that distinction isn't clear yet, that's fine; it will make sense as you go.
Step 0: Open a Terminal
"Terminal" (a.k.a. the "command line") is an app that lets you type commands for the operating system to run. Click the black terminal icon ( ) near the top-left of the screen. Then try typing each of these commands, one at a time, pressing Enter after each one:
| Command | Meaning | Description |
|---|---|---|
whoami |
"Who am I?" | displays your username |
pwd |
"Print working directory" | displays your location in the file system |
ls |
Short for "list files" | displays the files in the current location |
The rest of this document contains longer commands for you to run. Copy each command into the terminal, press Enter, and read the short explanation before moving on.
About the terminal
It is possible that up until now, you've always used a mouse to do everything on your computer. But when you start doing more sophisticated work, you'll find that the terminal is faster and more powerful than a mouse. Don't be afraid of the terminal; it's not as hard as it looks, and you'll get used to it quickly. Knowing your way around the terminal is also a necessary skill for any professional programmer, so it's worth learning now.
Step 1: Tell git who you are
Git associates every snapshot with your name and email so there's a record of who made each change.
In the commands below, replace "Your Name" with your actual first and last name (in double quotes), and "username@dukes.jmu.edu" with your actual JMU email address.
user.name and user.email listed.
Important
You only ever do this step once per machine.
Step 2: Create the shared repository
Run this command to create the shared repository, which is the hidden folder that will store your saved history.
--bare means "no working files here, just the history."
The symbols ~/ mean "in my home directory".
The folder name starts with a dot (.) so it stays hidden and you don't accidentally open it from the desktop file manager.
Step 3: Make your working copy
Run this command to create your working copy and connect it to the shared repository.
CS149 is the folder you'll open and work in for the rest of the semester.
Git will warn you that you cloned an empty repository; that's expected, because there's nothing in the shared repository yet.
Run the cd (change directory) command to move into your working copy:
Everything from here on happens inside your CS149 folder.
Run pwd and confirm the output ends with CS149.
Then run ls with the -a option to list everything in the folder, including hidden files (-a means "all").
Right now you should see just . , .., and a .git folder.
The .git folder is how your working copy keeps track of its connection to the shared repository; leave it alone.
Step 4: Add the starter files
We've provided some starter files for you on the course website.
Download each one into your CS149 folder using curl ("Client URL"):
curl downloads a file from a web address.
The -o option (for "output") saves it under the name you give.
Notice that the first two files save under a different name than the original web address: README becomes README.md, and gitignore becomes .gitignore (the leading dot makes it a hidden file in your folder).
Check what's here now:
You should see the three files you just downloaded, alongside the .git folder.
Check what changed:
Git lists the new files under "Untracked files" in red. Untracked means git can see the files but isn't saving their history yet; you have to tell git to do that in the next step.
Step 5: Save your first snapshot
Saving in git is two steps: first you stage (add) the files you want to save, then you commit them (take the snapshot).
This command stages all your changes, marking the files to be included in the next snapshot.
-A means "all changes."
The same files now appear under "Changes to be committed" in green. They've moved from untracked to staged: git knows you want to save them, but the snapshot hasn't been taken yet.
Commit roughly means "take a snapshot" and save it to your working copy's history. The text in quotes is the commit message, a short note describing what you saved. Git prints a summary of what was committed.
Notice it now says "nothing to commit, working tree clean." That means everything in your folder has been saved; there are no unsaved changes. A clean working tree is what you want to see at the end of a work session.
Step 6: Send your work to the shared repository
Your snapshot is saved in your working copy, but not yet in the shared repository. Sending it there is what will let your laptop pick it up later.
The push command copies your commits to the shared repository. Git prints a few lines showing the transfer.
Still "working tree clean," and now git may also tell you your branch is up to date with the shared repository. That means your working copy and the shared repository match: everything is saved and synced.
Step 7: Write a program and save it with git
Now let's put a real program under git.
Thonny
Thonny is the code editor and IDE (Integrated Development Environment) we'll be using for the first half of the semester. It is installed on your lab computer.
Open Thonny on the desktop and write a tiny program:
Save it into your ~/CS149 folder as hello.py, then run it in Thonny to make sure it works.
Back in the terminal, from inside ~/CS149, commit it with the same loop you just learned:
hello.py is saved in your history and waiting in the shared repository for the next lab.
Why'd we do all this?
We've put your shared repository in a location accessible from other computers, including your laptop. After we set up your laptop in the next lab, you'll be able to pull your work from the shared repository to your laptop, and push your work back to the shared repository when you're done. This is how you'll move your work between the computer lab and your own computer for the rest of the semester.
Summary
At the end of this lab, you now have:
- a shared repository (
~/.CS149.git) storing your saved history, and - a working copy (
~/CS149) with the starter files and yourhello.py.
For the rest of the course, your everyday rhythm inside ~/CS149 will be:
git status(see what's changed)git add -A(stage your changes)git commit -m "..."(save a snapshot)git push(send it to the shared repository)
You'll practice this loop so many times until it becomes second nature.
In the next lab, you'll set up your laptop to connect to this same shared repository, so your work follows you between the computer lab and your own computer.