Part 1 - Why Every Developer Needs Version Control
Have you ever saved files named index_final.html, index_final_v2.html, and index_FINAL_REALLY_FINAL.html? Git eliminates this chaos forever.
Git is a distributed version control system (VCS) that tracks changes in your source code over time. It allows you to take snapshots of your project, compare changes across dates, experiment fearlessly on isolated branches, and collaborate seamlessly with engineers across the world.
Part 2 - The Three Stages of Git Architecture
Every file in a Git project lives in one of three logical areas:
- Working Directory: Your actual files on disk that you are editing right now.
- Staging Area (Index): A staging staging room where you pick and choose exactly which file changes should be included in the next commit.
- Git Repository (.git folder): The permanent history of snapshots (commits) stored safely in the database.
# Check current status of your workspace
git status
Part 3 - Essential Daily Git Commands
These core commands form 90% of your daily version control workflow:
# 1. Initialize a new local repository
git init
# 2. Stage files for commit
git add index.html # Stage single file
git add style.css script.js
git add . # Stage all modified and new files
# 3. Commit staged changes with a clear message
git commit -m "feat: implement responsive navigation bar"
# 4. View commit history
git log --oneline --graph --decorate
# 5. Inspect uncommitted line-by-line differences
git diff
Part 4 - Branching & Merging: Fearless Experimentation
Branches allow you to build new features, fix bugs, or experiment without ever endangering your production code on the main branch.
# 1. Create and switch to a new feature branch
git switch -c feature/dark-mode
# (Older syntax: git checkout -b feature/dark-mode)
# 2. Work on your feature, stage, and commit
git add .
git commit -m "feat: add dark mode color tokens and toggle"
# 3. Switch back to main
git switch main
# 4. Merge the completed feature into main
git merge feature/dark-mode
# 5. Safely delete the merged feature branch
git branch -d feature/dark-mode
Part 5 - Resolving Merge Conflicts Step-by-Step
A merge conflict occurs when two branches modify the exact same lines of code in different ways. Git halts the merge and inserts conflict markers:
<<<<<<< HEAD
<h1 class="text-blue">Welcome to Somil's Portfolio</h1>
=======
<h1 class="text-sky">Welcome to Somil Sharma's Portfolio</h1>
>>>>>>> feature/branding
To resolve the conflict:
- Open the conflicted file in your code editor.
- Choose which version to keep (or combine both).
- Delete the conflict divider markers (
<<<<<<<,=======,>>>>>>>). - Stage the resolved file:
git add index.html - Complete the merge:
git commit -m "fix: resolve header branding conflict"
Part 6 - Connecting to GitHub & Remote Workflows
GitHub is a cloud hosting platform for Git repositories. It provides collaboration tools like Pull Requests, Issue tracking, and automated CI/CD deployment pipelines.
# 1. Connect local repo to remote GitHub repository
git remote add origin https://github.com/somilsharma/portfolio.git
# 2. Push code to GitHub (setting upstream on main)
git push -u origin main
# 3. Pull latest updates pushed by teammates
git pull origin main
# 4. Clone an existing repository to a new computer
git clone https://github.com/somilsharma/portfolio.git
The Golden Rule of .gitignore
Never commit passwords, API keys, build artifacts, or operating system junk! Always create a .gitignore file at the root of your project:
# .gitignore
node_modules/
dist/
.env
.DS_Store
*.log