Git for Beginners: Basics and Essential Commands
What is Git?
Git is a version control system. It tracks changes to your files over time and lets you go back to any previous version whenever you need.
Think of it like a detailed save system for your code. Every time you save a checkpoint, Git remembers exactly what every file looked like at that moment. You can jump back to any checkpoint, compare changes, or even work on different versions simultaneously.
Git is distributed — every developer has a complete copy of the project history on their own computer. You don't need an internet connection to view history, make commits, or create branches. Everything works locally.
Why Git is Used
Without Git, collaboration is chaos. With Git, teams of thousands can work on the same codebase without stepping on each other's toes.
Git solves real problems:
History — See what changed, when it changed, and who changed it. Every modification is recorded.
Undo mistakes — Made a bad change? Go back to any previous version instantly.
Collaboration — Multiple people can work on the same project without overwriting each other's work.
Experimentation — Try new ideas in a separate branch without breaking the main code. If the experiment fails, just delete the branch.
Backup — Your code exists in multiple places. If your laptop dies, the code survives.
Every serious software project uses Git. It's not optional — it's fundamental.
Core Concepts
Before diving into commands, let's understand how Git thinks about your files.
Repository
A repository (or "repo") is a folder that Git is tracking. It contains your project files plus a hidden .git folder that stores all the history.
When you "initialize" a repository, you're telling Git: "Start tracking everything in this folder."
Working Directory
The working directory is where you actually edit files. It's what you see in your file explorer — the current state of your project.
When you modify a file, the change happens in the working directory first. Git notices the change but doesn't record it yet.
Staging Area
The staging area (also called the "index") is a preparation zone. Before saving a checkpoint, you choose which changes to include by adding them to the staging area.
This gives you control. Maybe you changed five files, but only want to save three of them in this checkpoint. You stage only those three.
Commit
A commit is a saved checkpoint. It captures the state of all staged files at a specific moment, along with a message describing what changed.
Each commit has a unique ID (a long string of letters and numbers) and knows which commit came before it. This creates a chain of history.
Branch
A branch is a separate line of development. You can work on new features in a branch without affecting the main code. When the feature is ready, you merge the branch back.
HEAD
HEAD is a pointer to your current location in the project history — usually the latest commit on your current branch. It's Git's way of knowing "where you are."
The Three Stages
Understanding this flow is essential:
- You edit files in the working directory
- You stage changes with git add — they move to the staging area
- You commit with git commit — they're saved to the repository
This three-step process gives you fine-grained control over what gets recorded.
Essential Commands
Let's walk through the commands you'll use every day.
Starting a New Repository
git init
This creates a new Git repository in your current folder. Git will start tracking changes from this point.
mkdir my-project cd my-project git init
You'll see: Initialized empty Git repository in /path/to/my-project/.git/
The .git folder is where Git stores everything. Don't delete it — that's your entire history.
Checking the Status
git status
This shows what's happening in your repository:
- Which files have been modified
- Which changes are staged
- Which branch you're on
Run git status often. It's your dashboard.
Example output:
On branch main Changes not staged for commit: modified: index.html Untracked files: style.css
This tells you: index.html was modified but not staged. style.css is new and Git isn't tracking it yet.
Adding Changes to Staging
git add filename
This moves changes from your working directory to the staging area.
# Add a specific file git add index.html # Add multiple files git add index.html style.css # Add all changed files in current directory git add .
After adding, run git status again. You'll see the files are now under "Changes to be committed."
Committing Changes
git commit -m "Your message here"
This saves a checkpoint with all staged changes. The message should describe what you did.
git commit -m "Add homepage layout"
Write clear messages. Future you will thank present you. Messages like "fix" or "update" are useless. Messages like "Fix login button not responding on mobile" are helpful.
Viewing History
git log
This shows all commits in reverse chronological order — newest first.
commit a1b2c3d4e5f6... (HEAD -> main)
Author: Your Name
Date: Mon Jan 15 10:30:00 2024
Add homepage layout
commit 9z8y7x6w5v4u...
Author: Your Name
Date: Mon Jan 15 09:15:00 2024
Initial commit
For a compact view:
git log --oneline
a1b2c3d Add homepage layout 9z8y7x6 Initial commit
Viewing Changes
git diff
Shows what changed in your working directory compared to the last commit. Useful before staging to review your modifications.
git diff --staged
Shows what's staged and will be included in the next commit.
A Basic Workflow
Here's how you'll actually use Git day-to-day:
1. Create or modify files
echo "Hello World" > index.html
2. Check what changed
git status
Output:
Untracked files: index.html
3. Stage your changes
git add index.html
4. Check status again
git status
Output:
Changes to be committed: new file: index.html
5. Commit with a message
git commit -m "Create homepage"
6. View your history
git log --oneline
Output:
a1b2c3d Create homepage
7. Keep working
Repeat the cycle: edit → add → commit.
Understanding the Flow
Every commit is a safe point you can return to. Commit often, with clear messages.
More Useful Commands
Removing Files from Staging
Staged something by mistake? Unstage it:
git restore --staged filename
The file stays modified in your working directory, but it's no longer staged.
Discarding Changes
Want to throw away changes and go back to the last commit?
git restore filename
Warning: This permanently discards your changes. Use carefully.
Viewing a Specific Commit
git show a1b2c3d
Shows the details of a specific commit — what changed, who made it, when.
Creating a Branch
git branch feature-name
Creates a new branch. To switch to it:
git checkout feature-name
Or do both in one command:
git checkout -b feature-name
Seeing All Branches
git branch
The current branch has an asterisk (*) next to it.
Going Back to a Previous Commit
To temporarily view an old commit:
git checkout a1b2c3d
To go back to the latest:
git checkout main
Configuring Git
Before your first commit, tell Git who you are:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
This information appears in every commit you make.
The .gitignore File
Some files shouldn't be tracked — build outputs, dependencies, secrets, system files. Create a .gitignore file to tell Git to ignore them:
# .gitignore node_modules/ .env *.log .DS_Store
Any file matching these patterns won't show up in git status and won't be committed.
Quick Reference
git init — Create a new repository
git status — Show current state
git add — Stage a file
git add . — Stage all changes
git commit -m "message" — Save staged changes
git log — View commit history
git log --oneline — Compact history view
git diff — Show unstaged changes
git diff --staged — Show staged changes
git restore — Discard changes in working directory
git restore --staged — Unstage a file
git branch — List branches
git checkout -b — Create and switch to new branch
Wrapping Up
Git has many features, but the basics are simple:
- git init — Start tracking a project
- git add — Prepare changes for saving
- git commit — Save a checkpoint
- git status — Check what's happening
- git log — See your history
Master these five commands first. Everything else — branches, merging, rebasing, remotes — builds on this foundation.
The key insight is the three-stage flow: working directory → staging area → repository. Once that clicks, Git becomes predictable. You'll always know where your changes are and how to move them forward.
Start a small project, make some commits, break things, fix them. That's how Git becomes second nature.
0 Comments
Sign in to join the conversation
No comments yet. Be the first to comment!