Let's Create Code

Feedback welcome: hello@simplegitcourse.com

Chapter 2: Versioning

This module will cover the most essential concept to Git: versioning code.

This chapter is a continuation of the “Use Case 1: Capturing Changes” section in the previous chapter. If you happened to have skipped over that section, I strongly recommend reading it before proceeding.

In this chapter, we will be learning these core skills:

  1. Creating sample files using a CLI (setting up a new project).
  2. Telling Git about the existence of those files (initializing Git).
  3. Making and capturing changes to those files (Commits).

Before we start, ensure you understand the terminology below. Some readers are new to programming and may not understand these:

  • “Project”: in this case, it is simply a directory on our computer with a list of files.
  • “Directory”: basically a folder on your computer.
  • “Current directory”: directories can be listed in other directories, kind of like a tree. When we are using a CLI, we always will enter commands in the context of a given directory. For example, if we create a file, based on the directory we are in at the moment, the file will be created there.
  • “Going up a directory” or “going into a directory”: since directories are kind of like a family tree, where there is a root directory on your computer, and multiple children, grandchildren directories, etc, we can go up a directory by changing to that directory. Same goes for navigating to a child directory.
  • “Print”: showing something on the CLI

Next, here is a handy cheatsheet of the CLI commands we will be using in the forthcoming sections:

  • “mkdir”: make a directory
  • “cd”: change directory
  • “ls”: list files in a directory
  • “echo”: print out something to the CLI that you enter
  • “pwd”: print the current directory we are in (“print working directory”)
  • “cat”: concatenate and print files

| For advanced students: if you ever want to read up on what a command does, you can type “man <command>”. “Man” stands for “manual”, which allows you to read the documentation on that command. Give it a try!

Finally, here is a cheatsheet of the Git commands we will be learning in this chapter:

  • “git init”: creates a new Git repository
  • “git status”: shows which files in our directory have been changed.
  • “git commit”: creates a commit
  • “git log”: prints our Commit history
  • “git diff”: prints the detailed list of changes made since the last Commit

Setting Up Our Project

To try out Git, let’s first create some files with text in them. To keep things simple, we will not be adding actual code.

| Note: if you are using Windows to follow along, ensure you are using the Git Bash application, as explained in Chapter 1 regarding Git installation, so that these commands work.

Ok, let's get started!

We can use the default directory that we are placed in when the terminal opens.

Now, create a new directory called “closet”:

mkdir closet  

We’ve named this “closet” because this is where we will store (and version) different things that will go inside of an imaginary closet. We’ll be adding text to different files that includes names of different types of clothing.

`mkdir <directory name>` here stands for "make directory".

Now navigate to the `closet` directory:

cd closet  

"cd" stands for "change directory", which allows us to navigate to the respective directory.

Let’s place our first file into this directory. Run the following command to add some text to a new file, README.md:

echo "A project to help us learn Git." > README.md  

The `echo <text>` command here simply prints back the text you've entered.

The ">" symbol here takes that text and injects it into a new file, in this case, `README.md`.

The `.md` file type stands for "Markdown" and is similar to a `.txt` file that you are probably already familiar with, except it can include special formatting.

| Note: since this course covers as realistic examples as appropriate, note that "README.md" is a common file that is typically contained in a code project. It typically will contain a high level overview of the project as well as useful commands for first-time use (i.e. “read me first if you're new to this project!”).

You should see the following printed to the screen:

A project to help us learn Git.  

Now, let’s list all the files in our directory and confirm no other files are here:

ls  

“ls” stands for list files.

You should only see:

README.md  

You shouldn’t see any other files.

Now let’s confirm the contents of the file are in there:

cat README.md  

You should see the contents of the file printed out to the screen.

Perfect, now we have a starter file which represents our new project and we are ready to version our code. We'll do this in the next lesson.

Next up: Creating Commits