Version control is a system that records changes to files over time so that you can recall specific versions later. It's essential for software development and collaboration.
Track Changes
Keep a detailed history of all changes made to your code, including who made them and when.
Collaboration
Multiple developers can work on the same project simultaneously without conflicts.
Backup & Recovery
Never lose your work. Revert to previous versions if something goes wrong.
Why Version Control Matters
Pro Tip: Version control is not just for code! You can use it for any text-based files including documentation, configuration files, and even this tutorial!
Reversibility: Easily undo changes that break your application
Accountability: See exactly who changed what and when
Branching: Work on different features simultaneously
Merging: Combine changes from different contributors
Distributed Development: Work offline and sync when ready
Quick Quiz: What is the primary purpose of version control?
A) To make code run faster
B) To compress files
C) To track changes and enable collaboration
D) To write better code
2. Git Fundamentals
Git is a distributed version control system that tracks changes in source code during software development. It's designed for speed, data integrity, and support for distributed workflows.
Key Git Concepts
Repository (Repo)
A directory containing your project files and the entire history of changes. Can be local or remote.
Commit
A snapshot of your project at a specific point in time. Each commit has a unique ID and message.
Branch
A parallel version of your repository. Allows you to work on different features independently.
Merge
The process of combining changes from different branches into a single branch.
Git Workflow Overview
Basic Git Workflow
Working Directory
Staging Area
Repository
1
Working Directory
This is where you edit your files. Changes here are not yet tracked by Git.
2
Staging Area (Index)
A holding area for changes you want to include in your next commit. Use git add to stage files.
3
Repository
The Git database where your project history is stored. Use git commit to save staged changes.
Installing Git
Installation: Download Git from git-scm.com and follow the installation instructions for your operating system.
# Verify Git installation
git --version
# Configure Git with your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Check your configuration
git config --list
Quick Quiz: What are the three main areas in Git workflow?
A) Local, Remote, Cloud
B) Working Directory, Staging Area, Repository
C) Master, Branch, Merge
D) Add, Commit, Push
3. GitHub Fundamentals
GitHub is a cloud-based hosting service for Git repositories. It provides a web-based interface for Git and adds collaboration features like issue tracking, project management, and code review.
GitHub vs Git
Git
Version control system
Command-line tool
Works locally
Distributed
Free and open source
GitHub
Hosting service for Git repositories
Web-based interface
Cloud-based
Collaboration features
Free tier + paid plans
Key GitHub Features
Code Hosting
Store your Git repositories in the cloud with unlimited public repositories.
Pull Requests
Propose changes to a repository and collaborate on code review before merging.
Issue Tracking
Track bugs, feature requests, and other project-related discussions.
Collaboration
Work with team members, assign tasks, and manage project permissions.
GitHub Actions
Automate your workflow with CI/CD pipelines and automated testing.
GitHub Pages
Host static websites directly from your GitHub repositories.
Creating Your First Repository
Interactive Demo: Create a Repository
Follow these steps to create your first GitHub repository:
Important: Never commit sensitive information like passwords, API keys, or personal data to a public repository!
4. Essential Git Commands
Master these fundamental Git commands to manage your repositories effectively. Each command is explained with practical examples.
Repository Setup
# Initialize a new Git repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
# Clone to a specific folder
git clone https://github.com/user/repo.git my-project
Basic File Operations
# Check repository status
git status
# Add files to staging area
git add filename.txt # Add specific file
git add . # Add all files
git add *.js # Add all JavaScript files
# Commit changes
git commit -m "Your commit message"
# Add and commit in one command
git commit -am "Your commit message"
Viewing History
# View commit history
git log
# View compact log
git log --oneline
# View graphical log
git log --graph --oneline --all
# View changes in a specific commit
git show commit-hash
# View differences
git diff # Working directory vs staging area
git diff --staged # Staging area vs last commit
git diff HEAD~1 # Compare with previous commit
Branching and Merging
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create new branch
git branch feature-branch
# Switch to branch
git checkout feature-branch
# Create and switch to new branch
git checkout -b feature-branch
# Merge branch
git checkout main
git merge feature-branch
# Delete branch
git branch -d feature-branch
# Unstage a file
git reset filename.txt
# Discard changes in working directory
git checkout -- filename.txt
# Revert a commit (creates new commit)
git revert commit-hash
# Reset to previous commit (dangerous!)
git reset --hard HEAD~1
# Reset to specific commit
git reset --hard commit-hash
Warning: Commands like git reset --hard can permanently delete your work. Always make sure you have backups or your changes are committed before using destructive commands.
Command Practice
Test your knowledge of Git commands:
What command would you use to create a new branch called "feature-login"?
A) git create feature-login
B) git new-branch feature-login
C) git branch feature-login
D) git checkout feature-login
How do you stage all modified files for commit?
A) git commit -a
B) git add .
C) git stage *
D) git add-all
5. Git Workflows
Learn popular Git workflows that teams use to collaborate effectively. Choose the right workflow for your project size and team structure.
Centralized Workflow
Best for: Small teams, simple projects
Everyone works on the main branch. Simple but can lead to conflicts with multiple developers.
main branch
# Centralized workflow commands
git clone https://github.com/team/project.git
git add .
git commit -m "Add new feature"
git pull origin main # Get latest changes
git push origin main # Push your changes
Feature Branch Workflow
Best for: Medium teams, organized development
Each feature is developed in its own branch. Features are merged back to main via pull requests.
main
feature-A
feature-B
# Feature branch workflow
git checkout -b feature-login # Create feature branch
git add .
git commit -m "Implement login"
git push origin feature-login # Push feature branch
# Create pull request on GitHub
# After review and approval, merge via GitHub
git checkout main
git pull origin main # Get merged changes
git branch -d feature-login # Delete local feature branch
Gitflow Workflow
Best for: Large teams, scheduled releases
Structured workflow with specific branches for development, features, releases, and hotfixes.
Simplified workflow focusing on main branch with feature branches and pull requests.
1
Create Branch
Create a feature branch from main for your work.
2
Add Commits
Make changes and commit them to your feature branch.
3
Open Pull Request
Open a pull request to propose your changes.
4
Review & Discuss
Team reviews code and discusses changes.
5
Deploy & Test
Deploy from the feature branch to test in production-like environment.
6
Merge
Once approved and tested, merge to main and deploy.
Choosing a Workflow: Start simple with GitHub Flow or Feature Branch workflow. Move to Gitflow only if you need structured releases and have a large team.
6. Best Practices
Follow these proven practices to maintain clean, professional repositories and collaborate effectively with your team.
Commit Message Guidelines
Writing Great Commit Messages
Good commit messages make your project history readable and useful.
# Good commit message structure:
#
# <type>(<scope>): <subject>
#
# <body>
#
# <footer>
# Examples:
feat(auth): add login functionality
fix(ui): correct button alignment on mobile
docs(readme): update installation instructions
refactor(api): optimize user data fetching
test(utils): add unit tests for validation helpers
Do:
Use present tense ("add" not "added")
Keep subject line under 50 characters
Capitalize the subject line
Don't end with a period
Use imperative mood
Don't:
Write vague messages like "fix bug"
Use past tense
Include unnecessary details in subject
Use periods in subject line
Commit without a message
Repository Structure
project-root/
├── README.md # Project overview and setup instructions
├── .gitignore # Files to ignore in version control
├── LICENSE # License information
├── package.json # Project dependencies (if applicable)
├── src/ # Source code directory
│ ├── components/ # Reusable components
│ ├── utils/ # Utility functions
│ └── index.js # Main entry point
├── tests/ # Test files
├── docs/ # Documentation
└── assets/ # Static assets (images, fonts)
Branch Naming Conventions
Consistent Branch Names
Feature Branches:
feature/user-authentication
feature/shopping-cart
feat/api-integration
Bug Fix Branches:
fix/login-error
bugfix/mobile-layout
hotfix/security-patch
Essential Files
README.md
The front page of your repository. Should include:
# Current repository state:
# - Branch: main
# - Status: clean
# - Remote: origin (https://github.com/user/repo.git)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Practice Makes Perfect: The best way to learn Git is to use it daily. Start with simple projects and gradually work on more complex scenarios. Don't be afraid to experiment with branches – you can always start over!
Congratulations!
You've completed the Git and GitHub tutorial. You now have the foundation to use version control effectively in your development projects.
Remember: Git is a powerful tool with many features. Start with the basics and gradually explore advanced features as you become more comfortable.