A Beginners Guide to Git


What is Git?

Git is a version control system that is used for software development and other version control tasks. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows[1].

Create Repository

To create a new repository, the command git init is used, within your directory. The repository is contained within the .git folder.


Three trees are maintained by git, within your local repository. They are,

  • Working Directory consists of the actual files you are working on.
  • Staging Area (Index) used for preparing commits. All changes that you want to commit are staged on the first. The index provides greater control over what changes should be committed into the Repository.
  • Object Store (Repository) contains committed changes from the staging area.


Source :


Files can be added to the staging area by using either git add <filename> or git add . (for all modified files).
To commit the files from the staging area to the local repository use git commit -m "comment"


Branches are used to develop new features in isolation. Once the changes/branch have been tested, the branch is then merged back into master branch. The feature branch is then deleted. Below are the main branch commands,

git checkout -b feature-add_button create new branch
git checkout master switch to master branch
git branch -d feature-add_button delete branch
git branch list branches


To push the changes from your local repository to a remote repository (i.e github) enter git push origin master. It is worth noting that master relates to the name of the remote branch.

If you have not cloned an existing repository and want to connect your repository to a remote server, you will need to add it with the commands git remote add origin <server> [2]

Fetch/Merge Vs Pull

To update your local branch/repository with new commits, you can either perform a pull or a fetch then merge. Let me explain,

  • Git Fetch obtains new commits from the remote repo and adds them within the local repository. Like so, git fetch origin master
  • Git Merge is used to merge the changes obtained via git fetch to the local branch. To merge to the current branch enter git merge feature-add_button
  • Git Pull combines both fetch and merge commands together git pull origin master

Clone vs Fork

So that you can work on code independently from a project there are 2 methods, fork and clone.

But what are the differences?

  • Fork creates a copy of the repository within your own Github account.
  • Clone allows you to download the repository locally, along with all of the commit history using the git client. Below is an example on how you would clone a specific branch from a repository,
git clone This email address is being protected from spambots. You need JavaScript enabled to view it.:felix001/commontools.git -b development


There are 3 ways that changes are merged by Git. They are,

  • Fast-Forward - Simplest, No changes are detected on the parent branch. All other commits are added to the parent branch, appearing like there was never a branch.
  • Automatic - Occurs when non-conflicting merges occur. The child branch timeline is preserved and a automatic merge commit is created to show the merging of the 2 branches.
  • Manual - Occurs when git is unable to automatically resolve any conflicts. All merge conflicts must be resolved before proceeding.


The git log command allows you to review your commit history within your repository.

$ git log
commit f790effc8afab5915998964800c4a7b4f472edf8
Author: felix001 <This email address is being protected from spambots. You need JavaScript enabled to view it.>
Date:   Sat Oct 15 22:41:16 2016 +0100


commit 9521a0bf65556438dc2297f53d3bdb1bcd4cb263
Author: felix001 <This email address is being protected from spambots. You need JavaScript enabled to view it.>
Date:   Sat Oct 15 22:38:59 2016 +0100


commit 8f18326957258c8c1334630cd9078317a7fad9b1
Author: felix001 <This email address is being protected from spambots. You need JavaScript enabled to view it.>
Date:   Fri Oct 14 21:40:53 2016 +0100

    add file

This is a powerful command with a number of options, here are some further examples,

git log show the commit history; contains the commit ids
git log -5 show the last 5 git commits
git log --author=felix001 show logs, filtered by author
git log --pretty=oneline compress each commit onto a single line
git log --graph --oneline --decorate --all print the commit history as a funky ASCII graph


Differences can be viewed between staging, commits, the working tree using the git diff command.
However a summary can be display via the git status command.

$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   test_file_b

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


no changes added to commit (use "git add" and/or "git commit -a")

Let's look at the various options within git diff

git diff diff between working directory and staging
git diff --color <filename> diff between working directory and staging, single file
git diff --color --staged <commit_id> diff between staging and last commit
git diff --color <commit_id> diff between last commit and working directory
git diff <commit_id> <commit_id> <file_name> diff between 2 commits same branch
git diff origin/master master diff between remote branch and local branch
git diff origin/master master <filename> diff between remote branch and local branch, single file

TipTo obtain the commit id use the git log command.


There may be a time where you have made a number of changes on your code, but you need to quickly revert back and reapply these changes at a later date. Git stash takes the modified tracked files, stages changes and saves them on a stack that can be reapplied at any time.

The key commands to remember are,

git stash stash changes
git stash list show list of stashes
git stash pop reapply the last stash



Additional Resources 

Tags: Linux, Github, Git

About the Author


R Donato

Rick Donato is the Founder and Chief Editor of He currently works as a Principal Network Security Engineer and has a keen interest in automation and the cloud.

You can find Rick on Twitter @f3lix001