Understanding Version Control
Think of Git as a time machine for your code. Just as a writer keeps different drafts of their novel, Git helps developers track changes in their code. Imagine you're building a house (your project) - Git is like taking photographs at different stages of construction, allowing you to return to any point if needed.
Project Structure
Before we begin, ensure you have this folder structure:
project_root/
├── .git/ (Created automatically)
├── src/ (Your source code)
├── readme.md (Project documentation)
Essential Setup Steps
Installing Ubuntu on Windows
First, we'll set up our development environment. Think of this like setting up your workshop before starting a project.
# Open PowerShell as Administrator and run:
wsl --install -d Ubuntu
# After installation, launch Ubuntu and create your user account
Installing Git
Installing Git is like getting your basic toolset ready. In Ubuntu:
# Update package list
sudo apt update
# Install Git
sudo apt install git
# Verify installation
git --version
Configuring Git Identity
This is like signing your work. Every change you make will have your signature:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
GitHub Setup
GitHub is like a central library where developers store their code. It's a place where your local Git repository connects with the outside world.
Creating SSH Keys
SSH keys are like a secure handshake between your computer and GitHub:
# Generate SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"
# Start SSH agent
eval "$(ssh-agent -s)"
# Add your SSH key
ssh-add ~/.ssh/id_ed25519
# Copy your public key
cat ~/.ssh/id_ed25519.pub
Adding SSH Key to GitHub
- Go to GitHub.com and log in
- Click your profile picture → Settings
- Select "SSH and GPG keys"
- Click "New SSH key"
- Paste your public key and save
Your First Repository
Creating a repository is like starting a new project folder. Let's create one and push it to GitHub:
# Create a new directory
mkdir my_first_project
cd my_first_project
# Initialize Git repository
git init
# Create a sample file
echo "# My First Project" > readme.md
# Stage the file (like putting it in a preparation area)
git add readme.md
# Commit the file (like taking a snapshot)
git commit -m "Initial commit: Add readme"
# Create repository on GitHub and copy its SSH URL
# Then link your local repository
git remote add origin git@github.com:username/my_first_project.git
# Push your code
git push -u origin main
Real World Application
Let's look at a practical example of how this setup helps in real development:
# Creating a new feature branch
git checkout -b feature_login_page
# Make changes to your code
echo "<h1>Login Page</h1>" > login.html
# Stage and commit changes
git add login.html
git commit -m "Add basic login page structure"
# Push to GitHub
git push origin feature_login_page
Common Workflows
In real-world development, you might work on multiple features simultaneously. Here's how Git helps:
# Check status of your files
git status
# View commit history
git log
# Create and switch to a new branch
git checkout -b feature_name
# Merge changes from another branch
git merge branch_name
Further Learning
To deepen your understanding, explore these related topics:
- Git branching strategies (like Git Flow)
- Pull requests and code review processes
- Resolving merge conflicts
- Git hooks for automated tasks
- Advanced Git commands and their applications
Troubleshooting Tips
Sometimes things don't go as planned. Here are common issues and solutions:
# If you made a mistake in your last commit
git commit --amend
# If you need to undo staged changes
git reset HEAD file_name
# If you need to discard local changes
git checkout -- file_name