Git for Beginners: Basics and Essential Commands
Learn Git fundamentals with this beginner-friendly guide. Understand what Git is, why it's used, and master essential commands like init, add, commit, and log with practical examples.
A beginner-friendly guide to Git that explains what it is, why developers use it, and walks you through essential commands with practical examples. Learn the core concepts and start your version control journey with confidence. If you've ever accidentally deleted important code, wondered "wait, what did I change?", or struggled to collaborate with teammates without overwriting each other's work - Git is here to save the day. Let me show you why Git has become every developer's best friend.

What is Git?
Git is a distributed version control system (VCS) that tracks changes in your code over time. Think of it as a time machine for your projects - you can save snapshots of your work, go back to any previous version, and work on multiple features simultaneously without breaking anything.
Let me break that down:
- Distributed: Every developer has a complete copy of the project history on their computer. You're not dependent on a central server to view history or make commits.
- Version Control: It tracks every change you make, who made it, and when.
- System: It's a tool that runs on your computer and helps manage your code.
Unlike Google Docs where changes are automatically saved and merged, Git gives you complete control over what gets saved, when, and how different changes combine together.
Why Git is Used
Picture this: You're working on a project and everything's going great. Then you decide to add a new feature, and suddenly... nothing works. You try to undo your changes, but you've made so many edits that you can't remember what to revert.
This is where Git shines. Here's why developers worldwide rely on it:
1. Track Every Change
Git remembers every modification you make to your code. You can see:
- What changed
- Who changed it
- When it was changed
- Why it was changed (through commit messages)
2. Collaborate Without Chaos
Multiple developers can work on the same project simultaneously. Git intelligently merges everyone's changes and alerts you when there are conflicts that need manual resolution.
3. Experiment Safely
Want to try a risky new approach? Create a branch, experiment freely, and if things don't work out, just switch back. Your main code remains untouched.
4. Undo Anything
Made a mistake? No problem. Git lets you revert to any previous state of your project. It's like having unlimited undo with perfect memory.
5. Backup & Distribution
With platforms like GitHub, GitLab, and Bitbucket, your code is backed up in the cloud. If your laptop crashes, your work is safe.
Git Basics and Core Terminologies
Before we dive into commands, let's understand the fundamental concepts. Think of this as learning the vocabulary before having a conversation.
Repository (Repo)
A repository is your project folder that Git is tracking. It contains all your files plus a hidden .git folder where Git stores all the version control magic.
my-project/
├── index.html
├── style.css
├── script.js
└── .git/ ← Git's brain lives hereCommit
A commit is a snapshot of your project at a specific point in time. It's like taking a photo of all your files exactly as they are right now. Each commit has:
- A unique ID (hash)
- Author information
- Timestamp
- A message describing what changed
Think of commits as save points in a video game - you can always go back to them.
Branch
A branch is an independent line of development. Imagine your project as a tree:
main branch: A --- B --- C --- D
\
feature branch: E --- FThe main branch is your stable, production-ready code. Feature branches let you work on new things without disturbing main.
HEAD
HEAD is simply a pointer that tells Git which commit you're currently looking at. Usually, it points to the latest commit on your current branch.
HEAD → main → [Latest Commit]Working Directory
The working directory is where you actually edit your files. It's your normal project folder.
Staging Area (Index)
The staging area is like a loading dock where you prepare changes before committing them. You choose which changes to include in your next commit.
Remote
A remote is a version of your repository hosted on the internet (like GitHub). It's where you push your code to collaborate and back up your work.
The Three States of Git
This is crucial to understand. Your files can be in three states:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Working │ │ Staging │ │ Repository │
│ Directory │─────▶│ Area │─────▶│ (Committed) │
│ │ add │ │commit│ │
│ (Modified) │ │ (Staged) │ │ (Saved) │
└─────────────────┘ └─────────────────┘ └─────────────────┘- Modified: You've changed files but haven't told Git to save them yet
- Staged: You've marked files to be included in the next commit
- Committed: Changes are safely stored in Git's history
Common Git Commands
Now let's get our hands dirty with the essential commands every developer uses daily.
Setting Up Git (First Time Only)
Before you use Git, tell it who you are:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"This information will be attached to every commit you make.
git init - Start Tracking a Project
git initThis creates a new Git repository in your current folder. It adds the .git directory and starts tracking your project.
When to use: When starting a new project or adding version control to an existing project.
Example:
mkdir hello-world
cd hello-world
git init
# Output: Initialized empty Git repository in /path/to/hello-world/.git/git status - Check What's Happening
git statusThis is your diagnostic command. It shows:
- Which files have been modified
- Which files are staged for commit
- Which files are untracked (new files Git doesn't know about yet)
Pro tip: Run this command all the time. Before committing, after committing, whenever you're unsure. It's your project's health check.
Example output:
On branch main
Changes not staged for commit:
modified: index.html
Untracked files:
style.cssgit add - Stage Your Changes
git add <filename> # Stage a specific file
git add . # Stage all changes in current directory
git add *.js # Stage all JavaScript filesThis moves your changes from the working directory to the staging area, preparing them for commit.
When to use: After you've made changes and tested them, use git add to prepare them for saving.
Example:
# You modified index.html and created style.css
git add index.html
git add style.css
# Or simply: git add .git commit - Save Your Snapshot
git commit -m "Your descriptive message here"This saves all staged changes to the repository history. The -m flag lets you add a message describing what you did.
Good commit messages:
- "Add user login functionality"
- "Fix navigation menu on mobile devices"
- "Update README with installation instructions"
Bad commit messages:
- "Fixed stuff"
- "Changes"
- "asdfgh"
Example:
git commit -m "Add navigation bar and homepage styling"
# Output: [main 3a7f8bc] Add navigation bar and homepage styling
# 2 files changed, 45 insertions(+)git log - View History
git log # Full history with details
git log --oneline # Compact, one commit per lineThis shows all your commits in reverse chronological order (newest first).
Example output:
git log --oneline
3a7f8bc Add navigation bar and homepage styling
b45e23a Create initial HTML structure
9c1d456 Initial commitgit diff - See What Changed
git diff # Changes not yet staged
git diff --staged # Changes that are stagedThis shows exactly what you modified - line by line. Perfect for reviewing your work before committing.
Example:
diff --git a/index.html b/index.html
- <h1>Hello</h1>
+ <h1>Hello World</h1>git branch - Work with Branches
git branch # List all branches
git branch feature-login # Create new branch
git branch -d feature-login # Delete branchBranches let you work on features independently without affecting your main code.
Example workflow:
git switch main
git merge feature-login # Brings feature-login changes into mainA Basic Developer Workflow
Let me walk you through a real-world scenario from start to finish.
Scenario: Building a Simple Website
Step 1: Initialize Repository
mkdir my-website
cd my-website
git initStep 2: Create Your First File
echo "<h1>My Website</h1>" > index.htmlStep 3: Check Status
git status
# Output: Untracked files: index.htmlStep 4: Stage and Commit
git add index.html
git commit -m "Create homepage with heading"Step 5: Make Changes
echo "<p>Welcome to my site!</p>" >> index.htmlStep 6: Review Changes
git diff
# Shows the new paragraph you addedStep 7: Stage and Commit Again
git add index.html
git commit -m "Add welcome message to homepage"Step 8: View Your History
git log --oneline
# Output:
# f8a3c21 Add welcome message to homepage
# 7b2e891 Create homepage with headingCongratulations! You've just completed a full Git workflow.
Visual Guide: How Git Works
Let's visualize the complete flow:
┌────────────────────────────────────────────────────────────────────┐
│ GIT WORKFLOW │
└────────────────────────────────────────────────────────────────────┘
1. MODIFY FILES
┌─────────────────┐
│ Working │ ← You edit index.html
│ Directory │
└─────────────────┘
2. STAGE CHANGES (git add)
┌─────────────────┐
│ Working │
│ Directory │
└────────┬────────┘
│ git add
▼
┌─────────────────┐
│ Staging │ ← index.html ready to commit
│ Area │
└─────────────────┘
3. COMMIT CHANGES (git commit)
┌─────────────────┐
│ Staging │
│ Area │
└────────┬────────┘
│ git commit
▼
┌─────────────────┐
│ Repository │ ← Permanent snapshot saved
│ (.git folder) │
└─────────────────┘Commit History Flow
Time flows left to right →
C1 ← C2 ← C3 ← C4 ← C5 (HEAD → main)
↑
└─ C3a ← C3b (feature-branch)Each commit points to its parent, creating a chain of history. Branches diverge and can merge back together.
Local Repository Structure
hello-world/
│
├── .git/ ← Git's internal database
│ ├── objects/ ← All commits, files, trees stored here
│ ├── refs/
│ │ ├── heads/ ← Branch pointers
│ │ │ └── main
│ │ └── tags/
│ ├── HEAD ← Points to current branch
│ ├── config ← Repository settings
│ └── index ← Staging area info
│
├── index.html ← Your actual files (working directory)
├── style.css
└── script.jsPro Tips for Beginners
-
Commit Often: Small, frequent commits are better than one giant commit. Think of it like saving your game frequently.
-
Write Meaningful Messages: Your future self will thank you when you're trying to find when a bug was introduced.
-
Use
git statusLiberally: When in doubt, rungit status. It's your best friend. -
Don't Fear Branches: They're cheap and easy. Experiment freely!
-
Learn to Read
git log: Understanding your history helps you navigate your project. -
Start Small: Don't try to learn everything at once. Master the basics first (init, add, commit, log, status).
Common Beginner Mistakes (And How to Avoid Them)
Committing without checking status
Always run git status before committing to see what you're about to save
Vague commit messages like "update"
Be specific: "Fix login button alignment on mobile"
Working directly on main for everything
Create feature branches for new work
Forgetting to stage files
Use git add before git commit
Not committing often enough
Commit whenever you complete a logical unit of work
What's Next?
You've learned the fundamentals! Here's what to explore next:
- Remote repositories: Learn
git clone,git push, andgit pullto work with GitHub - Collaboration: Understand pull requests and code reviews
- Advanced branching: Explore
git rebaseand merge strategies - Fixing mistakes: Learn
git reset,git revert, andgit stash - Git GUI tools: Try GitKraken, SourceTree, or VS Code's built-in Git features
Conclusion
Git might seem overwhelming at first, but it's like learning to ride a bike - awkward initially, but soon becomes second nature. The key is to practice consistently with real projects.
Start simple:
git initto start trackinggit addto stage changesgit committo save snapshotsgit logto see history
These four commands alone will make you more productive and confident in your coding. Everything else builds on this foundation.
Remember: every expert Git user was once a beginner who felt confused by terms like "staging area" and "HEAD". Give yourself time, experiment in test projects, and don't be afraid to make mistakes - that's what Git is designed to handle!
Now go forth and commit with confidence!