Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Published
β€’3 min readβ€’View as Markdown
Git for Beginners: Basics and Essential Commands

Imagine you are playing a difficult video game. You are about to fight a boss, but you know you might lose. What do you do?
You save your game.
If you lose, you reload the save. You don't have to start the entire game from level 1.

Git is exactly like that, but for your code.

If you delete a critical file by mistake, or if you write code that breaks your entire app, Git lets you "load a save file" and go back to when everything was working perfectly.
In technical terms, Git is a Version Control System (VCS). It tracks every change you make to your files, allowing you to travel back in time, collaborate with friends, and code without fear.

The Git Workflow: How it Actually Works

Before we type any commands, you need to understand the "Three States" of Git.

  1. Working Directory: This is your actual folder where you are typing code right now.

  2. Staging Area: This is a "waiting room." You pick specific files to go here before you save them.

  3. Repository: This is the safe database where your "snapshots" (commits) are stored forever.

The Flow Diagram

Here is how your code moves through Git:

The Essential Commands Cheat Sheet πŸ“

You don't need to memorize all 100+ Git commands. As developers, we use these 7 commands for 90% of the work.

1. git init (The Start Button)

This turns your regular folder into a Git repository.

  • Usage: Go to your project folder in the terminal and run this once.

  • Analogy: Buying a blank diary to start writing your history.

2. git status (The "What's Happening?" Check)

This tells you which files have been changed, which are in the staging area, and which are untracked.

  • Usage: Run this constantly. Before you add, before you commit always check status.

  • Output: Red files = not staged. Green files = ready to commit.

3. git add <filename> (The Selection)

This moves a file from your Working Directory to the Staging Area.

  • Usage:

    • git add index.html (Adds just one file)

    • git add . (Adds everything changed in the folder)

  • Analogy: putting items into your shopping cart. You haven't bought them yet, but you're getting ready to.

4. git commit -m "message" (The Save Point)

This takes everything in the Staging Area and saves it permanently to the Repository.

  • Usage: git commit -m "Added login button"

  • Analogy: Clicking "Checkout" and paying. The transaction is recorded forever.

  • Note: Always write a message inside the quotes so you know what you saved.

5. git log (The History Book)

This shows you a list of all previous commits (save points).

  • Usage: git log

  • Output: It shows the Author, Date, and the Commit Message.

6. git push (The Upload)

This sends your local commits to a remote server (like GitHub).

  • Usage: git push origin main

  • Analogy: Uploading your photos to Google Photos/iCloud so they are safe if your computer breaks.

7. git clone <url> (The Copy Paste)

This downloads an existing repository from the internet to your computer.

A Typical Workflow

Here is what your daily routine will actually look like:

  1. Code a feature: You write some code (Any Code).

  2. Check status: git status (You see your files are red/modified).

  3. Stage files: git add . (Now they are green/staged).

  4. Save: git commit -m "Created homepage layout" (Saved to local history).

  5. Backup: git push origin main (Sent to GitHub).

Summary

Git might seem intimidating because of the terminal, but remember: it is just a tool to save your work.

  • git add is "I want to save this."

  • git commit is "I am saving this."

  • git push is "Save Someplace Safe."

May your merge conflicts be zero! 0️⃣

More from this blog