QUICK GUIDE TO GIT WORKFLOW FOR BEGINNERS — Part 1 (config, init, add, commit, status & log)

Salifu Sani Rich
Sep 8, 2018 · 7 min read
Understanding the basics of git

This article is to give a quick introduction and guide you to using git as a beginner. It is assumed that you have git installed on your system (Linux, Mac or Windows). This post does not cover the technical details on downloading and installing git, visit here(https://git-scm.com/) to learn all that is involved in downloading and installing git.

What is GIT — a quick overview?

GIT is a free and open source distributed Version Control System (VCS) or Source Control Management (SCM) system. It allows developers to store source code, files and other documents or artifacts so that different versions of their document exist as created at different point in time.

Basic Concepts of GIT?

Before discussing git commands which is the major tool to work with GIT, we need to understand some basic concept of GIT workflow.

Projects in git can be categorized into 3:

Section of a GIT project
  1. WORKING DIRECTORY — this is the section where you work on your files. This is the root directory of your GIT project.
  2. STAGING/INDEX AREA — this is the section where changes in the projects or artifacts are marked for commit. It is sometimes referred to as INDEX. This is where all related changes are built up.
  3. COMMIT AREA — this is the most important area of GIT. This is where files are stored in git. The .git directory is where GIT stores the committed artifacts and meta-data for the project. This is where all files artifacts are stacked safely in GIT database.

As you work with GIT, your files or artifacts can be in 4 different states:

Major States in a GIT Project
  1. UN-STAGED — means that the file or artifact has not been added to the get staging area
  2. MODIFIED — means that you have changed an artifact but have not committed it to the git database yet. In this state the user must have added the file to git and is currently making changes.
  3. STAGED — means that you have marked the un-staged or modified file in its current version to go into your next commit snapshot(i.e. the state of an artifact at a particular time). Here the developer adds the modified artifact to the GIT index or Staged area.
  4. COMMITTED — means that the artifact is safely stored in your local GIT database. This is achieved by the GIT -commit command. Here, the artifact gets safely stored on the GIT database.

GIT CONFIGURATION

Starting to work with GIT, you may need to configure your GIT profile. This entails setting basic info about the git user such as username and user email, which can be done by setting a global or local configuration.

# global configuration -- this line is just a comment
git config --global user.name [Your Name]
git config --global user.email [youraddress@email.com]

The above commands creates a configuration file .gitconfig in the .git directory in the home or root of your machine and sets the default username and email for GIT on your machine.

To configure a subsequent GIT project to use a specific username or email which overrides the global configuration, make sure you cd (Change Directory) into the root folder of your working (project) directory and run the following command.

# local configuration — this line is just a comment
git config --local user.name [Your Name]
git config --local user.email [youraddress@email.com]

OR

# local configuration — this line is just a comment
git config user.name ["Your Name"]
git config user.email ["youraddress@email.com"]

You can check for existing configuration using these commands:

# global configuration — this line is just a comment
git config --global --list
# local configuration, in the root — this line is just a comment
git config --local --list

INITIALIZING A GIT REPOSITORY (REPO)

git init

To setting up a GIT repo from scratch, cd (Change Directory) into the root folder of your working (project) directory, and run this command:

#initializes a git repo
git init

The above command only initializes the project for GIT, hence creating the .git directory in your working project directory.

git add

To add your files and artifacts to the staging area you run this command:

#to add all files
git add *
#to add all files
git add --all
#to add a particular file or artifact without the square brackets
git add [filename]

Files in the staged area are only tracked (indexed) by git, they are not yet saved as snapshot on git. This command only tells git to keep watch of the files in the working directory.

git commit

To safely store your artifacts or files at particular time in your local GIT database, you run this command

# commits artifacts to git
git commit -m ['a commit message']

The option [-m] denotes a commit message is to be added.


git status

You can check the status of files in git working directory. To do this run this command.

# checks the status of your artifacts on git
git status

This command tells the state (untracked/unstaged, staged/added, modified, committed or removed/deleted) of the artifacts as discussed in the earlier section.

git status (photo credit: stackoverflow)

The git status command can be used with options: --long (which is the default when no option is used) or -s (which means short). The option --long returns the status in a detailed form. WHILE the option -s returns the status in a summary.

Using git status -s you should take note of specific notations (??, M, A, D) and colors of text in the result.

When ?? appears in color red in front of a file, it means the file is un-staged and are actually not tracked by git.

When A appears in color green, it means the file or artifact has been added to the staging area.

When M appears in color red, it means the file or artifact has been modified and is not updated on the git staging area (meaning that the git add [filename] has not ran after the file was updated). If the M appears in color green, it means the modified file on git has been added to the git staging git add and is not updated on the git staging area.

When D appears in red, it means the file or artifact has deleted and no longer on git.

Files that have been committed to git will not show up when you run the git status command. You are likely going to see a message the working directory is clean.


git log

The git log command is used to check the commit history of the git project. This command by default will show the git assigned commit id, author name and email (where applicable), and the date and time stamp of the commits made on the project. By running the git log command without any option will return the commit history in a detailed form.

To have the commit history shown in summary (with short detail). You can run the git log --oneline command. This will return the history of all commits with a git assigned commit id and the user provided commit message per line.

You can filter the git commit history you want to view by specifying the commit id of the commit you want to start from and that of which you want to end. [you can copy the commit id from the list return after running the git log --oneline command]

## General Syntax <since> and <until> are specified commit id
git log <since>..<until>
## This is what it used look like, I encourage the use of --oneline ## option
git log 895f6ea..f55e12d --oneline

You also filter the commit history to view by specifying the last number of commits you want to view.

## This command will return the last 2 commits
git log -n 2 --oneline

PS: GIT is not GITHub, so do not mix up both tools/technologies. In a subsequent post I will talk about GIT and GITHUB.

In other to make this more exciting with less read time, this article will be sectioned into several release posts. Links to the other parts will be provided on each post. Stay tuned, I will be updating the links to other related articles in this post.

If you have been enlightened, please give claps and follow me on medium Salifu Sani Rich and on twitter @sarscode.

NEXT: QUICK GUIDE GIT WORKFLOW FOR BEGINNERS — Part 2 (branching)

(REVIEWED By Ada Nduka Oyom & Tunde Yusuf)

Salifu Sani Rich

Written by

FrontEnd Web Developer | Advocate for Fundamentals and Simplicity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade