Git & GitHub
Complete Interactive Tutorial for Developers
Master version control and collaborative development with hands-on examples
Table of Contents
1. Introduction to Version Control
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
- 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?
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
This is where you edit your files. Changes here are not yet tracked by Git.
Staging Area (Index)
A holding area for changes you want to include in your next commit. Use git add
to stage files.
Repository
The Git database where your project history is stored. Use git commit
to save staged changes.
Installing Git
Quick Quiz: What are the three main areas in Git workflow?
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:
Sign Up for GitHub
Visit github.com and create a free account.
Create New Repository
Click the "+" icon in the top right corner and select "New repository".
Configure Repository
- Choose a repository name (e.g., "my-first-repo")
- Add a description (optional)
- Choose public or private
- Initialize with a README file
Clone to Local Machine
4. Essential Git Commands
Master these fundamental Git commands to manage your repositories effectively. Each command is explained with practical examples.
Repository Setup
Basic File Operations
Viewing History
Branching and Merging
Remote Repository Operations
Undoing Changes
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"?
How do you stage all modified files for commit?
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.
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.
Gitflow Workflow
Best for: Large teams, scheduled releases
Structured workflow with specific branches for development, features, releases, and hotfixes.
Branch Types:
- main: Production-ready code
- develop: Integration branch
- feature/*: New features
- release/*: Prepare releases
- hotfix/*: Emergency fixes
Workflow Steps:
- Create feature branch from develop
- Work on feature
- Merge back to develop
- Create release branch
- Merge release to main and develop
GitHub Flow
Best for: Continuous deployment, web applications
Simplified workflow focusing on main branch with feature branches and pull requests.
Create Branch
Create a feature branch from main for your work.
Add Commits
Make changes and commit them to your feature branch.
Open Pull Request
Open a pull request to propose your changes.
Review & Discuss
Team reviews code and discusses changes.
Deploy & Test
Deploy from the feature branch to test in production-like environment.
Merge
Once approved and tested, merge to main and deploy.
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.
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
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:
- Project description
- Installation instructions
- Usage examples
- Contributing guidelines
- License information
.gitignore
Prevent unwanted files from being committed:
- Build artifacts
- Dependencies (node_modules)
- Environment files (.env)
- IDE configuration files
- OS-specific files (.DS_Store)
Security Best Practices
- Never commit passwords, API keys, or sensitive data
- Use environment variables for configuration
- Review code before merging pull requests
- Keep dependencies updated
- Use signed commits for verified authorship
- Enable two-factor authentication on GitHub
Code Review Guidelines
Effective Code Reviews
For Authors:
- Keep pull requests small and focused
- Write clear descriptions
- Test your changes thoroughly
- Respond to feedback constructively
- Update documentation if needed
For Reviewers:
- Review code promptly
- Be constructive and specific
- Check for functionality and style
- Verify tests are included
- Approve when satisfied
Quick Quiz: Which is a good commit message?
7. Interactive Examples
Practice Git concepts with these interactive scenarios. Each example simulates real-world situations you'll encounter as a developer.
Scenario 1: Your First Contribution
Scenario: Contributing to an Open Source Project
You want to contribute to an open source project. Follow the complete workflow:
Fork the Repository
Go to the project's GitHub page and click "Fork" to create your own copy.
Clone Your Fork
Add Upstream Remote
Create Feature Branch
Make Your Changes
Edit the README.md file to fix the typo, then:
Push to Your Fork
Create Pull Request
Go to your fork on GitHub and click "New Pull Request". Write a clear description of your changes.
Scenario 2: Handling Merge Conflicts
Scenario: Resolving Merge Conflicts
Two developers modified the same file. Learn how to resolve conflicts:
The Conflict Occurs
When you try to merge or pull, Git shows a conflict:
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
View Conflict Markers
Open the conflicted file. Git adds conflict markers:
Resolve the Conflict
Edit the file to resolve the conflict. Choose one version or combine both:
Mark as Resolved
Scenario 3: Emergency Hotfix
Scenario: Emergency Production Fix
Critical bug in production needs immediate fix while development continues:
Create Hotfix Branch
Create a hotfix branch from the main branch:
Implement Quick Fix
Make the minimal necessary changes to fix the issue:
Test the Fix
Run tests to ensure the fix works and doesn't break anything:
Deploy to Production
Merge Back to Development
Ensure the fix is also in the development branch:
Clean Up
Git Command Simulator
Command Line Simulator
Practice Git commands in a safe environment:
8. Summary & Next Steps
Congratulations! You've completed the Git and GitHub tutorial. Here's what you've learned and where to go next.
What You've Learned
Core Concepts
- Version control fundamentals
- Git repository structure
- Working directory, staging area, and commits
- Branches and merging
- Remote repositories
Practical Skills
- Essential Git commands
- GitHub collaboration features
- Pull request workflow
- Conflict resolution
- Best practices and conventions
Quick Reference
Most Used Commands
Daily Commands:
git status
git add .
git commit -m "message"
git push
git pull
Branching:
git branch
git checkout -b branch
git merge branch
git branch -d branch
Information:
git log
git diff
git show
git remote -v
Next Steps
Advanced Git
- Interactive rebasing
- Cherry-picking commits
- Git hooks
- Submodules
- Advanced merging strategies
DevOps Integration
- GitHub Actions (CI/CD)
- Automated testing
- Deployment pipelines
- Code quality tools
- Security scanning
Team Collaboration
- Code review best practices
- Issue tracking and project management
- Documentation strategies
- Open source contribution
- Team workflow optimization
Additional Resources
Recommended Reading
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.