Add The Project To A Git Repo

Goals

  • Create a local git repository

  • Add all our files to the git repository

  • To track our changes over time, we are going to use a version control system — we have chosen to go with git.

Steps

Step 1

Open up a second terminal. For Mac OS X, press Cmd+T. On Linux, try Ctrl+T and on Windows, you will need to open a new window the same way you opened the current one.

Once you have a second terminal, connect to the virtual machine again:

vagrant ssh

Step 2

Switch to our project directory

Type this in the terminal:
cd /var/www/suggestotron

Step 3: Setup Git

Type this in the terminal:
git config --global user.email "[email protected]"
git config --global user.name "Your Name"

This tells git (and anyone who has access to your repository) who made the changes — this is critical when working in teams.

Step 4: Initialize our Repository

Type this in the terminal:
git init
Expected result:
Initialized empty Git repository in /var/www/suggestotron/.git/

It doesn't look like anything really happened, but 'git init' initialized its repository (repo) in a hidden directory called .git. You can see this by typing ls -al (list all files).

Step 5

Type this in the terminal:
git status
Expected result:
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# public/
#
nothing added to commit but untracked files present (use "git add" to track)

git status tells you everything git sees as modified, new, or missing.

Step 6

Type this in the terminal:
git add .

git add . tells git that you want to add the current directory (aka .) and everything under it to the repo.

git add

With Git, there are usually many ways to do very similar things.

  • git add foo.txt adds a file named foo.txt
  • git add . adds all new files and changed files, but keeps files that you've deleted
  • git add -A adds everything, including deletions

"Adding deletions" may sound weird, but if you think of a version control system as keeping track of changes, it might make more sense. Most people use git add . but git add -A can be safer. No matter what, git status is your friend.

Step 7

Type this in the terminal:
git commit -m "Created my first PHP file"

git commit tells git to actually do all things you've said you wanted to do.

This is done in two steps so you can group multiple changes together.

-m "Created my first PHP file" is just a shortcut to say what your commit message is. You can skip that part and git will bring up an editor to fill out a more detailed message.

Approximate expected result:
[master (root-commit) a74d816] Created my first PHP file
 1 file changed, 3 insertions(+)
 create mode 100644 public/index.php
The result you get may differ and is not important.

Explanation

By checking your application into git now, you're creating a record of your starting point. Whenever you make a change during today's workshop, we'll add it to git before moving on. This way, if anything ever breaks, or you make a change you don't like, you can use git as an all-powerful "undo" technique. But that only works when you remember to commit early and often!

Next Step: