6  Version control system: Git

6.1 Introduction

Version control system, aka VCS or source control, is a software to maintain history of changes to a project, in context of folders, files and content of files.

In context of programming projects, a vcs is used for code and config files needed to reproduce a project. All the information is stored in a compressed directory.

Major use cases for a VCS are

  • Track: history of changes with related information
  • Rollback: in case needed
  • Collaborate: changes to different parts can be made in parallel and merged

Some popular VCS are

  • Git
  • Apache Subversion
  • Mercurial

This book covers Git as it is most popular. Getting familiar with vcs through cli should help with the following

  • Basic usage of vcs without branching
  • Introduce how basic things work without gui
  • Understand and use gui tools better
  • There are few complex tasks that can only be done from cli, when needed

There are gui tools in most editors so most of the interaction is through them.

To learn Git in-depth, refer to Pro Git. It is recommended to go through first 2 sections of the book to understand the basic fundamentals and operations.

A VCS is complemented by an online (cloud) service for hosting repositories. There are several different online providers with their own set of functionalities.

  • SourceForge: supports multiple VCS
  • GitHub: supports git only
  • GitLab: supports git only
  • BitBucket: supports multiple VCS

The key ideas behind working of Git are summarized in the first section of Pro Git, which is highly recommended.

6.2 Git CLI

Below is a summary of common Git cli commands based on use cases.

6.2.1 Config

  • Check Git config
git config --list --show-origin
  • Setup username and email for Git
    • mandatory after fresh installation
    • these are stamped for each commit to track who made changes
git config --global user.name "name"
git config --global user.email "email@example.com"

6.2.2 Initialize repository

git init

The above command is run from the folder to be version controlled.

When a repository is initialized

  • .git folder is created which is the repository
  • All files and sub-folders are tracked by default
    • Add .gitignore to ignore tracking certain files and folders

6.2.3 Check status and logs

git status --short
git log --oneline

6.2.4 Add changes to staging area

git add <file or dir path>

6.2.5 Commit

6.2.5.1 Using text editor

git commit
  • Opens the default text editor to enter message and commit the staged changes
    • in this case first line will be the short message
    • remaining typed lines will form the detailed message

6.2.5.2 Quick commit

git commit -m "message text"

6.2.5.3 Stage all changes and commit

git commit -am "message text"
  • Stage all changes
  • Commit the staged changes with short message
  • In the -a option no need to add files separately

6.2.6 Check differences

6.2.6.1 Since last commit

git diff

6.2.7 Undo changes from last commit

git restore <file name>
  • Discard changes to file in working directory