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:
Rollback Scenario:
| Version | index.html Lines |
|---|---|
| V1 | 10 |
| V2 | 20 |
| V3 | 30 |
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:
-
Working Directory – Where code is written
-
Staging Area – Where changes are tracked
-
Repository – Where tracked changes are committed
Installing Git on Windows
Download Link:
Installation Steps:
-
Open the installer
-
Click Next until installation completes
-
Open Git Bash
Initializing a Git Repository
Initializes an empty Git repository by creating a .git directory.
Without running
git init, Git commands will not work.
Basic Git Commands (With Explanation)
Creates a new file.
Displays the current state of files.
Adds the file to the staging area.
Git User Configuration (Mandatory)
Sets the global Git username.
Sets the global Git email.
Commits will not work without user configuration.
Committing Changes
Commits the staged file to the repository.
Displays commit history.
Shows commits in single-line format.
Shows the last two commits.
Git Workflow:
Using GitHub
Each developer works locally, and GitHub is used to combine all developer code into a single project.
Pushing Code to GitHub
Connects the local repository to GitHub.
Pushes local commits to GitHub.
Git Branches
A branch is an independent line of development.
Common Branches:
-
master / main
-
release
-
hot-fix
Branch Commands:
Lists all branches.
Creates a new branch.
Switches to another branch.
Creates and switches to a new branch.
Renames a branch.
Deletes a branch (cannot delete the current branch).
Real-Time Branch Workflow
Each developer works on a separate branch and pushes changes to GitHub.
(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:
Merges the movies branch into master.
Merge vs Rebase
| Merge | Rebase |
|---|---|
| Preserves commit history | Rewrites commit history |
| Suitable for public repositories | Suitable for private repositories |
| Shows merge commits | Creates linear history |
Git Revert
Safely undoes a commit without deleting history.
Git Clone and Fork
Downloads the repository along with all branches.
Fork:
-
Copies a repository from one GitHub account to another
-
Public repository required
Branch Protection Rules (GitHub)
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
Fetches and merges changes from GitHub.
Fetches changes without merging.
Git Cherry-Pick
Used to apply specific commits to another branch.
Applies a selected commit.
.gitignore
Used to prevent files from being tracked.
Add filenames (e.g., credentials, logs) to ignore.
Git Stash
Used to temporarily save tracked but uncommitted changes.
Temporarily hides changes.
Restores stashed changes.
Lists all stashes.
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
Post a Comment