What is git?

Git (/ɡɪt/) is a version control system

git branch

Version control is a system that records changes to a file or set of files
over time so that you can recall specific versions later.

Pro Git book

git init

Create a new directory somewhere on your system

$ mkdir ~/Desktop/git-test && cd ~/Desktop/git-test

Create something for git to track

$ echo "This is a README" > README

Setup git in this directory

$ git init

-> Initialized empty Git repository in /Users/you/Desktop/git-test/.git/

Git is now initialized in this directory and can start to track changes to files.

It has created a hidden directory inside the folder where it keeps track of all changes, you can see it if you type ls -la. The git repository is entirely self contained and does not depend on any other file outside of the folder.

git status

Now that we have set up git what can we do with it?

One useful command we could try is the status command.

$ git status

-> On branch master

   No commits yet

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


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

Nice. Git just gave us some very useful information!

It told us that we have no commits yet, and also that we have an Untracked file. Let’s go ahead and add this file so git can start to track it.

git add

Add untracked files to git using the add command

$ git add README

Let’s ask git about its status again

$ git status

-> On branch master

   No commits yet

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

           new file:   README

We can see that the status has changed. The file is now to git in a state called “staged”.

In order for git to begin to track it we need to commit it.

git commit

The commit command tells git that we want to record the current state of its staged files.

$ git commit -m "Initial commit"

-> [master (root-commit) 9319ba3] Initial commit
    1 file changed, 1 insertion(+)
    create mode 100644 README

We passed the -m flag in order to supply a commit message from the command line. Its common to call the first commit “Initial commit”. The initial commit is also known as the “root commit”, from where our tree of recorded changes will grow.

Let’s take a look at the status again

$ git status

-> On branch master
   nothing to commit, working tree clean

All good!

Life-cycle of file changes

git lifecycle

Summary of what we know so far

Command Description
git init Initialize a git repository
git add Add files for git to track
git commit Commit files and/or changes to files

The content of this post was presented on a c0dereview meetup in December 2017, you can view the slides from the presentation here.

Illustrations from Pro Git Book (CC BY 3.0)