Chapter 6: Version Control with Git
Chapter 6 of 15
Chapter 6: Version Control with Git
6.1 Git Basics
Git is a distributed version control system that tracks changes in your code. It allows you to save snapshots of your project, collaborate with others, and manage different versions of your code.
Why Use Git?
- Version History: Track all changes and revert if needed
- Collaboration: Multiple developers can work on the same project
- Branching: Work on features without affecting main code
- Backup: Code is stored in multiple locations
Basic Git Commands:
# Initialize a new Git repository
git init
# Check status of files
git status
# Add files to staging area
git add filename.js
git add . # Add all files
# Commit changes with a message
git commit -m "Add user authentication feature"
# View commit history
git log
# Create a new branch
git branch feature-name
# Switch to a branch
git checkout feature-name
# Merge branches
git merge feature-name
Git Workflow:
- Working Directory: Make changes to files
- Staging Area: Add files with
git add - Repository: Commit changes with
git commit
6.2 GitHub
GitHub is a cloud-based hosting service for Git repositories. It provides collaboration features, issue tracking, and code review tools.
GitHub Setup:
- Create a GitHub account at github.com
- Install Git on your computer
- Configure Git with your name and email
- Generate SSH keys for secure authentication
Git Configuration:
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# View configuration
git config --list
Connecting Local Repository to GitHub:
# Create repository on GitHub first, then:
git remote add origin https://github.com/username/repo-name.git
git branch -M main
git push -u origin main
Common GitHub Workflows:
- Clone Repository:
git clone https://github.com/username/repo.git - Push Changes:
git push origin main - Pull Changes:
git pull origin main - Fork Repository: Create your own copy of someone else's project
- Pull Requests: Propose changes to a project
6.3 Branching and Merging
Branches allow you to work on different features or versions of your code simultaneously.
Branch Strategies:
- Main/Master Branch: Production-ready code
- Development Branch: Integration branch for features
- Feature Branches: Individual features or fixes
- Hotfix Branches: Urgent production fixes
Branch Commands:
# List all branches
git branch
# Create and switch to new branch
git checkout -b feature-name
# Switch branches
git checkout branch-name
# Merge branch into current branch
git merge feature-name
# Delete branch
git branch -d feature-name
6.4 Collaboration Best Practices
Effective collaboration requires following Git best practices:
- Commit Often: Make small, frequent commits with clear messages
- Write Good Commit Messages: Describe what and why, not how
- Pull Before Push: Always pull latest changes before pushing
- Use Branches: Don't commit directly to main branch
- Review Code: Use pull requests for code review
- Resolve Conflicts: Handle merge conflicts carefully