# 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 development team uses it daily.

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

Git helps you manage your code changes safely, work together smoothly, and develop software in a professional way.

This article explains **Git from scratch**, in simple language, with beginner-friendly examples and the most commonly used commands.

## What Is Git?

**Git is a version control system.**

In simple words, Git helps you **track changes in your code over time**.

Think of Git like a **time machine for your project**:

* You can see what changed
    
* You can go back to older versions
    
* You can work with others without breaking their code
    

Git was created to solve one big problem:

> *How do developers safely manage changes in large projects?*

## Why Git Is Used

Git is used because it **solves real problems developers face while building software**, especially when projects grow or multiple people work together.

Below are the core reasons Git is essential in modern development.

### 1\. Version Control (Track Changes Safely)

Git keeps a complete history of your project. Every change is recorded. You can see who changed what and when. You can go back to any previous version.

<mark>👉 If something breaks, Git lets you rollback safely.</mark>

### 2\. Collaboration Without Others

In real projects, many developers work on the same codebase.

**Git allows:**

* Multiple people work simultaneously
    
* Merging work without overwriting others’ code
    
* Resolving conflicts in a controlled way
    

<mark>👉 Without Git, teamwork becomes messy and error-prone.</mark>

### 3\. Experiment Without Fear (Branches)

Git allows you to create **branches**.

* Try new features
    
* Test ideas
    
* Fix bugs separately
    

<mark>If something fails, you can simply discard the branch—</mark>**<mark>main code stays safe</mark>**<mark>.</mark>

### 4\. Backup & Recovery

Your code is not tied to one machine.

* Stored locally and remotely (GitHub, GitLab, etc.)
    
* If your laptop crashes, your code is still safe
    
* Deleted files can be recovered from the history
    

<mark>👉 Git acts like an automatic backup system.</mark>

### 5\. Professional Workflow Standard

Almost every company uses Git.

* Code reviews
    
* CI/CD pipelines
    
* Deployment automation
    
* Open-source contributions
    

<mark>👉 Knowing Git is </mark> **<mark>mandatory</mark>**<mark>, not optional, for developer jobs.</mark>

### 6\. Clear Project History

Git creates a clean timeline of your project:

* Feature additions
    
* Bug fixes
    
* Improvements
    

<mark>This helps teams understand </mark> **<mark>why</mark>** <mark> a change was made, not just </mark> **<mark>what</mark>** <mark> changed.</mark>

## 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:

```javascript
git init
```

### **Understanding Git Workflow**

Here's how Git organizes your work:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767529670587/2da8e710-eb59-40db-8413-0ea8689d602b.png align="center")

**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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767531105989/f1d7c068-da14-4452-8087-9da0ef8d9800.png align="center")

**Core Concepts of Commit History**

* **Snapshot-Based:** Commits are snapshots of the entire project, not just file differences.
    
* **Parent References:** Each commit references its immediate predecessor (parent).
    
* `HEAD` Pointer: `HEAD` represents the current snapshot you are working on, usually pointing to the tip of the current branch.
    
* **Unique Identifier:** Each commit is identified by a 40-character SHA-1 hash. 
    

### **📦 Commit**

A **commit** is a snapshot [of](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwxn5vv8e520rs0oujee.png) 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.

## **Git Repository Structure**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1767530080014/f98bb0d1-c125-4b71-96de-6e389470bc8d.png align="center")

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 E**[**xa**](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F925fjikl9gtvwjnyiyrl.png)**mples)**

Let's learn Git commands by actually using them.

### **git init**

Initializes Git in your project folder.

```javascript
git init
```

Creates a hidden `.git` folder that Git uses to track everything.

---

### **git status**

Shows the current state of your repository.

```javascript
git status
```

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.

```javascript
git add filename.txt
```

Add everything:

```javascript
git add .
```

Staging means **"I want to include this in the next commit**.

---

### **git commit**

Creates a snapshot of staged changes.

```javascript
git commit -m "Add initial project files"
```

✔️ **Always write clear and meaningful messages.**

---

### **git log**

Shows commit history.

```javascript
git log
```
