Session 2 Terminal, File/folder Structure, Git Basics

Goals

By the end of this session, you will be able to:
  • notate file paths from diagrams
  • navigate through directories from the command line
  • make and remove files and directories from the command line
  • define and identify methods for a class
  • explain the purpose of git
  • initialize, add, and commit changes in a local repository

Agenda

Materials

  • Notebook
  • Writing instrument
  • Laptop
  • Headphones & mic

Housekeeping, Feedback, and Goals

  • In the chat, post a keyboard shortcut that you practiced and now feel comfortable using.
  • You will be screensharing your terminal today. If you have programs open that might be distracting (or you have confidential work open), close or minimize those programs now.
  • Questions during the session? Click “raise hand” for whole-group question, or message the non-presenting host for an individual question.
  • We’ll stay on the call at the end of today’s session for any Qs that are not addressed during the lesson.

Wins

  • Solid understanding of data types
  • Solid understanding of Googling + filtering tips specific to programming
  • Study habits and shortcuts

Follow-Ups

  • Hands on examples
  • Sessions are designed to move fast
  • More breakout room time

Today’s Goals

(See above)

Intros, Review, and Icebreaker (5 minutes)

1. Introduce yourself: name, pronouns

2. What's the most boring fact about yourself?

3. Accountability review: what were the skills/processes that you decided to focus on last week, and how are you progressing toward mastery of those skills?

Classes (Intro: Part II)

Note: This is a topic that we will revisit in bite-size chunks leading up to Session 4. We will not talk about the technical aspects of how classes play a part in programming today; instead, we will apply the terminology and concepts to real life.

Class and Attribute Review

Try It: Classes

Post one sentence in the chat using this structure:

______, ________, and _______ (instances) are different versions of a ________ (class).

For example: My Old Navy sweatshirt and my American Giant sweatshirt (instances) are different versions of a sweatshirt (class).

Try It: Attributes

Post one sentence in the chat about your class using this structure:

Each _______ (class) has a _______, ________, and ________ (attributes).

For example: Each Sweatshirt (class) has a color, brand, and size (attributes).

Methods of Classes

A method of a class defines behavior/actions. Methods are generally verbs (action words or very short action phrases). Methods generally answer the questions “What things can instances of this specific class do?” or “What can be done to instances of this specific class?”

For example:

  • Each child (class) can greet, run, and learn (methods).
  • Each sweatshirt (class) can be purchased, be returned, be washed, and be repaired (methods).
  • Each book (class) can be read, have the price updated, and be highlighted (methods).
  • Each bank account (class) can have money deposited, can have money withdrawn, can be opened, can be closed, and can change ownership (methods).

Try It: Methods

Post one sentence in the chat about your class using this structure:

Each _______ (class) can ___________, __________, and ___________.

Files, Directories, and Paths

On your computer, you probably have many files and directories. Files are things like text documents, images, videos, PDFs, etc. Directories (or folders) are the structures we use to organize these files.

In the diagram below, we would say that there’s a directory called essays that contains three files: life_lessons.docx, book_report.docx, and literary_analysis.docx:


Example #1

files and directories 1
A file path is a way to notate where a file "lives" on your computer. This is the structure:
directory_name/file_name.extension
The file path for the first file in the diagram would be:
essays/life_lessons.docx

What is the path for book_report.docx?

What is the path for literary_analysis.docx?

Things to Note

  1. A file cannot be inside of another file. This means that every part of the path before the actual file is a directory.
  2. For now, we will follow two convention rules:
    • Use lower case letters when naming directories and files
    • Use underscores (_) or hyphens (-) instead of spaces when naming directories and files. However, keep in mind that different languages and frameworks have different conventions. Rather than arguing over which approach is "correct" (you'll find a lot of this on the internet), it is more important to pick an approach and be consistent. For today's lesson, we'll use the underscore (_) approach.
  3. Folders do not have extensions (like .docx or .txt or .csv, etc.) but file names do have extensions.
  4. File extensions matter. A .md file will behave differently than a .docx file, which will also behave differently than a .txt file because the extensions help the operating system figure out which application can open the file.

Example #2

files and directories 2

The path for the grading.txt file is

to_do/work/grading.txt

What is the path for cleaning.txt?

What is the path for random.txt?

What is the path for recurring.txt?

What is the path for bills.txt?

What is the path for data_entry.csv?

Things to Note

  1. We commonly refer to directories with an analogy of parent and child. In the above example to_do is the parent directory of the home and work directories. projects_to_delectate is a child directory of the work directory.

Terminal and Command Line

The terminal is what we call a command line interface. Basically, that’s a fancy way of saying that it’s the program we use to give commands to the computer. The command line is a text-based way to make files, make directories, and run applications, among other things.

Your terminal will look something like this:

The text timo@Tims-MacBook-Pro:~$ is called the command prompt, and the $ indicates that it’s ready to accept input.

Something to note: When copying and pasting commands from the internet, do not include the $. It is meant to symbolize that a command is run from the terminal.

If you want to further customize your command prompt, check out the EZPrompt Generator.

Commands

In this section on command prompts, we’ll learn (or review) the commands below. We’ll look at all 10 together (take notes!), then you’ll go into breakout rooms to practice them.

Action Commands

Safe/Informative Commands

Destructive Commands

1. Where am I? (pwd)

When you open the terminal, you will be in your home directory. Being in various directories will allow you to do different things, just like you can do different things at home vs. on vacation vs. at work.

To figure out where you are in your computer’s directories, type pwd after the command prompt. pwd stands for “print working directory”

timo@Tims-MacBook-Pro:~$ pwd
/Users/timo

You’ll see the path from the root of your computer to your current directory.

2. Make a Directory (mkdir)

To make a folder using the visual interface of Finder, this is what you might do:

making a folder using finder

We can make new directories with the mkdir command. Unlike pwd where we didn’t need to type anything else, we’ll need to add a name for the directory. Keep your directories lowercase with no spaces. If you need to use a space, use the underscore (_).

For example, the following two commands will create two directories called work_spreadsheets and latest_projects:

timo@Tims-MacBook-Pro:~$ mkdir work_spreadsheets
timo@Tims-MacBook-Pro:~$ mkdir latest_projects

You won’t get any confirmation that your directory was created – you’ll just see a new command prompt ready for your next comamnd.

3. Listing Contents of a Directory (ls)

With a visual interface (as shown in the gif above), you can easily see the contents of a directory. On the command line, it’s a little different.

To check what’s inside of a directory, we use the ls command which stands for list (although I like to think to myself: “list stuff”). As an example, let’s assume that I have the directories and files from this diagram on my computer:

files and directories 2
If I was in the essays directory and I typed ls, this is what I'd see:
timo@Tims-MacBook-Pro:~/essays$ ls
    book_projects    life_lessons.docx    notes.docx

You will only see the directories and files that are directly inside of where you are. You will not see any directories or files that are nested down the path. This is why we do not see the contents of book_projects listed.

Now assume we're in the book_projects directory. If I type ls, I'll see this:

timo@Tims-MacBook-Pro:~/essays/book_projects$ ls
    literary_analysis.docx    book_report.docx

4. Go Into a Directory (cd)

You can move into a directory using the cd command, which stands for “change directory”. After cd, type the name of the directory you want to go into.

files and directories 2
For example, if I was in the essays directory and wanted to move into the book_projects directory to see my documents, I would type this:
timo@Tims-MacBook-Pro:~/essays$ cd book_projects
timo@Tims-MacBook-Pro:~/essays/book_projects$ 
We see that the second command prompt now lists the path of new directory that we're in. From there, if I used the ls command, I would be able to see the contents of my folder:
timo@Tims-MacBook-Pro:~/essays/book_projects$ ls
literary_analysis.docx    book_report.docx

Things to Note

  1. You can't pick any random directory from your computer to give to the ls command. It has to be a directory inside wherever you currently are (or you need to use the full path to get to that directory, which we won't talk about today).

5. Get Out of a Directory (cd ..)

To get out of a directory you’re in, we use cd .. (with a space between the d and the first dot). This means “go back up one level.”

If I’m in the book_projects directory and I want to get back to essays, this is what I’d type:

timo@Tims-MacBook-Pro:~/essays/book_projects$ cd ..
timo@Tims-MacBook-Pro:~/essays$

Notice that my path no longer includes book_projects because I’m outside of that folder now.

Try It: cd and cd ..

Let's try to figure out the following scenarios together.

files and directories 2

If I'm in the work directory, what do I need to type to get to to_do?

If I'm in the projects_to_delegate directory, what two commands do I need to type to get to to_do?

(We'll learn how to combine these momentarily)

I'm in the home directory. What three commands do I need to type to get to projects_to_delegate?

(We'll learn how to combine these momentarily)

I'm in the projects_to_delegate directory. What three commands do I need to type to get to home?

(We'll learn how to combine these momentarily)

Things to Note

  • One can combine commands to navigate multiple levels through your directory structure.
  • For the third example in the previous Try It section, one could navigate to the projects_to_delegate directory as long as one knows the path:
  • cd ../work/projects_to_delegate
  • For the fourth and final example above:
  • cd ../../home
  • Each level in the path is spearated by a /

6. Make a File (touch)

We know how to make directories (or folders) using the mkdir command. In order to make files inside of those directories, we use touch. The following two commands show how I would make two new files, chapter_1.txt and chapter_2.txt:

timo@Tims-MacBook-Pro:~/latest_projects$ touch chapter_1.txt
timo@Tims-MacBook-Pro:~/latest_projects$ touch chapter_2.txt

We don’t see any confirmation that the file was created, but we can use ls to see what’s inside the directory:

timo@Tims-MacBook-Pro:~/latest_projects $ ls
chapter_1.txt    chapter_2.txt

7. See the text in a file (cat)

If we want to check what text is inside of a file, we can use cat:

timo@Tims-MacBook-Pro:~/latest_projects $ cat chapter_1.txt
It was the best of times, it was the worst of times

8. Remove a File (rm)

In the past, you’ve probably gotten rid of files by using the Move to trash command or dragging them into the trash, like this:

file to trash

We can remove files from the command line using the rm command, like this:

timo@Tims-MacBook-Pro:~/latest_projects $ rm chapter_1.txt

Again, we don’t get a confirmation, but if I were to ls right now, nothing would appear since the directory is now empty.

timo@Tims-MacBook-Pro:~/latest_projects $ ls
chapter_2.txt

Things to Note

  • A file removed using the rm command does not go into your trash where you could restore it later.
  • Although it may be possible to recover files deleted with rm, it is a difficult process requiring special tools and time. For now, assume that any file you remove using the rm command is gone for good.

9. Remove a Directory and Its Contents (rm -rf)

We can use rm to remove a file, but we use a different command when we’re removing a directory. Since a directory could potentially contain other files and directories inside of it, we use rm -rf which stands for remove recursively, or go inside this directory and remove everything inside of it as well.

In order to remove a directory, you must be OUTSIDE of that directory. For example, if I’m inside a books directory and I want to remove it, I first need to get out of it using cd .., then use the rm -rf books:

timo@Tims-MacBook-Pro:~/latest_projects/books$ cd ..
timo@Tims-MacBook-Pro:~/latest_projects$ rm -rf books

Now when I type ls, I will no longer see books listed.

Try It: Removing files (rm) and directories (rm -rf)

files and directories 2

We'll work through these exercises together.

For this scenario, assume that each question is independent of the rest, and that the starting point is always the diagram to the left.

I'm in to_do. What do I type to remove random.txt?

I'm in to_do. What do I type to remove the home directory?

I'm in the work directory. What two commands do I type to remove the home directory?

I'm in the projects_to_delegate directory. What two commands do I type to remove the directory I'm currently in?

I'm in projects_to_delegate. What four commands do I need to type to remove the cleaning.txt file?

Putting it All Together

At this point, we'll split into breakout rooms. The person whose name first name starts with the letter closest to T will share their screen by clicking the green "Share Screen" button. We will call this person the driver. Every other person in the room will be a navigator.

The driver will be the person typing the commands, but the navigators should be the ones who brainstorm what comes next or what to type. In addition to naming the steps, discuss why you are doing each step, or what exactly is taking place in each step.

If you are already comfortable with commands, your challenge during the breakout is somewhat more difficult: your goal is to focus on your explanations, communication, and ability to gauge whether or not another person understands you.

Challenge #1

  1. Type cd to get to your home directory (you’ll probably already be here, but do it just to be sure)
  2. Make a new directory called terminal_practice
  3. Move into that directory
  4. Print your current directory
  5. Make a file called favorite_foods.txt
  6. List the contents of your directory (you should see just your favorite_foods.txt file appear)
  7. Use Atom add your #1 favorite food to the file
  8. Add your #2 favorite food to the file
  9. Use the cat command to show what’s in that file
  10. Delete the favorite_foods.txt file
  11. Get back out of the terminal_practice directory
  12. Remove the terminal_practice directory

Challenge #2

For this next challenge, the person whose name is next closest to the letter T will share their screen and become the driver.

  1. Type cd to get to your home directory (you’ll probably already be here, but do it just to be sure)
  2. Make a new directory called my_first_projects
  3. Make another new directory called my_other_projects
  4. List the contents of your directory (you should see these two directories you just made in the list)
  5. Remove the my_other_projects directory
  6. Move into the my_first_projects directory
  7. Make a file called ruby.txt
  8. Make a file called javascript.txt
  9. Make a file called python.txt
  10. List the contents of your directory (you should see the three files you just created)
  11. Delete the ruby.txt file but leave the others
  12. Get back out of the my_first_projects directory
  13. List the contents of your directory (you should see my_first_projects)
  14. Remove the my_first_projects directory

Challenge #3

The person who has not yet been the driver will share their screen.

  1. Type cd to get to your home directory (you’ll probably already be here, but do it just to be sure)
  2. Make a new directory called session3_practice
  3. Move into the session3_practice directory
  4. Print the path to your current directory
  5. Make a file called terminal.txt
  6. List the contents of your directory (you should see the terminal.txt file you just created)
  7. Add the text “The terminal is an interface to give commands to the computer” to the terminal.txt file
  8. Use the cat command to check that the text got added to the terminal.txt file
  9. Get back out of the session3_practice directory
  10. Remove the session3_practice directory

BREAK

Turn off your mics and videos and walk away from the computer. Stand up, stretch, drink water. Do a few sit-ups, squats, push-ups, jumping jacks, arm circles, stress ball squeezes, or whatever else moves your body.


Git

Git is a Version Control System (VCS). It allows you to save work on your project, and reference previous states of a project if needed. Normally when we save something on our computer, the newer version overwrites the older version. This is problematic if we need to look back at an earlier version. Git solves this problem by providing you multiple save points. You can get the current version, and ANY previous version. Git’s philosophy: never lose anything.

Git works similarly to the way that Google Docs tracks changes across time. Let’s take a brief detour to this Google doc.

Why Use Git?

There are numerous reasons to use git. Among them:

  • The ability to “go back in time” to previous versions of your code if you break something
  • The ability to “go back in time” to previous versions of your code if you change your mind about a feature
  • The ability to chunk parts of your work so that you can choose from chunks to keep at a later time
  • The ability to work collaboratively on the same project (even the same files!)
  • The ability to use branches to separate features of a program
  • The ability to track who made specific changes to specific files

In this section on git, we’ll learn (or review) these git comamnds:

Action Commands

Safe/Informative Commands

Things to Note

Git has a steep learning curve, and it can be super confusing at the beginning. Tonight's lesson is not meant to make you a git master, but it will expose you to the basic git commands and workflow. Most likely, you will not have the commands or workflow memorized after tonight.

To get comfortable with git (just like with anything else), you will need to put in hours of practice. It's an excellent idea to go back over this lesson (either in print or recording) multiple times.

Watch and take notes during the demonstration of the git workflow. After the demonstration, we’ll move into breakout rooms where you’ll get to try with your group.

Getting Set Up

We’ll start with a directory called notes with a file inside called things_to_remember.txt.

1. Start Tracking a Directory With Git (git init)

By default, your changes will not be tracked. In order to tell git that we want to start tracking something, we need to initialize git in a directory.

timo@Tims-MacBook-Pro:~/notes$ git init
Initialized empty Git repository in /Users/timo/notes/.git/

This means git now HAS THE ABILITY to track changes in the notes directory. This does not mean that git is tracking anything yet. Git is now prepared to track activity in th notes directory

NOTE: If you don’t see this message when you try git init, it means you have not set up git on your computer.

Things to Note

1. You only need to initialize a directory once in order to track everything else inside of it.

For example, if I typed this while in the essays directory:

timo@Tims-MacBook-Pro:~/essays$ git init

Then every file inside that directory, regardless of how deeply nested, would be available for git tracking.

Do not run the git init command more than once in the same directory or in child directories of the directory you initially ran it from. It will never be the answer to the problem you are facing

2. Empty directories are not tracked by git. Therefore, if you create a directory but there's nothing inside of it, git will ignore it. Git will only track it when there is a file inside of the directory.


3. What happens if you accidentally initialize git in a directory where you didn't intend?

When you type git init, a directory called .git is created inside of that folder. You can't see this folder using the ls command since it's a hidden folder. Everything that starts with "." is hidden. Instead, you can type ls -a which will display all hidden and visible files and directories.

In an git initialized directory, you'll see a .git directory. To remove git tracking, you would type:

rm -rf .git

This removes the git directory, and you are no longer using git to track your project. If you accidentally run git init in your home directory, and start tracking most of the files and directories on your entire computer, then remember to run rm -rf .git from your home directory. One way to tell if you've accidentally initialize git in your home directory is to first go home by running:cd ~ and then run: git status. If you see files being tracked but not staged for commit, then you need to remove the .git folder as layed out above.However, if your terminal returns:fatal: Not a git repository (or any of the parent directories): .git, then you are all good.

2. Check the Status (git status)

When you want to see what git is tracking and the current status of files, you can use git status:

timo@Tims-MacBook-Pro:~/notes$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  things_to_remember.txt

nothing added to commit but untracked files present (use "git add" to track)

No commits yet means that we haven’t asked git to save a version yet. You can think of a commit as a snapshot of your work at a certain point in time. In the Google Doc, each save point was a commit. So far, we have no commits for our notes project.

Untracked files refers to the fact that even though we initialized a git repository, we haven’t started tracking anything yet. It lists the files that are available to track. (Also note “untracked files present” near the bottom).

Things to Note

You can type git status as many times as you want, whenever, wherever. It it a safe command that will not change anything about your tracking.

Let’s talk briefly about this diagram from the git documentation. This diagram shows what you can do after you have initialized git:

Git Workflow Diagram

Source: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

3. Add a File to Track (git add <filename>)

Let’s add one of the files that we want to track to the staging area. Google’s dictionary defines “staging area” as “a stopping place or assembly point en route to a destination”. If you’ve ever run a race, you know that the staging area is the place where all of the runners gather before the race begins. It’s a place to make sure everything is good to go before continuing on.

We will add our file to the staging area.

timo@Tims-MacBook-Pro:~/notes$ git add things_to_remember.txt
timo@Tims-MacBook-Pro:~/notes$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

  new file:   things_to_remember.txt

Notice the “changes to be committed” section: It tells us that we have a new file, things_to_remember.txt, that is ready to be committed. We can say that the things_to_remember.txt file is in the git staging area. That means no changes are tracked yet, but we’re ready to take a snapshot of it (we’ll do this next).

This may seem silly right now, given that we only have one file – why not just commit the file? Why do we have to add it first? Let’s take a small detour.

4. Take a Snapshot in Time (git commit -m <message>)

Now that our file is in the staging area, let’s tell git that we’re ready to commit (take a snapshot of this moment to save). It’s good practice to use “Initial Commit” for your very first commit in each repository.

timo@Tims-MacBook-Pro:~/notes$ git commit -m 'Initial commit'
[master (root-commit) b7654a6] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 things_to_remember.txt

Now if I look at my git status, this is what I see:

timo@Tims-MacBook-Pro:~/notes$ git status
On branch master
nothing to commit, working tree clean

Let’s figure out on the diagram what we just did:

Git Workflow Diagram

5. Making File Changes, Adding New Commits, and Viewing Those Changes By Using (git diff <filename>)

Now that we have our initial commit (or snapshot in time), let’s make a change to one of our files.

We’ll open up Atom to make these changes - Do you have auto-save enabled?

Your working directory now has changes that differ from what our file looked like the last time we committed (or took a snapshot).

timo@Tims-MacBook-Pro:~/notes$ git diff things_to_remember.txt
diff --git a/things_to_remember.txt b/things_to_remember.txt
index e69de29..9ffd068 100644
--- a/things_to_remember.txt
+++ b/things_to_remember.txt
@@ -0,0 +1,2 @@
+Be kind
+Work hard

Then add your changes and commit them using an imperative verb that starts with a capital letter and a short commit message explaining what you did.

The basic git workflow is this:

  1. Make file changes - we’ll do this in Atom
  2. Check the git status to see what files are unstaged
  3. Check the git diff <filename> to see what changes have happened since the last commit
  4. git add <whatever file you want to stage>
  5. Check the git status to make sure the correct files are in the staging area
  6. git commit -m 'Message about your changes' to commit the changes

Try It: Git Together

At this point, we'll split into breakout rooms. The person whose name is first alphabetically will start by sharing their screen, unless this person already has significant git experience. Whoever is sharing their screen will be called the driver. Every other person in the room will be a navigator.

The driver will be the person typing the commands, but the navigators should be the ones who brainstorm what comes next or what to type. In addition to naming the steps, discuss why you are doing each step, or what exactly is taking place in each step.

If you are already comfortable with git, your challenge during the breakout is somewhat more difficult: your goal is to focus on your explanations, communication, and ability to gauge whether or not another person understands you.

  1. Get back to your home directory
  2. Create a new directory called to_do. Inside of that directory, create a file called tasks.txt.
  3. Initialize git inside of the to_do directory so that we have git available to track changes.
  4. Before you check the status, predict what it will say. Then, check the current status.
  5. Add tasks.txt to the staging area.
  6. Make your first commit in this directory.
  7. Before you check the status, predict what it will say. Then, check the current status.
  8. Use Atom to add three separate tasks to tasks.txt.
  9. Before you check the status, predict what it will say. Then, check the current status.
  10. Before you check the difference between your last commit and what the file looks like now, predict what it will say. Then, check the diff.
  11. Add the changes to the staging area.
  12. Before you check the status, predict what it will say. Then, check the current status.
  13. Commit the changes using a properly formatted commit message.
  14. Before you check the status, predict what it will say. Then, check the current status.

Done? Switch drivers and try the process again!

Things to Note: Oh no, my terminal is broken!

  1. It is highly likely you will make mistakes in the terminal. Do not fret, there are a few tools to save you when you inevitably receive unexpected feedback or behavior from the terminal.
  2. control + C shortcut will stop most terminal tasks and bring you back to your current directory with no harm done. Try this first when you encounter unexpected behavior.
  3. At some point your terminal will look like it does in the photo below. This is Vim land. Vim is a text editor within your terminal that is beyond the scope of mod-0. When you find yourself here, you can type: :q, which stands for quit in Vim, and then hit enter to exit Vim. Sometimes, if you've already typed some characters, you need to use control + Cin order to get to a point within Vim where :q or :qa!, which is the command to quit and abandon changes, will successfully exit Vim.

Mistakes in the terminal

Terminal Mistakes Screencast

Close Out

Understanding git and the workflow can be tricky at first; however, this pattern is one that you will come to memorize over time. As you are memorizing the steps, be sure that you know what each step does.

Today (and during Mod 0), we’ll just be moving forward in time using git. We won’t learn how to go back in time, but it’s important to understand and master making small commits before adding in new git functionality.

We do not expect you to have these steps and reasons memorized right now. You can always reference back to this tutorial (or other tutorials that you find online). The more you practice, the easier it will become.

Homework

Find the homework in your Mod 0 Project Board. Post in the Mod 0 Slack channel if you’re stuck (and don’t be afraid to help each other out).