Difficulty: beginner
Estimated Time: 30-35 minutes

git initgit configgit addgit commitgit diff

Version control allows you to record changes to a file or set of files over time enabling you to go back to a specific version if required. Git is a Distributed Version Control System (DVCS). This means that instead of having a snapshot of the latest files, you have a complete mirror of the repository on your local machine. The repository keeps track of the changes, when they occurred and who by. Having the complete repository on your local machine reduces delays due to network traffic and enables you to continue working when disconnected. The high level points this lab will cover are:

  • Initializing a new Git repo
  • Committing a modified version of a file to the repo
  • Common Git version control commands
  • Interacting with GitLab

By the end of this lab, you should be able to create a Git repo, use common Git commands, commit a modified file, view your project's history. This Scrapbook environment has been pre-configured with git installed. For more information on installing Git, please refer to the official documentation.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Git SCM - Lab 101

Step 1 of 8

Step 1 - Initialize git repository

A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed. To store a directory under version control you need to create a repository. With Git you initialise a repository in the top-level directory for a project.

To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new .git subdirectory in your current working directory. This will also create a new master branch.

When a directory is part of a repository it is called a Working Directory. A working directory contains the latest downloaded version from the repository together with any changes that have yet to be committed. As you're working on a project, all changes are made in this working directory.

Task

Populate a new project

We'll start off with creating a simple project. Go ahead and create a README.md file. And add some content to the README.md

# Simple HTML Project

This is my first simple HTML project tracked by git.

Then another file called index.html with simple HTML content.

<‍!DOCTYPE html>

<‍html>
  <‍head>
      <‍title>Page title<‍/title>
  <‍/head>

  <‍body>
    <‍h1>My First Heading<‍/h1>
    <‍p>My first paragraph.<‍/p>
  <‍/body>
<‍/html>

These two files will form the initial release of our small project.

Now that the project has been initialized with some files, let's inspect it from the command line. Switch to the Terminal section and use ls, which will show you the two files which you have created.

So far we have established a basic project which we can track it's changes with git.

Initializing a new repository / git init

As this is a new project, a new repository needs to be created. Use;

git init

to create an empty Git repository - basically a directory called .git/ with subdirectories for objects, refs/heads, refs/tags, and template files. An initial HEAD file that references the HEAD of the master branch is also created. You will now have a .git directory, and you can inspect that with

ls -a

Inspect the contents of the .git/ directory with:

ls .git/

which reveals all the files and directories that construct the repository. For your new empty project, it should show you three entries, among other things:

  • a text file called HEAD, that has the content ref: refs/heads/master in it. This is similar to a symbolic link (a shortcut) and points at refs/heads/master relative to the HEAD file.
  • a subdirectory called objects/, which will contain all the objects of your project. You should never have any real reason to look at the objects directly, but you might want to know that these objects are what contains all the real data in your repository.
  • a subdirectory called refs, which contains references to objects.

Inspect the repository / git status

You can view which files have changed between your working directory and what's been previously committed to the repository using the command:

git status

The output of this command is called the "working tree status". git status displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git (and are not ignored by .gitignore). The first are what you would commit by running git commit; the second and third are what you could commit by running git add before running git commit.

ProTip

  • Running git init in an existing repository is safe. It will not overwrite things that are already there.
  • All files are "untracked" by Git until it's been told otherwise. The details of how is covered in the next step.