Skip to main content

Command Palette

Search for a command to run...

Day 7 - Git and GitHub

Updated
β€’6 min read

🧠 1. What is Version Control System (VCS)?

A Version Control System (VCS) helps developers:

  1. Share code efficiently among team members.

  2. 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, and git push form 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

FeatureGitGitHub
TypeTool (VCS)Platform (built on Git)
UsageLocal versioningOnline hosting & collaboration
OwnershipOpen sourceOwned by Microsoft
AccessCommand lineWeb UI + CLI
ExampleInstalled on local/EC2GitHub.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

StepCommandPurpose
Initialize Repositorygit initCreate a new local Git repository
Check Statusgit statusShow current changes
Add File(s)git add <filename>Stage files for commit
Commit Changesgit commit -m "message"Save staged files as a version
View Historygit logShow list of commits
Compare Changesgit diffSee what changed in files
Push to Remotegit push origin mainUpload local commits to GitHub

🧩 9. How Git Tracks Changes

  • Git creates a hidden folder .git/ when you run git init.

  • It contains:

    • objects/ β†’ data storage (commits, blobs)

    • refs/ β†’ references to branches

    • config β†’ repository configuration

    • hooks/ β†’ automation scripts

    • HEAD β†’ 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

  1. Create a GitHub account at https://github.com

  2. Click New Repository

  3. Name it β†’ e.g., calculator

  4. Choose Public or Private

  5. 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

  1. Fork the main repo (copy)

  2. Clone to your local system

  3. Make changes

  4. Commit and push to your fork

  5. Create a Pull Request (PR) to merge into the main branch


🧩 13. Summary

ConceptPurpose
Version ControlManage code versions and track changes
Centralized VCS (SVN)Single server, limited collaboration
Distributed VCS (Git)Full local copies, better reliability
GitCore tool for version control
GitHubCollaboration platform built on Git
ForkCopy of a repository
CommitSaved version of code
Push/PullUpload/download code from remote repo

βœ… In short:

Git manages your code versions locally.
GitHub lets you share and collaborate globally.

More from this blog

Dinesh's Blog

104 posts