Git lesson 2: add and commit a file to a repo

In the previous lesson, you created a new folder called project_repo that contained a file called doc1.txt. You then initialized the folder as a repository by running the command git init in that folder. Subsequently, Git started to monitor everything in your repository: new files, deleted files and changed files. You also learned how to check the current state of your repository using the git status command, as well as the git st alias you previously created.

In this lesson you will learn how to add a file so that Git knows to keep track of it. You will then save (i.e., commit) this change to your repository.

Add file to repository

You will now make Git track doc.txt using git add <filename>:

# Add file to repo
git add doc1.txt
# Check status of repo
git status

Running git status will return:

On branch master

Initial commit

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

    new file:   doc1.txt

This message tells you there are changes in your repository that can now be committed. Specifically, the message says there is a new file in the repository. If you made a mistake and did not intend to add doc1.txt to your repository, Git tells you to run git rm --cached doc1.txt to undo your previous action.

Staging. When you add a new or changed file in Git, it is often referred to as staging a file. You can stage many new or changed files simultaneously. You can even use git add * to add all changes.

Think of staging as a way of telling Git what new or changed files you want to include the next time you save the state of your repository. Rather than keeping a record for each added or modified file, you can add a selection of new and modified files, then save these changes as a single save or commit action. This flexibility allows you to group related changes together into one save action.

As you will see in future lessons, you can often skip the staging phase and directly save all changes made to your repository.

Now that you have told Git to start tracking doc1.txt, have a look at the status of your repository with your st alias:

git st

Running this command should return A doc.txt, which informs you that you have a staged (or added) file in your repository.

Commit changes

You are now ready to save a snapshot of your repository using git commit. Think of this snapshot as a save point in a video game or a backup tool; in Git this snapshot is call a commit.

If you run git commit on its own, Git will open your default text editor and you will be instructed to type a commit message. This message should be a brief summary of the changes included in the current commit. You have to provide a commit message; Git won’t let you make the commit if you don’t provide one.

If your commit includes substantial or complex changes that are not easily summarized in a short sentence, add a blank line after your short sentence and describe the changes in the commit in more detail. However, don’t simply list the various changes that will be committed, as this information can easily be retrieved. You want to provide the reader of the commit message (usually your future self) with an explanation of why these changes were needed. While the rationale for a change is usually clear when you make it, things are often fuzzier a few months or years later. So do yourself a favour and write informative commit messages.

Because your current commit is simple (i.e., add doc.txt), you only need to provide a short sentence as your commit message. In such instances, the commit message can be inputed via the command line using the -m flag when you run the git commit command.

# Make initial commit to repo
git commit -m 'Add doc1.txt to repo on branch master'        
# Check status after commit.
git status

You have successfully made your first commit! There should now be nothing outstanding in your repository. To convince yourself of this, run the git status command.

Summary

In this lesson you told Git to start tracking changes to a file in your repository using the git add command. You then saved a snapshot of your repository using the git commit command. In the next lesson you will learn how to view your repository’s log, as well as discover how to use the in-built Git help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s