Git Explained: A Complete Practical Guide for Beginners

 Git – Complete Guide with Practical Commands

Introduction

Git stands for Global Information Tracker.
It is a Distributed Version Control System (DVCS) used to track source code changes, manage multiple versions of files, and collaborate efficiently in software development.


Version Control System (VCS) & Source Code Management (SCM)

  • VCS – Version Control System

  • SCM – Source Code Management

A VCS helps store and manage different versions of code separately so that developers can track changes and rollback when required.

Example:

V1 : 1 Service : 100 Lines Repo-1 V2 : 2 Services : 200 Lines Repo-2 V3 : 3 Services : 300 Lines Repo-3

Rollback Scenario:

V3 → V2
Versionindex.html Lines
V110
V220
V330

What is Git?

Git is used to:

  • Track files and changes

  • Maintain multiple versions of the same file

  • Work across platforms (Windows, Linux, macOS)

  • Handle large projects efficiently

Key Points:

  • Free and open-source

  • 3rd generation VCS

  • Written in C

  • Released in 2005


CVCS vs DVCS

Centralized Version Control System (CVCS)

  • Code stored in a single central repository

  • Example: SVN

Distributed Version Control System (DVCS)

  • Code stored in multiple repositories

  • Example: Git


Git Architecture (Stages)

Git works with three stages:

  1. Working Directory – Where code is written

  2. Staging Area – Where changes are tracked

  3. Repository – Where tracked changes are committed


Installing Git on Windows

Download Link:

https://github.com/git-for-windows/git/releases/download/v2.52.0.windows.1/Git-2.52.0-64-bit.exe

Installation Steps:

  • Open the installer

  • Click Next until installation completes

  • Open Git Bash


Initializing a Git Repository

git init

Initializes an empty Git repository by creating a .git directory.

Without running git init, Git commands will not work.


Basic Git Commands (With Explanation)

touch index.html

Creates a new file.

git status

Displays the current state of files.

git add index.html

Adds the file to the staging area.


Git User Configuration (Mandatory)

git config --global user.name "raham"

Sets the global Git username.

git config --global user.email "raham@gmail.com"

Sets the global Git email.

Commits will not work without user configuration.


Committing Changes

git commit -m "commit-1" index.html

Commits the staged file to the repository.

git log

Displays commit history.

git log --oneline

Shows commits in single-line format.

git log --oneline -2

Shows the last two commits.

Git Workflow:

Create → Track → Commit touch → git add → git commit

Using GitHub

Each developer works locally, and GitHub is used to combine all developer code into a single project.


Pushing Code to GitHub

git remote add origin https://github.com/devopsbyraham/paytm.git

Connects the local repository to GitHub.

git push origin master

Pushes local commits to GitHub.


Git Branches

A branch is an independent line of development.

Common Branches:

  • master / main

  • release

  • hot-fix

Branch Commands:

git branch

Lists all branches.

git branch movies

Creates a new branch.

git checkout movies

Switches to another branch.

git checkout -b recharge

Creates and switches to a new branch.

git branch -m old new

Renames a branch.

git branch -D movies

Deletes a branch (cannot delete the current branch).


Real-Time Branch Workflow

Each developer works on a separate branch and pushes changes to GitHub.

git branch movies git checkout movies touch movies{1..5} git add movies* git commit -m "movies commits" git push origin movies

(Similar steps apply for other branches such as dth, train, and recharge.)


Merging Branches

Using GitHub (Recommended)

  • Create Pull Request

  • Review changes

  • Merge and confirm

Using Git:

git checkout master git merge movies

Merges the movies branch into master.


Merge vs Rebase

MergeRebase
Preserves commit historyRewrites commit history
Suitable for public repositoriesSuitable for private repositories
Shows merge commitsCreates linear history

Git Revert

git revert commit_id

Safely undoes a commit without deleting history.


Git Clone and Fork

git clone https://github.com/anitalluri00/paytm.git

Downloads the repository along with all branches.

Fork:

  • Copies a repository from one GitHub account to another

  • Public repository required


Branch Protection Rules (GitHub)

SettingsBranchesAdd Rule Enforcement Status: Active Target: All branches

Prevents accidental deletion or force-push.


Merge Conflicts

Merge conflicts occur when the same file is modified in multiple branches.
They must be resolved manually.


Git Pull vs Git Fetch

git pull origin master

Fetches and merges changes from GitHub.

git fetch

Fetches changes without merging.


Git Cherry-Pick

Used to apply specific commits to another branch.

git cherry-pick commit_id

Applies a selected commit.


.gitignore

Used to prevent files from being tracked.

.gitignore

Add filenames (e.g., credentials, logs) to ignore.


Git Stash

Used to temporarily save tracked but uncommitted changes.

git stash

Temporarily hides changes.

git stash apply

Restores stashed changes.

git stash list

Lists all stashes.

git stash clear

Deletes all stashes.


Conclusion

Git is a core skill for developers and DevOps engineers.
Understanding branching, merging, pull requests, and collaboration workflows is essential for real-world projects.

Comments

Popular posts from this blog

Managing Amazon EBS Volumes and Snapshots Across Regions

Git for Beginners: Complete Guide from Installation to First Push on GitHub

AWS - Amazon Web Services