If you're starting your coding journey, Git is one of the first tools you need to learn. It's not optional anymore it's as essential as knowing how to code itself.
Let's learn Git from scratch, in simple terms.
What is Git?
Git is a version control system. Think of it as a time machine for your code.
Simple explanation:
- Git tracks every change you make to your files
- You can go back to any previous version anytime
- Multiple people can work on the same project without breaking things
- Everything is saved with a complete history
Created by: Linus Torvalds (the creator of Linux) in 2005
Type: Distributed Version Control System (DVCS)
What "distributed" means: Every developer has the complete project history on their computer. If one computer breaks, nothing is lost.
Why Use Git?
1. Track Changes
Know exactly what changed, when, and why.
2. Undo Mistakes
Broke something? Go back to when it worked.
3. Collaborate Safely
Multiple people can work together without overwriting each other's code.
4. Experiment Freely
Try new ideas without fear. If it fails, just discard it.
5. Professional Standard
Every company uses Git. It's a must-have skill.
Core Concepts You Need to Know
Before jumping into commands, understand these basics:
Repository (Repo)
A folder that Git tracks. It contains your project and its entire history.
Two types:
- Local repository: On your computer
- Remote repository: On GitHub, GitLab, etc.
Commit
A snapshot of your project at a specific moment. Think of it as a save point in a video game.
Each commit has:
- A unique ID (hash)
- Author name
- Date and time
- A message describing what changed
Branch
A separate version of your code. Like parallel universes for your project.
Common branches:
-
mainormaster: The main version -
feature-login: Working on login feature -
bugfix-header: Fixing a header bug
Staging Area
A waiting room for changes before they're committed. You decide which changes to include in the next commit.
HEAD
A pointer that shows which commit you're currently on. Usually points to the latest commit on your current branch.
Working Directory
The actual files you see and edit on your computer.
The Three States of Git
Understanding this is crucial:
Working Directory → Staging Area → Repository
1. Modified: You changed files but haven't saved them to Git
2. Staged: You marked changed files to be committed
3. Committed: Changes are saved in Git history
Flow:
- Edit files (Modified state)
- Add files to staging (Staged state)
- Commit files (Committed state)
Essential Git Commands
Let's learn the commands you'll use every day.
1. Setup Git (First Time Only)
Tell Git who you are:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Check your settings:
git config --list
2. Starting a New Project
Create a new Git repository:
# Navigate to your project folder
cd my-project
# Initialize Git
git init
This creates a hidden .git folder that stores all Git data.
3. Checking Status
See what's happening in your repo:
git status
This shows:
- Which files changed
- Which files are staged
- Which branch you're on
Example output:
On branch main
Changes not staged for commit:
modified: index.html
Untracked files:
style.css
4. Adding Files to Staging
Add specific file:
git add index.html
Add multiple files:
git add index.html style.css
Add all changes:
git add .
What this does: Moves files from "Modified" to "Staged" state.
5. Committing Changes
Save your staged changes:
git commit -m "Add homepage design"
The -m flag adds a message describing what you did.
Good commit messages:
- "Add user login feature"
- "Fix navigation bar bug"
- "Update contact page content"
Bad commit messages:
- "Changes"
- "Update"
- "asdfgh"
6. Viewing History
See all commits:
git log
Shorter version:
git log --oneline
Example output:
a3b4c5d Add login page
f6e7d8c Fix header styling
b9c8d7e Initial commit
7. Working with Branches
See all branches:
git branch
Create a new branch:
git branch feature-login
Switch to a branch:
git checkout feature-login
Create and switch in one command:
git checkout -b feature-login
Delete a branch:
git branch -d feature-login
8. Merging Branches
Merge a branch into current branch:
# Switch to main branch
git checkout main
# Merge feature branch into main
git merge feature-login
9. Connecting to Remote Repository
Add a remote repository:
git remote add origin https://github.com/username/repo.git
Check remote connections:
git remote -v
10. Pushing and Pulling
Push your code to remote:
git push origin main
Pull latest code from remote:
git pull origin main
First time push:
git push -u origin main
The -u flag sets up tracking.
11. Cloning a Repository
Download an existing repository:
git clone https://github.com/username/repo.git
12. Other Useful Commands
See changes in files:
git diff
Undo changes (not committed):
git restore index.html
Unstage a file:
git restore --staged index.html
See commit details:
git show commit-id
Basic Developer Workflow
Here's how you'll use Git daily:
Starting Your Day
# 1. Pull latest changes
git pull origin main
# 2. Create a new branch for your task
git checkout -b feature-add-footer
# 3. Start coding!
While Working
# 4. Check what changed
git status
# 5. Add your changes
git add .
# 6. Commit with a clear message
git commit -m "Add footer with social links"
# 7. Keep committing as you make progress
Finishing Your Work
# 8. Push your branch to remote
git push origin feature-add-footer
# 9. Create a Pull Request on GitHub (done in browser)
# 10. After approval, merge and delete branch
git checkout main
git pull origin main
git branch -d feature-add-footer
Common Mistakes to Avoid
1. Forgetting to commit regularly
Commit after each small change. Don't wait until end of day.
2. Bad commit messages
Write clear messages. Your future self will thank you.
3. Working directly on main
Always create a branch for new features.
4. Not pulling before pushing
Always pull latest changes first to avoid conflicts.
5. Adding everything blindly
Don't do git add . without checking git status first.
Quick Reference Cheat Sheet
# Setup
git config --global user.name "Name"
git config --global user.email "email"
# Start
git init # Create new repo
git clone <url> # Copy existing repo
# Basic workflow
git status # Check status
git add <file> # Stage file
git add . # Stage all files
git commit -m "message" # Commit changes
git log # View history
# Branches
git branch # List branches
git branch <name> # Create branch
git checkout <name> # Switch branch
git checkout -b <name> # Create and switch
git merge <name> # Merge branch
# Remote
git remote add origin <url> # Add remote
git push origin <branch> # Push to remote
git pull origin <branch> # Pull from remote
# Undo
git restore <file> # Undo changes
git restore --staged <file> # Unstage file
Next Steps
Now that you know Git basics:
- Practice daily: Use Git for every project
- Learn GitHub: Create an account and push your code
- Explore branching: Practice creating and merging branches
- Study conflicts: Learn how to resolve merge conflicts
- Read documentation: Git has excellent docs at git-scm.com
Final Thoughts
Git seems complicated at first, but it's just:
- Make changes
- Stage them
- Commit them
- Push them
That's 90% of what you'll do daily.
The other 10%? You'll learn as you go.
Start using Git today. Create a repo. Make commits. Push to GitHub. The best way to learn Git is by using it.
Resources:
- Official Git documentation: git-scm.com
- Interactive tutorial: learngitbranching.js.org
- GitHub guides: guides.github.com
Top comments (0)