Exercise Monday

Goal: Follow along with this lesson to see how changes are tracked using Git. We'll create a one-page website that says "hello" to the world. The steps we follow in this lesson will reflect the daily workflow we'll use when coding all of our projects. Use the details in the cheat sheet as reference for starting all of your projects.

Git Name and Email

In order to save code using the Git version control system, Git must have a name and email to stamp every time we take a snapshot of our work. This information is set up differently depending on whether we work with a partner or work alone.

Pair Programming Setup (In Class)

When you're pairing at Epicodus, the names and emails of both partners will need to be set.

To accomplish this, we use a program called pivotal_git_scripts. It's already installed on Epicodus computers. We'll only use this tool in class, so you don't need to install it on your personal machine, but if you'd like to learn more about it, you can check out their documentation here).

While Working at Epicodus: In the home directory (e.g. /Users/Guest/ on Epicodus computers), we will create a file called .pairs that matches this example. Then, we'll open this file in Atom to make it look similar to the example below. Feel free to copy and paste this text from the lesson and add your pair's personal details, including their initials, name and email. If any of the spacing or section header and content is off, it will not work

.pairs
pairs:
  al: Ada Lovelace 
  edlf: Enrique de la Fuente 
email:
  al: [email protected]
  edlf: [email protected]  

In this example, Ada and Enrique will be working as a team to create our hello-world web page. They will only need to create the .pairs file once at the start of each day, as the same .pairs file will be referenced by each project they work on.

The names used in this file will be publicly-visible in your and your partner's commits on GitHub.

Working Solo Setup (Outside of Class)

When working alone on a project, we do not need to use a .pairs file. We do this with a global configuration in the terminal:

$ git config --global user.name "Padma Patil"
$ git config --global user.email [email protected]

This sets the name and email for every save that is made in any directory anywhere on the device. If you are setting this on your personal device you will only have to set this once for it to be set on every project.

Starting a New Project

Whenever we start a new project, we create a new project directory.

If we enter ls at the prompt, we can see the list of what other files and directories are in our home directory. We need to decide if this is where we want our new project to be stored. If you are on your own device, you may want to create a new directory that stores all of the code you plan to create.

Creating a Project Directory

At Epicodus, we will create our project directories in the Desktop directory for easy access. Let's change directories from our home directory into our Desktop using the cd command:

$ cd Desktop

Now, we can add our hello-world project directory :

$ mkdir hello-world

Another ls shows that our hello-world directory was created. This is also mirrored by the new folder on the Desktop of our machines.

Let's change directories again so that we are now in the hello-world directory:

$ cd hello-world

To confirm that we are where we think we are, we can always show our location with a pwd:

$ pwd

Initializing Git and Git pair

The next step before we create a single file for our hello-world website is to create a Git directory within our project directory that will track everything we add, modify and delete.

We do this by initializing a new Git repository:

$ git init
Initialized empty Git repository in /Users/staff/Desktop/hello-world/.git/

Now if we do an ls, we might expect to see the new directory. But where is it? Directories beginning with a . are called hidden files and do not appear when you list the files with an ls. To see hidden files, we have to add a modifier to our ls command, -a which tells our terminal to list all:

$ ls -a
.git

On Windows you can use

> ls -force

There it is! The .git directory is a directory like any other. Let's cd into it and ls it's contents.

$ cd .git
$ ls
HEAD        description info        refs
config      hooks       objects

We aren't going to spend ANY time in this directory. The work done here to track our files is going to happen automatically. In fact, the reason the directory is hidden is to help ensure that we stay out of it to avoid making any changes to the very important work that Git will be doing for us. Now, as we add, update and delete files, Git will be in the background like a scribe, making notes of every change in our project directory.

So, let's return to the top level of our project directory by changing directories again, moving up one level:

$ cd ..

With our Git repository set up, our final step is to let Git know who will be writing changes to this project. Using the initials set up in the .pairs file, we can run the git pair command (if you are working solo, this step is unnecessary):

$ git pair al edlf
local:  user.name Ada Lovelace and Enrique de la Fuente
local:  user.email [email protected]
local:  user.initials al edlf

If we ever need to verify who the current pair assignment is, we can use the git about command:

$ git about
git user:       Ada Lovelace and Enrique de la Fuente
git email:      [email protected]
GitHub project: NONE

Now, we are ready to add a new file to our project. This will be the HTML page that will say "Hello" to the world.

Terminology


  • initialize: In Git, to create a new, empty repository to track changes to the project directory. Created in the project directory.

  • global: A configuration option that refers to every directory in every location of the device.

  • hidden files: Are not listed with an ls terminal command, but will be displayed with an ls -a terminal command. The .git directory is hidden by default.

Daily Git Workflow


Set up name and email

Pair Programming

Add this file to home directory (changing name, initials and emails):

.pairs
pairs:
  al: Ada Lovelace 
  edlf: Enrique de la Fuente 
email:
  al: [email protected]
  edlf: [email protected]  

Solo

$ git config --global user.name "Padma Patil"
$ git config --global user.email [email protected]

Create a new project directory with Git repository

In terminal:

$ cd Desktop
$ mkdir hello-world
$ cd hello-world
$ git init
$ git pair al edlf

Note: for solo work, the git pair command is unnecessary.

Git Commands


  • git init: Initializes new local Git repository.

  • git pair __ __: When two sets of initials matching .pairs file are provided, uses pivotal_git_scripts to pair two people together.

  • git config --global user.name ___: Globally configures Git profile for entire device (use only when working solo).

  • git about: Verifies the current pair assignment.