Day 7 - Git and GitHub
π§ 1. What is Version Control System (VCS)?
A Version Control System (VCS) helps developers:
Share code efficiently among team members.
Track and manage changes (versions) in code over time.
π§© 2. Why Do We Need Version Control?
π§ββοΈ Problem 1 β Sharing Code
Example:
Developer 1 writes addition functionality.
Developer 2 writes subtraction functionality.
Both must combine their work to build a calculator application.
In real projects (like Amazon or Flipkart), there are hundreds of files and dependencies.
Sharing and merging manually (through email, Slack, etc.) is impossible.
β VCS helps developers share and merge code safely and efficiently.
π§Ύ Problem 2 β Versioning
Example:
Initially: βAddition of 2 numbersβ
Later: βAddition of 3 numbersβ β βAddition of 4 numbersβ
Finally: Product manager wants to revert to addition of 2 numbers.
Without versioning, you cannot roll back to an older working version.
β VCS lets you create, track, and revert code versions (v1, v2, v3, etc.).
π§± 3. Types of Version Control Systems
π§© Centralized Version Control (e.g., SVN, CVS)
A single central server stores all code.
Developers (Dev1, Dev2, etc.) commit and pull changes from that server.
If the central server goes down β no one can collaborate.
Dev1 β
β
Central Server (SVN)
β
Dev2 β
β Single point of failure
β Limited collaboration when offline
π Distributed Version Control (e.g., Git)
Every developer has a full local copy of the repository.
Developers can commit, work, and roll back locally.
Later, changes can be pushed to a shared remote repository (e.g., GitHub).
ββββββββββββββββ
β Central Repo β
ββββββββ¬ββββββββ
β
ββββββββββ΄βββββββββ
β β
Local Repo 1 Local Repo 2
(Dev1) (Dev2)
β
No single point of failure
β
Work offline and sync anytime
β
Faster operations
π‘ Interview Tip
Q: What is the difference between centralized and distributed VCS?
A: Centralized has one main server (like SVN); distributed gives each developer a full local copy (like Git).
π 4. What is a Fork?
A Fork is a personal copy of a repository.
Developers use forks to make changes without affecting the original codebase.
Example:
Companyβs main repo β example/project
Your fork β yourname/project
β You can modify freely and later merge back via a Pull Request.
π§° 5. What is Git?
Git is an open-source distributed version control system.
You can install Git on any server (e.g., EC2 instance or local machine).
It tracks changes, manages versions, and supports collaboration.
Commands like
git add,git commit, andgit pushform the Git lifecycle.
π 6. What is GitHub?
GitHub is a cloud-based platform built on top of Git that adds:
UI and collaboration features
Issue tracking
Code reviews and pull requests
CI/CD and project management tools
You can:
Create repositories
Host projects
Collaborate with teams
Manage workflows visually
π§© Git vs GitHub β Key Difference
| Feature | Git | GitHub |
| Type | Tool (VCS) | Platform (built on Git) |
| Usage | Local versioning | Online hosting & collaboration |
| Ownership | Open source | Owned by Microsoft |
| Access | Command line | Web UI + CLI |
| Example | Installed on local/EC2 | GitHub.com / GitHub Enterprise |
βοΈ 7. Installing Git
πΉ For Linux:
sudo apt update
sudo apt install git -y
πΉ For Windows/Mac:
Download from π https://git-scm.com/downloads
β Verify installation:
git --version
π» 8. Basic Git Lifecycle & Commands
| Step | Command | Purpose |
| Initialize Repository | git init | Create a new local Git repository |
| Check Status | git status | Show current changes |
| Add File(s) | git add <filename> | Stage files for commit |
| Commit Changes | git commit -m "message" | Save staged files as a version |
| View History | git log | Show list of commits |
| Compare Changes | git diff | See what changed in files |
| Push to Remote | git push origin main | Upload local commits to GitHub |
π§© 9. How Git Tracks Changes
Git creates a hidden folder
.git/when you rungit init.It contains:
objects/β data storage (commits, blobs)refs/β references to branchesconfigβ repository configurationhooks/β automation scriptsHEADβ pointer to current branch
If .git/ is deleted β repo tracking is lost.
π 10. Example Workflow
# Step 1: Create folder
mkdir example && cd example
# Step 2: Initialize Git
git init
# Step 3: Create a file
echo "a+b" > calculator.sh
# Step 4: Check status
git status
# Step 5: Track file
git add calculator.sh
# Step 6: Commit
git commit -m "Initial version - addition"
# Step 7: Modify file
echo "a+b+c" > calculator.sh
# Step 8: Check difference
git diff
# Step 9: Commit new version
git commit -am "Updated addition to include 3 numbers"
# Step 10: View commit history
git log
β You can revert to any previous version using:
git reset --hard <commit_id>
βοΈ 11. Connecting to GitHub
Create a GitHub account at https://github.com
Click New Repository
Name it β e.g.,
calculatorChoose Public or Private
Click Create Repository
Now connect your local repo:
git remote add origin https://github.com/<username>/<repo>.git
git branch -M main
git push -u origin main
Your project is now hosted on GitHub π
π§± 12. Real-World Collaboration Workflow
Fork the main repo (copy)
Clone to your local system
Make changes
Commit and push to your fork
Create a Pull Request (PR) to merge into the main branch
π§© 13. Summary
| Concept | Purpose |
| Version Control | Manage code versions and track changes |
| Centralized VCS (SVN) | Single server, limited collaboration |
| Distributed VCS (Git) | Full local copies, better reliability |
| Git | Core tool for version control |
| GitHub | Collaboration platform built on Git |
| Fork | Copy of a repository |
| Commit | Saved version of code |
| Push/Pull | Upload/download code from remote repo |
β In short:
Git manages your code versions locally.
GitHub lets you share and collaborate globally.