What is Git — Simple Explanation with Examples
Git is a distributed version control system that tracks changes in files, enabling multiple developers to collaborate on projects simultaneously without overwriting each other’s work.
In this guide, you’ll learn how Git works, the essential commands for daily development, and how to collaborate with others using branches and pull requests. Git is the most important tool in a developer’s workflow.
Why Git Exists — The Problem It Solves
Before version control, collaboration looked like this:
- Emailing zip files back and forth
- Manually merging changes with “diff” and “patch”
- Naming folders like
project-final-v2-really-final - One person locking a file so others can’t edit it
Git solves all of these problems by providing:
- A complete history of every change
- The ability to work offline
- Branching for parallel development
- Merge conflict resolution
- Distributed backups (every clone is a full backup)
Centralized vs Distributed
Older systems like SVN and CVS used a centralized model — one server held the canonical history. If the server went down, you couldn’t commit or access history. Git is distributed: every developer has the full repository on their machine.
The Analogy — Save Points in a Game
Imagine playing a video game with unlimited save points:
- You can save at any moment (commit).
- You can branch off and explore a side quest without affecting the main story (branch).
- If you die on the side quest, you can reload the main story save (checkout).
- You can team up with friends, each working on different parts of the game, then combine your progress (merge).
Git is exactly this — a time machine for your code that makes experimentation safe and collaboration seamless.
The Three Areas of Git
Git operates across three states:
Working Directory Staging Area Repository
│ │ │
│─── git add ───────►│ │
│ │─── git commit ─►│
│ │ │
│◄──── git checkout ◄─────────────────│- Working Directory: Your actual files — what you see in your editor.
- Staging Area (Index): A preview of what will go into the next commit.
- Repository (HEAD): The stored history of commits.
Essential Git Workflow
# 1. Clone a repository (first time)
git clone https://github.com/user/project.git
# 2. Create a feature branch
git checkout -b feature/add-login
# 3. Make changes and stage them
git add login.html
git add login.css
# 4. Commit with a message
git commit -m "Add login page with form validation"
# 5. Push to remote
git push origin feature/add-login
# 6. Create a Pull Request (on GitHub/GitLab)
# (done via web UI or gh CLI)
# 7. After PR is merged, pull latest main
git checkout main
git pull origin main
# 8. Delete the feature branch
git branch -d feature/add-loginExpected output:
$ git commit -m "Add login page"
[feature/add-login a1b2c3d] Add login page
2 files changed, 45 insertions(+)Branching and Merging
Branches are lightweight pointers to specific commits. Creating a branch is instant — Git doesn’t copy files.
# List branches
git branch
# * main
# feature/add-login
# Switch branches
git checkout main
# Merge a feature branch into main
git merge feature/add-login
# Visualize branch history
git log --graph --oneline --allMerging vs Rebasing
| Operation | What It Does | Best For |
|---|---|---|
| Merge | Creates a merge commit joining two histories | Preserving exact history |
| Rebase | Applies commits from one branch onto another | Clean, linear history |
# Merge (creates a merge commit)
git checkout main
git merge feature/add-login
# Rebase (replays commits on top of main)
git checkout feature/add-login
git rebase mainThe Staging Area
Git’s staging area gives you fine-grained control over what goes into each commit:
# Stage specific files
git add src/index.js src/utils.js
# Stage specific changes within a file (interactive)
git add -p src/app.js
# Git asks for each hunk: stage this change? [y/n/q]
# See what's staged vs unstaged
git status
# Changes to be committed:
# modified: src/index.js
# Changes not staged for commit:
# modified: src/utils.js
# Unstage a file (keep changes)
git reset HEAD src/index.js.gitignore
The .gitignore file tells Git to ignore certain files (node_modules, build artifacts, secrets):
# Dependencies
node_modules/
vendor/
# Build output
dist/
build/
*.exe
# Environment files
.env
.env.local
# OS files
.DS_Store
Thumbs.db
# Logs
*.logConflict Resolution
When two developers modify the same lines, Git can’t automatically merge:
# After a merge conflict, Git shows:
git merge feature/conflicting
# Auto-merging app.js
# CONFLICT: content in app.js
# Automatic merge failed; fix conflicts and commit.
# Check which files have conflicts
git status
# both modified: app.js
# Open the file — Git marks conflicts:
# <<<<<<< HEAD
# console.log("version 1");
# =======
# console.log("version 2");
# >>>>>>> feature/conflicting
# Fix it, then:
git add app.js
git commit -m "Resolve merge conflict"GitHub/GitLab Collaboration
Creating a Pull Request
# Fork a repository (on GitHub)
# Clone your fork
git clone https://github.com/YOUR_USER/project.git
# Add upstream remote
git remote add upstream https://github.com/ORIGINAL_USER/project.git
# Create a feature branch
git checkout -b fix/typo-readme
# Make changes, commit, push
git add README.md
git commit -m "Fix typo in README"
git push origin fix/typo-readme
# Create PR via GitHub CLI
gh pr create --title "Fix typo in README" --body "Fixed a spelling error"Common Use Cases
1. Solo development
Track your own changes, experiment with branches, roll back mistakes, and sync between machines.
2. Open-source collaboration
Thousands of developers contribute to projects like Linux, React, and Kubernetes using Git forks and pull requests.
3. Team projects
Multiple developers work on features simultaneously on separate branches. Reviews happen through pull requests. CI/CD pipelines run automatically.
4. DevOps deployment
Git hooks and CI/CD trigger automated deployments when code is pushed to specific branches — push to main, deploy to production.
5. Infrastructure as Code
Terraform, Ansible, and Kubernetes configs are version-controlled with Git. Changes to infrastructure are reviewed, approved, and audited.
FAQ
Related Terms
Branch, Merge, Rebase, Pull Request, Conflict Resolution, Fork, Cherry Pick, Stash
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro