Git – The Basic Workflow

Git is a version control system (VCS) that allows to keep track of the changes made to a project over time.

Git records the changes you make to a project, stores those changes and then allows you to reference them as needed.

From the following article you will learn what does the basic Git workflow consist of and the simple Git commands for everyday use.

The Basic Git Workflow

The usual Git workflow is the following:

  1. After creating a Git Repository, your work is done in the Working Directory
  2. Once your work reaches a significant point (for example fixing of some bug or the end of the working day), you add your changes to the Staging Area
  3. Once the stagging area contains everything you intend to commit, you save changes to the Git Repository

A Git project can be thought of as having 3 parts:

Part Description
Working Directory Where you do all the work: create, edit, delete and organize files
Staging Area Where you list the changes you made in the working directory
Git Repository Where Git permanently stores those changes as different versions of the project

Create a New Git Repository

Open a directory with an existing project or create a new directory and run the following command inside it to create a new Git repository:

$ git init

This command creates the .git sub-folder in the project root, which contains all of the necessary metadata needed by Git.

Check the Status of the Git Repository

Check the current state of the Git repository:

$ git status

This command is the main tool that helps to determine which files are in which state (which are added to the stagging are and which are untracked).

What does those untracked files mean? Untracked – means that Git sees the file but has not started tracking changes yet. In order for Git to start tracking a file, it has to be added to the staging area.

Track Files in Git

Run the below command to tell Git to start tracking a file (add to the staging area):

$ git add <filename>

To start tracking all files, run:

$ git add .

Show Differences in Git

Since the files are tracked, we can check the differences between the working directory and the staging are.

Cool Tip: Did something wrong? Want to undo everything? You can easily revert all changes back to the last commit! Read more →

To check the differences, run:

$ git diff

To show the changes that were made to a particular file, run:

$ git diff <filename>

Save Changes in Git

So what exactly does commit do? Commit permanently stores changes from the staging area inside the repository.

A commit is the last step in the Git workflow.

Commit all changes from the stagging area:

$ git commit -m "Brief description"

Commit a single file only:

$ git commit -m "Brief description" <filename>

Good practice for Git commit messages:

  • Must be in quotation marks
  • Written in the present tense
  • Should be brief (50 characters or less)

View the Commit History

List the commits made in the Git repository:

$ git log

Cool Tip: Want to look back to see what has happened with one particular file? Find out the commit history of this file, history of diffs and up to the history of renames! Read more →

Basic Git Commands

Summarizing all the above, here are the basic Git commands for everyday use:

Command Description
git init Creates a new Git repository
git status Inspects the contents of the working directory and staging area
git add Adds files from the working directory to the staging area
git diff Shows the difference between the working directory and the staging area
git commit Permanently stores file changes from the staging area in the repository
git log Shows a list of all previous commits