Git lesson 1: Initialize a repository and check its status

In lesson 0 you learned to configure Git. You will now learn how to use Git to start tracking files in a folder.

Let’s create your first repository. The first thing you need to do is create a new folder. This is where you will put the project files you want to put under version control. Don’t worry if you don’t know what version control means, this should become clear by the end of this lesson. We are creating a new folder for the current example, but in the real world you might use a folder that already exists and already contains files.

Repository. Computer files are usually stored in folders, sometimes referred to as directories when related files are stored systematically. When Git is used to track files in a folder or directory, the files, folder and version control information are collectively referred to as a repository, or repo for short.

Run the following commands from the location where you want to create the new folder for this lesson.

Create a folder and file

Following on from lesson 0, you will work with Git from the command line. It is possible to use Git with a graphical user interface (GUI), however it is useful to work from the command line when first starting out. By the end of these lessons, you will realize that working from the command line is fast and streamlined. You may even end up loving the command line… or at least not hating it!

# Create folder for project
mkdir project_repo
# Enter this new folder
cd project_repo

Great! You created a new folder for your project and you are now located in that folder (i.e., it is your current directory). The next thing we want to do is create a file for your project. While you would usually use a text editor of some sort, these lessons primarily use the echo command to write lines to files.

# Write 1 line of text to a text file called doc1.txt
echo 'Line 1, doc 1. Branch master project_repo.' > doc1.txt 

If you want to see what this command did, open the file doc1.txt in a text editor of your choice.

Initialize the repository

You now have a folder, project_repo, that contains a file, doc1.txt. The next thing you want to do is tell Git to start tracking this folder and its content. You will do this with the git init command.

# Initialize the repository
git init                        

If you look inside your project_repo, it might seem like nothing has changed. However, when running git init for the first time, Git creates a hidden folder called .git that contains the gory details required for version control. You will likely never need to look inside this folder, but it’s good to know it exists.

Check status of the repository

You will often want to verify the status of your repository (i.e., to check whether Git notices or is keeping track of files in the repository). If you have changed something in your repository or you are not sure about what files are tracked or untracked, you can run git status to see what is going on in your repository. Let’s verify the status of project_repo:

git status

After running the above command, Git will print the following status message to your screen:

On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    doc1.txt

nothing added to commit but untracked files present (use "git add" to track)

This information may seem a little confusing at first, but don’t worry, the important bits will be explained here and the rest will become clear in the next few lessons.

By default, Git initializes the repository with a branch called master (branches will be explained in a later lesson). Therefore, the first line of the above output message is telling you that you are currently on the master branch.

The next line says that you have an untracked file in your repository (i.e., doc1.txt). The helpful thing about Git is that it often suggests what to do next. In this case, Git tell you that you may want to start tracking doc1.txt with git add.

Before you start tracking doc1.txt, try using one of the aliases you created in lesson 0 to see a shorter status message. Specifically, use your st alias, which stands for status --short:

git st

Running this command returns:

?? doc1.txt

The double question marks indicate that doc1.txt is a file in your repository, but is currently not being tracked.

Summary

In this lesson, you learned how to initialize a repository and verify the status of this repository. In the next lesson you will learn how to add and commit your new file to your repository.

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 )

Facebook photo

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

Connecting to %s