Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
โ€ข10 min read
Git for Beginners: Basics and Essential Commands

Hey there! ๐Ÿ‘‹ If you've ever wondered how programmers keep track of their code changes or work together on the same project without messing things up, you're in the right place. Today, we're going to learn about Git โ€“ a super useful tool that every developer uses.

Don't worry if you've never heard of it before. I'll explain everything step by step, just like I'm teaching a friend. Let's dive in!


What is Git?

Imagine you're writing a story for school. You write the first draft, then make some changes, then add more paragraphs, and maybe delete some parts. What if you want to go back to an earlier version because you liked it better? Or what if you're working with friends and everyone needs to add their part without erasing someone else's work?

Git is like a time machine for your files.

More specifically, Git is a distributed version control system. Let's break that down:

  • Version Control System: A tool that tracks changes to your files over time

  • Distributed: Everyone working on the project has a complete copy of the project history on their computer

Think of Git as a super-smart save system that remembers every change you make to your files, who made it, and when. You can go back to any previous version whenever you want!


Why is Git Used?

You might be thinking, "Why can't I just save my files normally?" Great question! Here's why Git is awesome:

1. Track Every Change

Git remembers every single change you make. If something breaks, you can easily go back to when it was working.

2. Work with Others

Multiple people can work on the same project at the same time without overwriting each other's work.

3. Experiment Safely

Want to try something new but worried it might break your code? Git lets you experiment in a safe "sandbox" without affecting the main project.

4. Backup Your Work

Your entire project history is saved, so you'll never lose your work.

5. See Who Changed What

Git shows who made what changes and when. This is super helpful when working in teams.

Real-life analogy: Think of Git like Google Docs' version history, but way more powerful and designed specifically for code!


Git Basics and Core Terminologies

Before we start using Git, let's understand some basic terms. Don't worry โ€“ they're simpler than they sound!

Repository (Repo)

A repository is like a project folder that Git watches. It contains all your files and the complete history of changes.

Think of it as: A magic folder that remembers everything that ever happened inside it.

Commit

A commit is like taking a snapshot of your project at a specific moment. Each commit saves the current state of your files.

Think of it as: A checkpoint in a video game. You can always come back to this exact point.

Branch

A branch is a separate line of development. You can create branches to work on new features without affecting the main code.

Think of it as: A parallel universe where you can try things out. If it works, great! If not, the main universe (main branch) is still safe.

HEAD

HEAD is a pointer that shows which commit you're currently looking at. Usually, it points to the latest commit on your current branch.

Think of it as: A "You Are Here" marker on a map.

Staging Area (Index)

This is a middle area where you prepare files before committing them. You choose which changes to include in your next commit.

Think of it as: A shopping cart. You add items (changes) to the cart before checking out (committing).

Working Directory

This is your actual project folder where you create, edit, and delete files.

Think of it as: Your desk where you're doing the actual work.


How Git Works: The Three States

Git has three main areas where your files can be:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Working      โ”‚      โ”‚    Staging      โ”‚      โ”‚   Repository    โ”‚
โ”‚   Directory     โ”‚โ”€โ”€โ”€โ”€โ”€>โ”‚      Area       โ”‚โ”€โ”€โ”€โ”€โ”€>โ”‚   (.git folder) โ”‚
โ”‚                 โ”‚      โ”‚                 โ”‚      โ”‚                 โ”‚
โ”‚  Where you edit โ”‚      โ”‚  Prepare files  โ”‚      โ”‚  Saved commits  โ”‚
โ”‚     files       โ”‚      โ”‚  for commit     โ”‚      โ”‚   (history)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
     git add                  git commit

Here's the flow:

  1. Working Directory: You make changes to your files here

  2. Staging Area: You add the changes you want to save using git add

  3. Repository: You save those changes permanently using git commit


Local Repository Structure

When you create a Git repository, here's what it looks like:

my-project/
โ”‚
โ”œโ”€โ”€ .git/                    โ† Git's magic folder (stores all history)
โ”‚   โ”œโ”€โ”€ objects/             โ† Stores all commits
โ”‚   โ”œโ”€โ”€ refs/                โ† Stores branch information
โ”‚   โ””โ”€โ”€ HEAD                 โ† Points to current branch
โ”‚
โ”œโ”€โ”€ file1.txt                โ† Your actual project files
โ”œโ”€โ”€ file2.txt
โ””โ”€โ”€ folder/
    โ””โ”€โ”€ file3.txt

The .git folder is where Git stores all the magic. You don't need to touch it โ€“ Git handles everything automatically!


Common Git Commands

Now let's learn the essential commands you'll use every day. I'll explain each one with examples!

1. git init - Start a New Repository

This command creates a new Git repository in your current folder.

When to use: When you're starting a brand new project.

git init

What happens: Git creates a hidden .git folder that starts tracking your project.

Example:

mkdir my-first-project
cd my-first-project
git init

Output: Initialized empty Git repository in /my-first-project/.git/


2. git status - Check What's Going On

This shows you what's changed in your project and what's ready to commit.

When to use: All the time! Use this frequently to see what's happening.

git status

What it shows:

  • Files you've changed but not staged

  • Files ready to be committed

  • Files Git isn't tracking yet


3. git add - Stage Files for Commit

This moves your changes from the working directory to the staging area.

When to use: After you've made changes and want to prepare them for saving.

git add filename.txt          # Add a specific file
git add folder/               # Add all files in a folder
git add .                     # Add all changed files

Example:

git add index.html           # Stage just index.html
git add .                    # Stage all changed files

4. git commit - Save Your Changes

This creates a permanent snapshot of the staged changes.

When to use: After staging files and you want to save this checkpoint.

git commit -m "Your message describing what you changed"

The -m flag lets you add a message explaining what you did.

Example:

git commit -m "Added homepage header and footer"

Good commit messages:

  • โœ… "Fixed login button bug"

  • โœ… "Added user profile page"

  • โŒ "Updated stuff" (too vague!)

  • โŒ "asdfgh" (meaningless!)


5. git log - View Commit History

This shows a list of all the commits you've made.

When to use: When you want to see the history of changes.

git log                      # Full detailed log
git log --oneline            # Compact one-line view
git log --graph              # Visual branch graph

Example output:

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: Your Name <you@email.com>
Date:   Sat Jan 10 14:30:00 2026

    Added homepage header and footer

6. git diff - See What Changed

This shows the exact changes you made to files.

When to use: Before committing, to review what you changed.

git diff                     # See unstaged changes
git diff --staged            # See staged changes

7. git branch - Work with Branches

Branches let you work on different features separately.

git branch                   # List all branches
git branch feature-name      # Create a new branch
git branch -d branch-name    # Delete a branch

8. git checkout - Switch Branches

This command lets you switch between branches or restore files.

git checkout branch-name     # Switch to a branch
git checkout -b new-branch   # Create and switch to new branch

9. git merge - Combine Branches

This combines changes from one branch into another.

git checkout main            # Switch to main branch
git merge feature-branch     # Merge feature-branch into main

10. git clone - Copy a Repository

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

git clone https://github.com/username/project.git

11. git pull - Get Latest Changes

This downloads new changes from the internet repository.

git pull

12. git push - Upload Your Changes

This uploads your commits to an online repository (like GitHub).

git push

A Basic Developer Workflow: Step-by-Step Example

Let's put it all together! Here's how a typical developer uses Git from scratch.

Scenario: You're creating a simple website project.

Step 1: Create a Project and Initialize Git

# Create a new folder for your project
mkdir my-website
cd my-website

# Initialize Git
git init

Step 2: Create Your First File

# Create a file (on Mac/Linux)
echo "<h1>Welcome to My Website</h1>" > index.html

# On Windows, you can create the file manually

Step 3: Check Status

git status

Output:

On branch main
Untracked files:
  index.html

Git sees the file but isn't tracking it yet.


Step 4: Stage the File

git add index.html

# Check status again
git status

Output:

Changes to be committed:
  new file: index.html

Now the file is in the staging area, ready to be committed!


Step 5: Make Your First Commit

git commit -m "Create homepage with welcome message"

Output:

[main a1b2c3d] Create homepage with welcome message
 1 file changed, 1 insertion(+)

๐ŸŽ‰ Congratulations! You've made your first commit!


Step 6: Make More Changes

# Edit index.html and add more content
echo "<p>This is my first website!</p>" >> index.html

# Check what changed
git diff

Step 7: Stage and Commit Again

git add index.html
git commit -m "Add description paragraph to homepage"

Step 8: View Your History

git log --oneline

Output:

b2c3d4e Add description paragraph to homepage
a1b2c3d Create homepage with welcome message

Look at that! You can see your entire project history!


Commit History Flow

Here's how your commits build up over time:

main branch:

A โ† B โ† C โ† D โ† HEAD
โ”‚   โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ "Add contact page"
โ”‚   โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€ "Fix homepage typo"
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ "Add description paragraph"
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ "Create homepage"

Each letter is a commit (snapshot)
Arrows point to previous commits
HEAD shows where you are now

Quick Reference: Most Common Commands

Here's a cheat sheet for the commands you'll use most often:

CommandWhat It Does
git initStart tracking a new project
git statusSee what's changed
git add .Stage all changes
git commit -m "message"Save a checkpoint
git log --onelineSee history
git checkout -b branch-nameCreate new branch
git checkout mainSwitch to main branch
git merge branch-nameCombine branches

Important Tips for Beginners

1. Commit Often

Make small, frequent commits rather than huge ones. It's easier to understand and undo if needed.

2. Write Clear Commit Messages

Future you will thank present you! Explain what you did and why.

3. Use git status Frequently

Before and after commands, check status to understand what's happening.

4. Don't Panic!

Git is very forgiving. Most mistakes can be undone. Take your time to learn.

5. Experiment in Branches

Never be afraid to try things. That's what branches are for!


What's Next?

You've learned the basics of Git! Here's what you can explore next:

  • GitHub: A website where you can store your Git repositories online and share them

  • Collaborating: Learn how to work with others using Git

  • Advanced Commands: git rebase, git stash, git reset

  • Resolving Conflicts: What to do when two people change the same file


Summary

Let's recap what we learned:

โœ… Git is a version control system that tracks changes to your files
โœ… It works in three stages: Working Directory โ†’ Staging Area โ†’ Repository
โœ… Commits are snapshots of your project at specific points in time
โœ… Branches let you experiment safely without affecting the main code
โœ… Basic workflow: Create โ†’ Edit โ†’ git add โ†’ git commit โ†’ Repeat!

Remember, everyone starts as a beginner. The more you practice using Git, the more natural it will become. Start with a small personal project and try these commands yourself!

Happy coding! ๐Ÿš€

More from this blog

ThitaInfo Blogs

37 posts

Making AI simple, fun, and practical for developers.