Skip to main content

🌿 What is Git?

πŸ“– Definition​

Git is a distributed version control system. It tracks and manages code change history and enables multiple developers to work simultaneously. GitHub, GitLab, and Bitbucket are services that host Git repositories, making collaboration and code sharing easy.

🎯 Understanding with Analogy​

Game Save Points​

Think of Git like a game's save system:

  • Commit: Game save - saves current state
  • Branch: Parallel world - try different choices while keeping main story
  • Merge: Combine parallel worlds - apply good results to main story
  • Revert: Return to previous save - recover when you make mistakes

βš™οΈ How It Works​

Git's 3 Areas​

Working Directory (Work area)
↓ git add
Staging Area (Staging area)
↓ git commit
Repository (Repository)
↓ git push
Remote Repository (Remote repository)

Git Workflow​

1. Modify files (Working Directory)
2. Stage changes (Staging Area) β†’ git add
3. Create commit (Local Repository) β†’ git commit
4. Push to remote (Remote Repository) β†’ git push

πŸ’‘ Real Examples​

Basic Git Commands​

# Initialize Git repository
git init
# Or clone remote repository
git clone https://github.com/username/repo.git

# Check status
git status

# Stage changes
git add index.html # Specific file
git add . # All files

# Create commit
git commit -m "feat: Add user login"

# Push (upload)
git push origin main

# Pull (download)
git pull origin main

Branch Operations​

# Check branches
git branch

# Create new branch
git branch feature-login

# Switch branch
git checkout feature-login
# Or create and switch simultaneously
git checkout -b feature-login

# Latest method (Git 2.23+)
git switch feature-login
git switch -c feature-login

# Merge branch
git checkout main
git merge feature-login

# Delete branch
git branch -d feature-login

Collaboration Scenario​

# === Developer A ===
# 1. Clone repository
git clone https://github.com/team/project.git

# 2. Create new feature branch
git checkout -b feature-payment

# 3. Write code and commit
git add .
git commit -m "Implement payment feature"

# 4. Push to remote
git push origin feature-payment

# 5. Create Pull Request on GitHub

# === Developer B ===
# 1. Sync latest code
git checkout main
git pull origin main

πŸ€” FAQ​

Q1. What's the difference between Git and GitHub?

A:

Git (Tool)
β”œβ”€ Version control system (software)
β”œβ”€ Works on local computer
β”œβ”€ Used via command line (CLI)
└─ Free, open source

GitHub (Service)
β”œβ”€ Git repository hosting service
β”œβ”€ Web-based platform
β”œβ”€ Collaboration features (PR, Issue, Wiki)
└─ Code sharing and open source

Analogy: Git = Word processor, GitHub = Google Docs

Q2. How to write commit messages?

A: Follow clear and consistent conventions:

# βœ… Good commit messages
git commit -m "feat: Add user login API"
git commit -m "fix: Resolve signup button click error"
git commit -m "refactor: Remove duplicate code and separate functions"

# ❌ Bad commit messages
git commit -m "update"
git commit -m "fixed bug"
git commit -m "work in progress..."

# Conventional Commits rules
feat: New feature
fix: Bug fix
docs: Documentation changes
style: Code formatting
refactor: Code refactoring
test: Add tests
chore: Build, configuration changes

Q3. Difference between Merge and Rebase?

A:

# Merge
# - Preserves branch history as is
# - Creates merge commit
git checkout main
git merge feature-login

Result:
A---B---C---D main
\ /
E-----F feature-login

# Rebase
# - Moves branch to different base
# - Maintains linear history
git checkout feature-login
git rebase main

Result:
A---B---C---D main
\
E'---F' feature-login

# When to use?
Merge: Team collaboration, Pull Request
Rebase: Personal branch cleanup, clean history

Q4. How to resolve Conflicts?

A:

# Conflict occurs
git merge feature-login
# CONFLICT (content): Merge conflict in index.html

# Check conflicted files
git status

# Open file and manually resolve
# <h1>Final Title</h1>

# Mark as resolved
git add index.html
git commit -m "Merge branch 'feature-login' - resolved title conflict"

# Cancel merge if needed
git merge --abort

🎬 Summary​

Git is an essential tool for modern development:

  • Version Control: Track all change history
  • Collaboration: Multiple developers work simultaneously
  • Branch: Safe experimentation and feature development
  • Recovery: Return to previous state anytime

Mastering Git enables confident code management and efficient team collaboration! 🌿✨