Featured Image Alt Text: Master Git Tutorial - Terminal with Git commands, branching visualization, and version control flow on a laptop screen.

Meta Description: Complete Git tutorial from basics to advanced. Learn installation, commits, branching, merging, remotes, best practices, and troubleshooting. Master version control with simple explanations and real-world examples.

SEO-Ready Slug: /master-git-complete-tutorial-from-basics-to-advanced

Category: Development / Version Control

Tags: git, git-tutorial, version-control, github, git-commands, branching, software-development, developer-tools, coding-best-practices


Hey there! I'm a senior developer who's been living and breathing Git for over 15 years. I've debugged merge hell at 3 AM, recovered "lost" commits, and helped countless teams adopt Git properly. Today, I'm documenting everything I wish I had in one place when I started.

This isn't just another command list. This is a deep-yet-simple guide that explains why things work the way they do. By the end, you'll use Git confidently, not just copy-paste commands.

Part 1: Understanding the Fundamentals

What is Git, really?

Git is a distributed version control system created by Linus Torvalds in 2005.

Think of it as a time machine for your code:

  • It tracks every change
  • Allows multiple people to work without stepping on each other's toes
  • Lets you experiment safely with branches
  • Gives you the power to go back in time

Key concept: Git works locally first. Everything happens on your machine until you explicitly push to a remote (like GitHub).

Installing Git

On macOS:

brew install git

On Ubuntu/Debian:

sudo apt update && sudo apt install git

On Windows: Download from https://git-scm.com or use Winget:

winget install --id Git.Git

Verify:

git --version

First-time Setup (Do this once)

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "code --wait"  # VS Code example
git config --global init.defaultBranch main

Pro tip: Use --global for your machine. You can override per repository if needed.

Part 2: Git Basics - Your Daily Workflow

Initializing a Repository

mkdir my-project
cd my-project
git init

This creates a hidden .git folder — Git's brain.

The Staging Area Concept (This is crucial)

Git has three main areas:

  1. Working Directory — where you edit files
  2. Staging Area (Index) — where you prepare changes for commit
  3. Repository — where committed snapshots live
# Check status
git status

# Stage files
git add file.txt
git add .              # everything
git add -u             # modified tracked files only

# Commit
git commit -m "Add login feature"

Best practice: Write meaningful commit messages. I follow the Conventional Commits standard:

  • feat: add user authentication
  • fix: resolve login timeout
  • refactor: simplify payment flow

Viewing History

git log --oneline --graph --all
git log -p              # show changes
git show <commit-hash>

Part 3: Branching - Git's Superpower

Why branches? You can work on new features without breaking the main codebase.

# Create and switch branch
git checkout -b feature/user-profile

# List branches
git branch -a

# Switch branches
git checkout main
git switch feature/user-profile   # newer command

Visualizing branches:

git log --oneline --graph --decorate

Merging Changes

# From main branch
git checkout main
git merge feature/user-profile

Merge vs Rebase:

  • Merge creates a merge commit — preserves history exactly.
  • Rebase replays your commits on top of main — cleaner history.

My rule: Rebase feature branches before merging. Merge only when integrating to protected branches.

git rebase main
# Resolve conflicts if any, then
git rebase --continue

Part 4: Working with Remotes (GitHub, GitLab, Bitbucket)

# Add remote
git remote add origin https://github.com/user/repo.git

# First push
git push -u origin main

# Pull latest changes
git pull origin main

Pro workflow:

  1. git pull --rebase (keeps history linear)
  2. Work on feature branch
  3. Push feature branch
  4. Create Pull Request (PR)
  5. Review → Merge

Cloning Repositories

git clone https://github.com/user/repo.git
cd repo

Shallow clone for large repos:

git clone --depth 1 https://github.com/user/repo.git

Part 5: Intermediate to Advanced Commands

Stashing - Save work temporarily

git stash push -m "WIP: login UI"
git stash list
git stash pop               # apply and remove
git stash apply stash@{0}   # apply without removing

Cherry-pick - Apply specific commits

git cherry-pick <commit-hash>

Reset and Restore (Be careful!)

git reset --soft HEAD~1     # undo commit, keep changes staged
git reset --hard HEAD~1     # dangerous - removes everything
git restore file.txt        # newer, safer way

Reflog - Your safety net

git reflog

This shows everything you've done. Lost a commit? Reflog can recover it.

git reset --hard HEAD@{2}

Tags

git tag v1.0.0
git push origin v1.0.0

Part 6: Real-World Best Practices & Workflows

Gitignore

Create .gitignore:

node_modules/
*.log
.env
dist/

Commit Often, Push When Ready

Small, focused commits > one giant commit.

Branch Naming Convention

  • feature/add-payment
  • bugfix/login-crash
  • chore/update-deps

Code Review Workflow (Industry Standard)

  1. Create feature branch from main
  2. Work + commit frequently
  3. Rebase on latest main
  4. Push and open PR
  5. Request review
  6. Merge with "Squash and merge" or "Rebase and merge" depending on team policy

Part 7: Debugging Common Git Issues

"I committed to main by mistake!"

git checkout -b fix/accidental-main-commit
git reset --keep origin/main
# Now push your new branch and create PR

Merge conflicts?

Git marks them in files:

<<<<<<< HEAD
Your change
=======
Incoming change
>>>>>>> branch-name

Resolve, then:

git add .
git commit

"My changes disappeared!"

  1. Check git status
  2. Check git reflog
  3. git fsck --lost-found for extreme cases

Large files problem?

Use Git LFS:

git lfs install
git lfs track "*.psd"

Part 8: Advanced Topics

  • Git Hooks (pre-commit, pre-push)
  • Submodules (for dependencies)
  • Worktrees (multiple branches at once)
  • Bisect (find which commit introduced a bug)
  • Sparse Checkout (clone partial repo)

Example: Git Bisect

git bisect start
git bisect bad
git bisect good v1.0
# Git checks out commits, you test, mark good/bad
git bisect reset

Final Tips from a Senior Dev

  1. Always pull before starting work
  2. Commit message is documentation
  3. Protect your main branch with rules
  4. Review your own PR before requesting others
  5. Backup before dangerous commands (git branch backup-before-reset)

Git is not complicated once you understand the mental model: Snapshots + DAG (Directed Acyclic Graph).

Practice on a throwaway repo. Break things. Recover them. That's how you master it.

You've got this!


This article is documented as I would explain to my team — with the why, the how, and the "oh no" recovery paths. Happy coding!