Git Basics to Advanced

Rishi BakshiRishi Bakshi
6 min read

What is Git?

Git is a Distributed version control system.

What is Version control system?

Version control refers to the process of tracking and managing changes to files over time. Version control systems (VCS) enable developers to collaborate effectively, revert to previous versions, and manage different versions of a project.

we can categorize version control system into 3 parts:
1. Local version control: It simply means that as a developer or a creator if I write a software or a book and save it into my own machine with different versions. If you are working as part of a team, it becomes difficult to share updates, track contributions from others, or maintain a synchronized project history.

  1. Centralized version control system(CVCS)

    To address these collaboration challenges, centralized version control systems were introduced. In a CVCS, all versions of the project are stored in a central server. Team members can access, update, and commit changes to this central repository. This setup allows better coordination and version tracking among multiple contributors. However, it also introduces a dependency on the central server, if it goes down or gets corrupted, the entire history of the project could be at risk.

  2. Distributed Version Control System (DVCS)

    A Distributed Version Control System (DVCS) is an advanced model of version control that overcomes the limitations of centralized systems. In a DVCS, such as Git, every developer has a complete copy of the project repository, including its full history stored locally on their own machine.

    History of Git

    Git was created in 2005 by Linus Torvalds, the creator of Linux, after the Linux kernel community lost access to BitKeeper, a proprietary version control system they had been using for years. BitKeeper had offered free use to open-source projects, but this was revoked after a dispute. This triggered the need for a robust, distributed, and open-source alternative. Linus developed Git to be fast, reliable, and support distributed workflows. Within weeks, Git replaced BitKeeper for Linux development and quickly gained popularity among developers worldwide, becoming the most widely used version control system in the software industry today.

    Git Setup

    • git config --global --list – Displays all global Git configuration settings.

    • git config --global user.name "username" – Sets your Git username globally for all repositories.

    • git config --global user.email "user@example.com" – Sets your Git email globally for all repositories.

Git Commands

  • git init – Initializes a new local Git repository in your project directory.

  • git status – Shows the current status of changes (staged, unstaged, untracked files) in your repository.

  • git init -b main – Initializes a new Git repository and sets the default branch name to main.

  • git add <filename> – Stages the specified file for the next commit.

  • git add . – Stages all modified and new files in the current directory for commit.

  • git log – Displays a history of all commits made in the repository.

  • git commit -m "commit message" – Records the staged changes with a descriptive commit message.

  • git commit -a -m "commit message" – Stages and commits all tracked, modified files in one step with a message.

  • git diff – Shows the differences between working directory changes and the last commit (unstaged changes).

  • git diff --staged – Shows the differences between staged changes and the last commit.

  • git rm --cached <filename> – Removes a file from the staging area (index) without deleting it from the working directory.

What is remote repository?

A remote repository is a version of project hosted on the internet or a network, typically on platforms like GitHub, GitLab, or Bitbucket. It allows multiple developers to collaborate by pushing and pulling changes to and from a centralized location, keeping all team members’ code in sync.

Git Commands and Steps to Add Your Project to GitHub

  1. git init
    Initializes a local Git repository in your project folder.

  2. git branch -M main
    Renames the default branch to main (recommended for consistency with GitHub).

  3. ssh-keygen -o
    Generates a new SSH key pair for secure communication with GitHub (usually stored in ~/.ssh/id_rsa).


How to Add a Local Repository to GitHub

Step 1: Create a new repository on GitHub (without initializing with README).

Step 2: Link your local repo to GitHub:

git remote add origin

Replace <repository-link> with the SSH or HTTPS link from your GitHub repo.

Step 3: Push your code to GitHub:

git push -u origin main

This pushes your local main branch to the origin remote and sets it as the default upstream.

git remote -v
Lists the URLs of the connected remote repositories (origin in this case) for fetch and push.

Tagging

What is Tagging?

Tagging is a feature in Git used to mark specific points in a repository’s history, typically to indicate releases ( v1.0, v2.0 etc.). Tags are like bookmarks that help identify important commits.
git tag- Lists all tags in the current repository.

There are two types of tagging:

  1. Annotated tagging

  2. Lightweight taging

git tag -a v1.0 -m “First release“- Creates an annotated tag named v1.0 with the message "First release". This is used to mark a specific commit with version information and metadata.

git show v1.0 - Displays the details of the tag v1.0, including the commit message and changes if it’s an annotated tag. For lightweight tags, it simply shows the associated commit.

git push origin v1.0 - push the tag

Cloning, Branching, and Merging in Git

Cloning a project in Git means creating a local copy of a remote repository using the git clone command. After cloning, developers often use branches to work on new features or fixes without affecting the main codebase. You can create and switch to new branches using git checkout -b or git switch -c, and return to existing branches with git switch. Viewing all branches, including remote ones, is done with git branch --all. After finishing a feature, the branch can be merged back into main using git merge, ensuring your changes are integrated. Finally, git push is used to sync your local changes with the remote repository.

Commands and Their Details

  • git clone <repository_link>
    Clones the remote repository to your local machine.

  • git checkout -b feature1
    Creates and switches to a new branch named feature1.

  • git branch
    Lists all local branches.

  • git switch main
    Switches to the main branch.

  • git switch -c feature2
    Creates and switches to a new branch named feature2.

  • git branch --all
    Lists all branches, both local and remote.

  • git switch -
    Switches back to the previously checked-out branch.

  • git branch -d feature2
    Deletes the local branch feature2 (only if it's fully merged).

  • git push origin feature1
    Pushes the feature1 branch to the remote repository.


Merging a Branch (Workflow)

  • git switch main
    Switches to the main branch to prepare for merging.

  • git pull origin main
    Updates the local main with the latest changes from the remote.

  • git merge feature1
    Merges the feature1 branch into main.

  • git push origin main
    Pushes the updated main branch (after merging) to the remote repository.

1
Subscribe to my newsletter

Read articles from Rishi Bakshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rishi Bakshi
Rishi Bakshi