Git: A Comprehensive Guide to Distributed Version Control
Git is a distributed version control system (DVCS) that allows you to manage and track changes in your project's files over time. Here's a refined explanation:
What is Git?
Git is a distributed version control system (DVCS).
It captures snapshots of your project's files over time.
Every developer's machine can have a complete copy of the repository and its history, making it distributed.
You can revert to previous versions, create isolated branches for new features, and merge changes safely.
Think of Git as a time machine for your code — it records every change and who made it.
Why is Git used?
Track history: Monitor who changed what and when.
Experiment safely: Create branches to test features without affecting the main codebase.
Collaboration: Work concurrently with teammates and merge changes seamlessly.
Backup: With remotes like GitHub or GitLab, your code has an off-site backup.
Integrations: Compatible with CI/CD, code review tools, and many editors.
Git basics & core terminologies (simple explanations):
Repository (repo): The project folder tracked by Git, containing your files and a hidden
.gitfolder with history.Working directory (working tree): The files you see and edit on disk.
Staging area (index): A place where you prepare changes that will go into the next commit.
Commit: A recorded snapshot of the staged changes, with an author, timestamp, message, and a unique ID (SHA).
Branch: A movable pointer to a commit, used for parallel lines of development.
mainormasterare common default branches.HEAD: The current branch reference, pointing to the latest commit on the branch you have checked out.
Remote: A version of the repository hosted on another server, such as GitHub (origin).
Merge: Apply changes from one branch into another.
Checkout: Switch between branches or restore files from history.
Common Git commands (with beginner-friendly examples):
Start a new repository:
# Create a folder and make it a Git repository mkdir my-project cd my-project git initgit initcreates a.gitdirectory and initializes the folder as a repository.Check repository status:
git statusDisplays modified files, staged files, and untracked files.
Stage changes:
git add file.txt # Stage a single file git add . # Stage all changes in the current folderStaging allows you to prepare exactly which changes will be included in the next commit.
Commit staged changes:
git commit -m "Meaningful message: what and why"Commits the staged snapshot to the local history.
View history:
git log # Verbose history git log --oneline # Compact history (one line per commit)Inspecting changes:
git diff # See unstaged changes git diff --staged # See staged changes