DEV Community

Cover image for Git for Beginners: Basics and Essential Commands πŸš€
Harsh
Harsh

Posted on

Git for Beginners: Basics and Essential Commands πŸš€

If you are starting your journey as a developer, you will hear the word Git everywhere.

Job descriptions mention it, open-source projects require it, and almost every team uses it daily.

But what exactly is Git? And why is it so important?

This article explains Git from scratch, in simple words, with beginner-friendly examples and commonly used commands.

Developer working with code


πŸ“Œ What is Git?

Git is a distributed version control system.

That sounds complex, so let's simplify it.

πŸ‘‰ Git helps you track changes in your code over time.

It remembers:

  • What changed
  • When it changed
  • Who changed it
  • And lets you go back to older versions if something breaks

Think of Git like a time machine for your code.

Instead of saving files like:

project_v1
project_v2
project_final
project_final_real
Enter fullscreen mode Exit fullscreen mode

Git keeps everything organized and clean.

Git meme about file versions
We've all been there! Git saves us from this chaos.


πŸ€” Why Git is Used?

Git is used because it solves many real problems developers face.

1️⃣ Track Code History

You can see what changed and why it changed.

2️⃣ Work Without Fear

Made a mistake? You can go back to a previous version easily.

3️⃣ Collaboration

Multiple developers can work on the same project without overwriting each other's code.

4️⃣ Industry Standard

Almost every company and open-source project uses Git with platforms like:

  • GitHub
  • GitLab
  • Bitbucket

πŸ‘‰ Learning Git is non-negotiable for developers.

Team collaboration


🧠 Git Basics and Core Terminologies

Before commands, let's understand the basic concepts.

πŸ“ Repository (Repo)

A repository is a folder that Git tracks.

It contains:

  • Your project files
  • Git's history and metadata

You create it using:

git init
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Commit

A commit is a snapshot of your code at a specific point in time.

Think of a commit as: "Save point with a message"

Each commit has:

  • A unique ID (hash)
  • Author
  • Date
  • Message explaining the change

🌿 Branch

A branch lets you work on features independently.

  • main (or master) β†’ default branch
  • Feature branches β†’ new ideas or fixes

Branches help you experiment without breaking main code.

πŸ“ HEAD

HEAD points to your current position in Git history.

Simply put: HEAD = "Where you are right now"

Usually, HEAD points to the latest commit of the current branch.


πŸ“Š Understanding Git Workflow

Here's how Git organizes your work:

Git workflow diagram

Three main areas:

  1. Working Directory - Where you make changes
  2. Staging Area - Where you prepare changes for commit
  3. Repository - Where Git permanently stores commits

πŸ—‚οΈ Git Repository Structure

Git repository structure

Your local repository consists of:

  • Working Directory: Your actual project files
  • .git folder: All Git metadata and history
  • Remote Repository: GitHub/GitLab (optional)

πŸ›  Common Git Commands (With Examples)

Let's learn Git commands by actually using them.

πŸ”Ή git init

Initializes Git in your project folder.

git init
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Creates a hidden .git folder that Git uses to track everything.


πŸ”Ή git status

Shows the current state of your repository.

git status
Enter fullscreen mode Exit fullscreen mode

It tells you:

  • Which files are new
  • Which files are modified
  • What is staged for commit

πŸ‘‰ This is the most used Git command.


πŸ”Ή git add

Adds files to the staging area.

git add filename.txt
Enter fullscreen mode Exit fullscreen mode

Add everything:

git add .
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Staging means "I want to include this in the next commit".


πŸ”Ή git commit

Creates a snapshot of staged changes.

git commit -m "Add initial project files"
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ Always write clear and meaningful messages.

❌ Bad:

git commit -m "changes"
Enter fullscreen mode Exit fullscreen mode

βœ… Good:

git commit -m "Add login page UI"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή git log

Shows commit history.

git log
Enter fullscreen mode Exit fullscreen mode

You'll see:

  • Commit hash
  • Author
  • Date
  • Commit message

This helps you understand project history.

Commit history flow


πŸ”Ή git branch

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch:

git branch feature-login
Enter fullscreen mode Exit fullscreen mode

Switch branch:

git checkout feature-login
Enter fullscreen mode Exit fullscreen mode

(or modern way)

git switch feature-login
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή git merge

Merge one branch into another.

git merge feature-login
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Combines work from different branches.


πŸ”„ A Simple Git Workflow (From Scratch)

Let's see a real beginner workflow.

Step 1: Create a project

mkdir my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

Step 2: Initialize Git

git init
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a file

touch index.html
Enter fullscreen mode Exit fullscreen mode

Step 4: Check status

git status
Enter fullscreen mode Exit fullscreen mode

Step 5: Stage the file

git add index.html
Enter fullscreen mode Exit fullscreen mode

Step 6: Commit changes

git commit -m "Add initial HTML file"
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Congratulations! You just created your first Git commit.

Success celebration


πŸ˜„ Git Reality Check

Git commit meme


πŸ’‘ Tips for Beginners

βœ… Run git status often

βœ… Commit small, meaningful changes

βœ… Write clear commit messages

βœ… Don't fear breaking things β€” Git has your back

βœ… Practice with small projects

Learning journey

🎯 Conclusion

Git might seem intimidating at first, but once you understand the basics, it becomes second nature.

Start with these commands, practice regularly, and soon you'll be managing complex projects with confidence!

What's your biggest Git challenge? Drop a comment below! πŸ‘‡


=

Top comments (0)