Git Basics Tutorial: A Beginner's Guide to Version Control
Welcome to Git! This tutorial will teach you the essential Git commands you need to start managing your code like a pro.
📚 What is Git?
Git is a version control system that helps you track changes in your code, collaborate with others, and never lose your work. Think of it as a "save point" system for your projects.
🔧 Step 1: Install Git
First, install Git on your computer:
• Windows: Download from git-scm.com
• Mac: Install via Homebrew with 'brew install git'
• Linux: Use 'sudo apt-get install git' (Ubuntu/Debian)
Verify installation by opening your terminal and typing:
git --version
🚀 Step 2: Configure Git
Set up your identity:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
📁 Step 3: Create Your First Repository
Navigate to your project folder:
cd my-project
Initialize a Git repository:
git init
This creates a hidden .git folder that tracks all changes.
💾 Step 4: Basic Git Workflow
Here's the most common workflow:
1. Check status:
git status
2. Add files to staging:
git add filename.js
or add all files:
git add .
3. Commit your changes:
git commit -m "Add new feature"
4. View commit history:
git log
🌿 Step 5: Working with Branches
Branches let you work on features without affecting the main code:
Create a new branch:
git branch feature-name
Switch to the branch:
git checkout feature-name
Or do both at once:
git checkout -b feature-name
Merge branch back to main:
git checkout main
git merge feature-name
🔄 Step 6: Connecting to GitHub
To backup your code online:
1. Create a repository on GitHub
2. Link your local repo:
git remote add origin https://github.com/username/repo.git
3. Push your code:
git push -u origin main
📥 Step 7: Pull Changes
When working with others, get their updates:
git pull origin main
💡 Quick Tips for Beginners
✅ Commit often with clear messages
✅ Always pull before you push
✅ Use branches for new features
✅ Check 'git status' frequently
✅ Write meaningful commit messages
🎯 Common Commands Cheat Sheet
git init - Start a new repository
git status - Check what's changed
git add . - Stage all changes
git commit -m "message" - Save your changes
git push - Upload to remote repository
git pull - Download from remote repository
git clone [url] - Copy a repository
git branch - List branches
git checkout [branch] - Switch branches
git merge [branch] - Combine branches
🎓 Practice Exercise
Try this to solidify your learning:
1. Create a new folder called 'git-practice'
2. Initialize a Git repository
3. Create a file called 'hello.txt' with "Hello Git!"
4. Add and commit the file
5. Create a new branch called 'updates'
6. Add more text to hello.txt
7. Commit the changes
8. Switch back to main and merge your updates branch
✨ Conclusion
Congratulations! You now know the Git basics. The more you practice, the more natural these commands will become. Remember: every expert was once a beginner who kept practicing.
Happy coding! 🚀