The 12 Days of Git, Day 2: Tracking Files with Git

FacebooktwitterlinkedinmailFacebooktwitterlinkedinmailby feather

On the second day of Christmas, my true love gave to me… two files that have been versioned.

Okay, we’re ready to get into the nitty-gritty of using Git.

Blue File Cabinet
Blue File Cabinet, by Seth Cohen, Creative Commons image from Wikimedia

On our “first day of Git,” in Learn Git over the Holidays, we created a folder for an app called santa-tracker and initialized a Git repository in that folder. That folder, and the accompanying repository are currently empty.

Today, we are going to create a couple of files and then add them to the Git repository.

First, in your terminal make that folder your current working directory. Then, using whatever text editor you like, create a couple of files that will be in the app. For instance, I’m going to start the scaffolding of a web app, with an index.html file and a main.css file.

Here’s my index.html file:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Santa Tracker</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <h1>Santa Tracker</h1>

And here’s my main.css file:

body {
    background-color: #222;
    color: #f0f0f0;
h1 {
    text-align: center;

Even after you have these files in your repository directory, Git won’t automatically start tracking changes to them. To see what I mean, let’s use a new Git command:

git status

You should see the following output:

On branch master
Initial commit
Untracked files:
  (use "git add ..." to include in what will be committed)
nothing added to commit but untracked files present (use "git add" to track)

Even though Git is not tracking those files yet, it detects them, and even suggests the command you can use to start tracking them. The git add command takes a file name or a path to a file as an argument, and it tells Git that you want to add the current state of that file to the next version of the repository you save.To start tracking versions of files with Git, you need to tell Git which files you want to track. This is called “adding files to the staging area”.

Instead of typing that command for each file, we’re going to use a shortcut and give the git add command special argument of -A. The “-A” is short for “–all”. In your terminal, type:

git add -A

Now, if you type git status again, you will see:

On branch master
Initial commit
Changes to be committed:
  (use "git rm --cached ..." to unstage)
	new file:   index.html
	new file:   main.css

You’ll notice that the status output says Changes to be committed. The way Git “stages” changes—add and then commit—takes a little getting used to. We haven’t quite set this first version of the project in stone yet. We have to commit these change as a version with a name. While Git will track each file individually, you can “stage” several changed files at the same time, and give that set of changes a name. This will come in handy later, because you can look back through the names of these “changesets” and read the history of your work on the project.

What’s more, if you are making changes quickly, change two different files for different reasons, and then realize that your next version would reflect two totally unrelated changes to your project, you can git add each file separately, and then commit each change separately.

So, let’s wrap this verse of the song up by committing both of these new files at the same time. In the terminal, type:

git commit -m'Add first files to the repository'

You will see something similar to this output:

[master (root-commit) 8bfbaa2] Add first files to the repository
 2 files changed, 19 insertions(+)
 create mode 100644 index.html
 create mode 100644 main.css

That’s all there is to it. The git commit command requires a message, which you put in quotes and preface with a -m. If you forget the message, or need to include quotes in the message, or need several lines to explain a set of changes, you can just type git commit, and Git will open up your default text editor and use whatever you type there for the message.

To see all the options available for the commands we’ve learned today, check them out in the online Git reference:

Check in tomorrow for Day 3: Viewing Git History

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.