Git for Beginners: Basics and Essential Commands

Coding without GIT is driving without a seatbelt. You might survive, but one wrong move and it’s game over.
Master Your Code History
If you have a project folder that looks like this:
project_final.code
project_final_v2.code
project_REALLY_FINAL_THIS_TIME.code
project_FOR_GODS_SAKE_WORK.code
We’ve all been there. We make a change, code breaks, panic, and we wish we had a magical "Undo" button. Well, in the coding world, that magical button exists. It’s called Git.
What is Git?
Imagine you are playing a difficult video game. Before fighting a major boss, what do you do? You save the game.
Why? Because if the boss defeats you, you don't want to start the entire game from level one. You want to reload right before the fight and try again. Git is the "Save Game" feature for your coding projects.
In technical terms: Git is a Distributed Version Control System. It manages different versions of your project over time. It takes snapshots of your files so you can travel back in time to when things worked perfectly.
In simple words: Git helps you track changes in your code, go back in time if something breaks, and work with others without messing things up.
Think of Git like this:
You’re writing code every day
You save versions of your work
Git keeps a history of every change
You can undo mistakes without crying

Git Basics & Core Terminologies
Let’s decode the scary words.
1. Repository (Repo)
A repository is just a folder where Git tracks your code and its history.
It contains:
Your files
Git metadata (hidden .git folder)

2. Commit
A commit is a saved snapshot of your code at a point in time.
Each commit has:
Unique ID
Message
Timestamp
📌 Rule: Commit small, commit often.
3. Branch
A branch is a separate line of development.
Default branch = main or master
Use branches to:
Try new features
Fix bugs
Avoid breaking main code
💡 Branch = “Let me experiment without risk”
4. HEAD
HEAD is where you are right now in Git history.
Simple:
HEAD points to the current commit
Move HEAD → change versions
Why do we need it?
The Time Machine: You deleted a crucial function yesterday and realized you need it today? Git lets you get it back in seconds.
Working Together without Killing Each Other: Imagine two people editing the same file at the same time on Google Drive/Dropbox. It’s chaotic. Git manages this beautifully, allowing whole teams to work on the same project without overwriting each other's code.
Experiment Fearlessly: Want to try a crazy new feature that might break everything? Git lets you create a parallel universe (called a "branch") to test things out. If it fails, you delete the universe. If it works, you merge it into the main project.
Common Git Commands
1. git init
This command turns a regular folder into a Git repository. It creates that hidden .git vault.
What it does:
Creates .git folder
Git starts tracking this project
2. git status
This is the command we use most often. It tells us the state of our Working Directory and Staging Area. It answers: "What have I changed? What is ready to be saved?"
Tells you:
Modified files
Staged files
Untracked files
3. git add
This moves changes from your Working Directory to the Staging Area.
git add file.js. // adds specific file
git add . // Add everything:
📌 This does NOT save yet. It just prepares.
4. git commit
This takes the snapshot of whatever is inside the Staging Area and saves it to the Vault.
git commit -m "Add login page UI"
Rules: Always write a meaningful message! Don't write "fixed stuff". Write "Fixed the navigation bar alignment issue". Future you will thank present you
5. git log
This shows a list of all past commits, who made them, when, and their messages.
You’ll see:
Commit ID
Author
Date
Message
| Command | Direction | What it actually does |
| git add | Working Dir → Staging | Takes your "messy" changes and prepares them for a snapshot. |
| git commit | Staging → Repository | Takes everything in the "loading dock" and saves it permanently with a message. |
| git checkout | Repository → Working Dir | This is the "Time Travel" button. It pulls an old version out of the vault and puts it back on your factory floor so you can see it again. |
A Basic Developer Workflow: Building a "Chai Recipe" Site
Let’s walk through a real scenario from absolute scratch.
Step 1: Setup Create a folder for our project and enter it.
mkdir chai_project
cd chai_project
Now, tell Git to start watching this folder.
Bash
git init
(Output: Initialized empty Git repository in /chai_project/.git/)
Step 2: Create content (Working Directory).
Create a file named recipe.txt and add some text to it: "Ingredients: Water, Tea Leaves, Sugar, Milk."
Step 3: Check what's going on
git status
Git will tell you in red text that recipe.txt is "Untracked". It sees it in the Working Directory but it's not in the Staging Area yet.
Step 4: Stage the file (Move to Staging Area) We like this file. Let's prepare it for saving.
git add recipe.txt
If you run git status now, the text will turn green. It's ready to be committed.
Step 5: Commit the file (Move to Vault) Take the snapshot.
git commit -m "Initial commit: Added base ingredients list"
(Output: [main (root-commit) ...] Initial commit: Added base ingredients list)
Step 6: Make changes and repeat Let's update the file. Add "Ginger and Cardamom" to your recipe.txt.
Run git status. Git sees you modified the file. Run git add . to stage the change. Run git commit -m "Added spices for better taste" to save the new snapshot.
Step 7: View History See what we have done so far.
git log
You will see two entries. Your initial commit, and your spicy commit with timestamps.