git init

  • Tutorial
  • Cheatsheet
  • Resources

1/13 Introduction

Instructions

Every step in the tutorial starts with a text. At the end of the text is a button that starts the execution of a git command.

After clicking the button 4 things will happen, in the following order:

  • In the console (at the bottom left): The command will be added and executed.
  • On the right: An animation will show what the command does.
  • When the animation ends, the console shows the output of the command.
  • At the right of the button will appear a link to go to the next step.

The official definition of git is "a distributed version control system". But what does it mean? Let's use an example to explain it.

In the next table we have some changes we did to a file:

Time Status
July 7, 10:00 Create file tasks.txt.
July 7, 10:05 Add paragraph to file tasks.txt.
July 7, 10:10 Format the content of tasks.txt.
… …

We can think of every row in the table as a version of the file. So git is a system that helps us track version or changes of files through time (it is also distributed, but that's a feature for another tutorial).

Git basics

To initialize git inside a folder, we use the command git init. After executing this command, git will start tracking changes inside the folder.

Internally git have three states (also called areas) where changes can be in: working directory, staging area, and repository.

Click the next button to start the animation.

git init Next

2/13 First changes

The first thing we need to see git in action, is a file. We can create a new file with the command touch and passing a filename as the first argument (this is a UNIX command not a git one).

This command will create the file tasks.txt. In this file we will keep a list of tasks we need to do.

The working directory area tracks all changes we make inside the folder. So git will show this new file inside this area.

touch tasks.txt Next

3/13 Adding changes to the staging area

A commit is a change, or a group of changes, we wrap together and tell git to save. It is created in 2 steps: the first one is adding all the changes we want to the staging area.

To add changes to the staging area we use the command git add. The next command moves the changes of the file tasks.txt from the working directory to the staging area.

git add tasks.txt Next

4/13 Creating our first commit

Before, we saw the first of the 2 steps to create a commit.

The second step is explicitly telling git to wrap all the changes currently in the staging area into a commit node. We do this with the command git commit.

We give each commit a message to easily identify its contents later. To pass a message to git commit we use the option –m.

The next command will create our first commit with the message Add file tasks.txt.

git commit –m "Add file tasks.txt" Next

5/13 Modifying a file

The echo command mixed with >> adds lines to the end of a file.

The first task we want to do is to Buy milk. So we will add it to our file tasks.txt with the echo command.

Look how git tells us that the file has changed by adding it to the working directory area.

echo "Buy milk" >> tasks.txt Next

6/13 Git status

Another useful command is git status. This command shows us the changes we have inside the working directory and the staging area.

Look at the output of the command in the console (bottom left). git status will tell us that the changes are not staged (this means that the changes are inside the working directory). All the files with changes inside the working directory will be shown in red. And the file tasks.txt has a modified status because we add a line to it in the last step.

git status Next

7/13 Detailed list of changes

The command git status just shows us the names of the files with changes. If we want to know what changed inside each file of the working directory, we can use the command git diff.

In the output of git diff, the lines that we added to the file are shown in green, and the ones that we removed are shown in red.

git diff Next

8/13 Add again

We want to create another commit with the changes we did to tasks.txt. We need to add the changes to the staging area again with the command git add.

git add tasks.txt Next

9/13 Git status 2

We can use the command git status again to see the message git gives us when we have staged changes. The file tasks.txt still appears with a modified status, but the color has changed to green which means the changes are inside the staging area.

git status Next

10/13 Detailed changes 2

In step 7 we used the command git diff to see the detailed list of changes we had inside the working directory.

This command can also show the changes inside the staging area, if we pass the option ––cached to it.

The output has the same color code: the lines that we added are shown in green, and the ones that we removed are shown in red.

git diff ––cached Next

11/13 Our second commit

We want the changes in the staging area to be a new commit. We use the command git commit again, and pass a descriptive message that identifies this commit.

git commit –m "Add first task" Next

12/13 Seeing our commits

The command that completes this tutorial is git log. This command shows us the commits we have made.

git log has various options to show the output with different detail levels. We use the option ––oneline to see the output in a short format.

git log ––oneline Next

13/13 Closing

This tutorial has all the basic commands you need to start using git in your personal projects.

There is a lot more to learn about git, look at the Resources section for a list of next steps to expand your knowledge of git.

Also you can use the Cheatsheet section as a reference. It has all the commands in this tutorial with explanations for the most used options.

>
Initialized empty git repository in my_folder/.git/
>
>
>
[master (root-commit) cc04c1f] Add file tasks.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 fileName
>
>
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout –– <file>..." to discard changes in working directory)
modified: tasks.txt
no changes added to commit (use "git add" and/or "git commit –a")
>
diff ––git a/tasks.txt b/tasks.txt
index e69de29..557fb5d 100644
––– a/tasks.txt
+++ b/tasks.txt
@@ -0,0 +1 @@
+– Buy milk
>
>
On branch master
Changes to be commited:
(use "git reset HEAD <file>..." to unstage)
modified: tasks.txt
>
diff ––git a/tasks.txt b/tasks.txt
index e69de29..557fb5d 100644
––– a/tasks.txt
+++ b/tasks.txt
@@ -0,0 +1 @@
+– Buy milk
>
[master ff5dac2] Add first task
1 file changed, 1 insertions(+)
>
ff5dac2 Add first task
cc04c1f Add file tasks.txt
>

Working directory

Staging area

Local repository