Mastering Git: From Basics to Advanced

A Comprehensive Guide to Git Commands, Workflows, and Best Practices

Komal Vardhan Lolugu
5 min readJul 11, 2023

In the realm of software development, effective version control is crucial for managing projects, collaborating with teams, and maintaining a clean codebase. Git, developed by Linus Torvalds, has emerged as the de facto standard for version control due to its speed, flexibility, and extensive feature set. In this blog , we will embark on a journey from the fundamentals of Git to advanced techniques, covering essential commands, workflows, and best practices.

Git Logo
Git — Distributed Version Control System

What is Git ?

Git is a distributed version control system that tracks changes in files and directories over time. It allows multiple developers to work on a project simultaneously, preserving a complete history of all modifications. Git provides a local repository on each developer’s machine, which can be synchronized with a central repository or other developers’ repositories.

Git Workflow

Set up Git :

Before diving into Git, you need to set it up on your machine. Download and install Git from the official website (https://git-scm.com/downloads), and follow the installation instructions for your operating system.

Git Basics :

Basic Fundamental Concepts of Git before using it as a Version Control

  • Repository: A repository is a collection of files and directories that make up a project, along with their complete history.
  • Commit: A commit represents a snapshot of changes made to the repository at a particular point in time. It contains information about what changes were made and who made them.
  • Branch: A branch is a parallel version of a repository that allows developers to work on different features or bug fixes independently.
  • Remote: A remote is a copy of a repository that resides on a different machine or server. It enables collaboration and synchronization between developers.

Common Git Commands :
Let’s explore some commonly used Git commands and their practical examples

git init : Initializes a new Git repository in the current directory.

$ git init

git clone : Creates a local copy of a remote repository.

$ git clone <repository_url>

git add : Adds files or changes to the staging area, preparing them for the next commit.

$ git add <file_name>

git commit : Creates a new commit with the staged changes.

$ git commit -m "Commit message"

git status : Displays the current status of the repository, including any modified, staged, or untracked files.

$ git status

git branch : Lists all existing branches or creates a new branch.

List all Branches 
$ git branch

Create a new Branch
$ git branch <branch_name>

git checkout : Switches to a different branch or restores files from a specific commit.

Switch to different branch
$ git checkout <branch_name>

Restore files from specific commit
$ git checkout <commit_hash> -- <file_name>

git pull : Fetches the latest changes from a remote repository and merges them into the current branch.

$ git pull origin <branch_name>

git push : Uploads local commits to a remote repository.

$ git push origin <branch_name>

git log : Displays a chronological list of commits in the repository.

$ git log

Git Workflows :

  • Centralized Workflow : In this workflow, there is a central repository, and all developers clone from and push to that repository. It’s a simple workflow suitable for small teams or individual developers.
  • Feature Branch Workflow : Each new feature or bug fix is developed on a dedicated branch. Developers can work independently without interfering with the main branch. Changes are merged back into the main branch when ready.
  • Gitflow Workflow : This workflow uses two main branches: master for stable releases and develop for ongoing development. Feature branches are created off the develop branch and merged back into it. Release and hotfix branches are used for managing stable releases.

Git Hosting Platforms :

Several platforms provide remote hosting for Git repositories, making collaboration and code sharing easier:

  • GitHub: (https://github.com) — Offers public and private repositories with powerful collaboration features. (Recommended)
  • GitLab: (https://gitlab.com) — Provides both cloud-based and self-hosted options, offering integrated CI/CD and project management tools.
  • Bitbucket: (https://bitbucket.org) — Offers free private repositories for small teams and integrates well with other Atlassian products.
  • Azure DevOps: (https://azure.microsoft.com/en-us/services/devops) — Provides a suite of tools for planning, developing, testing, and deploying applications, including Git repository hosting. (Recommended)

Bonus Content

Additional Git Commands :

git diff : Shows the differences between the working directory and the staging area or between two commits.

$ git diff
$ git diff --staged
$ git diff <commit_hash1> <commit_hash2>

git merge : Combines changes from one branch into another branch.

$ git merge <branch_name>

git remote : Manages remote repositories linked to the local repository.

$ git remote add origin <repository_url>
$ git remote -v

git fetch : Retrieves the latest changes from a remote repository without merging them into the current branch.

$ git fetch origin

git stash : Temporarily saves changes that are not ready to be committed, allowing you to switch branches.

$ git stash
$ git stash list
$ git stash apply

git reset : Upstages changes or moves the current branch to a specific commit, discarding subsequent commits.

$ git reset <file_name>
$ git reset --hard <commit_hash>

Mastering Git is an essential skill for modern software developers, enabling efficient collaboration, code management, and project success. In this comprehensive guide, we covered Git from its basics to advanced techniques, including essential commands, workflows, and best practices. With a solid understanding of Git and consistent practice, you can streamline your development process, improve productivity, and ensure the integrity of your codebase. Embrace the power of Git and embark on a journey of version control mastery.

Remember, Git is not just a tool but a mindset and a foundation for effective collaboration. Continuously explore and experiment with Git’s capabilities, stay up to date with the latest features and best practices, and leverage the wealth of resources available to deepen your understanding. Happy version controlling!

Resources :

Download Git

GitHub

GitLab

Bitbucket

Azure DevOps

--

--

Komal Vardhan Lolugu
Komal Vardhan Lolugu

Written by Komal Vardhan Lolugu

Hey there! I'm Komal Vardhan Lolugu, a passionate Software Engineer. Working as a Fullstack Developer , where I've had the opportunity dive into Development..

No responses yet