Git & Version Control

Master Git version control system. Learn essential commands, branching strategies, and collaboration workflows used in modern development.

What is Git?

Git is a distributed version control system that tracks changes in source code during software development. Created by Linus Torvalds in 2005, Git is designed for coordinating work among programmers, but it can be used to track changes in any set of files.

Why Use Version Control?

  • 📜 History - Track every change made to your codebase
  • đŸ‘Ĩ Collaboration - Multiple developers can work on the same project
  • 🔄 Branching - Work on features in isolation without affecting main code
  • âĒ Revert - Easily undo changes and go back to previous versions
  • 🔍 Blame - See who made what changes and when

Git Workflow

The Three States

Git has three main states that your files can reside in:

  1. Modified - You have changed the file but not committed it
  2. Staged - You have marked a modified file to go into your next commit
  3. Committed - The data is safely stored in your local database

Branching Strategy

Common branching strategies include:

  • Git Flow - Feature branches, develop, and main branches
  • GitHub Flow - Simple feature branch workflow
  • Trunk-Based Development - Short-lived branches merged frequently

Git Best Practices

  • Write clear, descriptive commit messages
  • Commit often with small, focused changes
  • Use branches for new features and bug fixes
  • Pull frequently to stay up to date
  • Review code before merging (Pull Requests)
  • Never commit sensitive data (use .gitignore)
đŸ’ģ Essential Git Commands
# Initialize a new repository
git init

# Clone a repository
git clone https://github.com/user/repo.git

# Check status
git status

# Add files to staging
git add .
git add filename.txt

# Commit changes
git commit -m "Add new feature"

# Push to remote
git push origin main

# Pull latest changes
git pull origin main

# View commit history
git log --oneline -10
Output
Initialized empty Git repository in /project/.git/ Cloning into 'repo'... remote: Counting objects: 100%, done. On branch main Changes to be committed: new file: feature.py [main abc1234] Add new feature 1 file changed, 50 insertions(+) a1b2c3d Add authentication e4f5g6h Fix login bug i7j8k9l Initial commit
💡 These are the fundamental Git commands used in daily development. Master these to work efficiently with version control.
đŸ’ģ Git Branching & Merging
# Create and switch to a new branch
git checkout -b feature/user-auth

# Or with newer Git syntax
git switch -c feature/user-auth

# List all branches
git branch -a

# Switch to existing branch
git checkout main
git switch main

# Merge feature branch into main
git checkout main
git merge feature/user-auth

# Delete branch after merge
git branch -d feature/user-auth

# Rebase instead of merge
git checkout feature/user-auth
git rebase main
Output
Switched to a new branch 'feature/user-auth' * feature/user-auth main remotes/origin/main Switched to branch 'main' Merge made by 'recursive' strategy. auth.py | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) Deleted branch feature/user-auth
💡 Branching allows you to work on features in isolation. Merge or rebase to integrate changes back into the main branch.