computer science,

Version Control Using Git

Jun 25, 2020 · 7 mins read
Version Control Using Git
Share this

Version control is the most important system for managing code bases. It is the system for recording different versions of files, usually source code files, changing over time. It allows users to track changes in code over time and helps collaborative code development. Tracking and unique identification of every change posted to a code enables users to monitor progress, mark particular states of the code and completely or selectively revert to older versions if needed.

Version control system (VCS) is a very handy software for code organization, multiple developer collaboration and code sharing. VCS allows parallel code development/testing/deployment for several programmers spread across multiple large teams. The most common version control tools used are git, cvs, mercurial and svn.

Git is one of the best and most widely used distributed version control system to handle big projects. It was developed by Linus Torvalds during the development of the Linux kernel. It is a free and open-source tool shipped natively with most of the linux distros. While locally installed git is sufficient for individual purpose, the true essence of VCS lies in remote collaboration.

Version Control Tree Structure
Version Control Tree Structure


Git is a decentralized version control system where all users can have their individual versions of the code. Also, git allows offline code development while network connectivity and online VCS hosts are needed for remote collaboration. Online platforms like Github, Gitlab and Bitbucket are the most widely used ones and serve for code sharing within between the VCS framework. Git keeps variations in code over time by recording and tracking only the difference in code and not maintaining multiple code copies. All these differences are sorted and stored, as per different branches of the git tree, in a hidden .git folder inside the porject source code.

Git Concepts & Terminology

Following are the basic concepts and git terminology for access, modification and development of the project source code.

  • Repository - Project source code folder
  • Branch - Different version of the same source code that different users can use, add and makes changes to, independently. The master branch is usually the main branch that every other individual feature, bug-fix or release branch merges to.
  • Fork/Clone - Create a personal copy of the project source code, essentially from an online hosting platform
  • Commit - Add the new changes to the source code, to the current version aka branch and log it with a message and a timestamp
  • Remote - Online repository storage. It works for collaboration with multiple developers and backup storage.
  • .gitignore - Text file with names or regex rules for files and folders that should not be considered for tracking via VCS. These could be compiled files, executables, binaries or keys that don’t need to be put on online platforms or shared. Standard rules and common templates can be fetched online here at Github Online Repository
  • - A markdown file that usually carries description of the project, performance results, instructions for usage, development and installation of the project.

Basic Concept of Git VCS
Basic Concept of Git VCS

Common Git Commands

Setup and remote connection

  • Initialize the git control system inside the project source code folder  
    git init

  • Stage the new addtional changes completely or partially to commit to the branch
    git add .        // Stage all changes
    git add --all  // Stage changes including deleted files
    git add <file_name/folder_name>  // Stage all changes

  • Finally add staged changes to the current branch
    git commit -m <message to notify commit log>

  • Push the new commits on a local branch to remote repository
    git push origin <branch>

  • Fetch or obtain the updated codes in the different branches from the remote repositories
    git fetch

  • Merge the changes from a different branch to the current branch
    git merge <branch>

  • Pull and merge the changes made to the current branch on the remote repository from a differnt user. It is a fetch + merge
    git pull origin <branch>

Pull before push is a standard protocol for collaboration. Changes to a local branch can’t be merged to the same online unless the updates to the remote(made probably by a different collaborator) are absorbed in the local branch.

  • Clone a remote repository to the local machine
    git clone <remote_git_url>

  • Reset the all changes and get to master branch
    git reset --hard
    git reset --hard <commit_id>
    git reset   // Undo the staging command for the currently uncommitted changes


  • Check the difference and edits in new uncommitted and unstaged files
    git diff

  • Get the list of branches on the local machine
    git branch

  • Get the list of tags available
    git tag

  • See history for commits, messages and authors
    git log

  • List the changes, timestamps and authors for the file
    git blame <file_name>

  • Show the changes made in a particular commit
    git show <commit_id>

Branch Commands

  • Switch to the specified branch
    git checkout <branch_name>

  • Create a new branch
    git checkout -b <new_branch_name> <base_branch_name>

  • Delete a branch
    git branch -d <branch_name>

  • Switch to a particular commit ID of the code if it exists on the branch
    git checkout <commit_id>

Discard or Archive uncommitted changes

  • Restore a particular file to the last committed version. All uncommited local changes are lost
    git checkout <file_name>

  • Remove all new uncommitted edits and restore the project to the last commit
    git stash

  • Archive the current uncommitted changes to stash
    git stash save "<message_to_save_edit>"

  • Obtain the list of stashed or archived changes
    git stash list

  • Apply last stashed set of changes to edits
    git stash apply

  • Apply a particular set of stashed changes to the current branch
    git stash apply stash@{<stash_number>} // Stash number from the list

User Credentials’ Configuration

  • Add username for remote git service account
    git config --global "<github/bitbucket user_name>"

  • Add email ID for remote git service account
    git config --global "<github/bitbucket user_mail_id>"

  • Connect a local repository to remote git service URL
    git remote add origin "<github/bitbucket repository URL>"

  • Set the origin/remote git service URL to current local repository
    git set-url remote origin "<github/bitbucket repository URL>"

Benefits Of Git

  • Automated code release, build, testing and deployment schedules triggered on commits, tags or merges can be run from third party softwares like Jenkins to enhance efficiency during code development.
  • Streamlined code development with discete feature/bug-fixing/refactor branches
  • Multiple backups of codes in local or remote storage
  • Pull requests from feature branches usually incorporate code reviews and testing to maintain code quality and functionality
  • Hierarchy in permissions for code merging or modification to particular branches or creating tags helps avoid mistakes from newer developers and maintain code quality

Git is one of the most sought after technical skill in the software development industry. It has made collaboration more systematic and synchronized. While this list of git commands is only the ones used most commonly, man git can be used to learn more detailed description of git commands.