Master Git Completely

Master Git: From Basics to Advanced | Ajay Connect

Master Git Completely

From your first repository to advanced team workflows. The definitive guide for developers who want to version control like pros.

Start Learning Now

📚 Choose Your Path

Jump to any section or scroll through the complete guide

1

Installation

Setup & Configuration

2

Fundamentals

Core Concepts

3

Branching

Parallel Development

4

Merging

Combining Work

5

Remote Repos

Collaboration

6

Advanced

Power User Tips

1

Installation & Setup

Beginner

Let’s get Git installed and configured properly. This foundation prevents 90% of beginner frustrations.

🪟

Windows

Download from git-scm.com or use winget install Git.Git

🍎

macOS

Install Xcode CLI tools or brew install git via Homebrew

🐧

Linux

Ubuntu/Debian: sudo apt-get install git

⚡ Essential Configuration
# Identity (required for every commit)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Modern default branch naming
git config --global init.defaultBranch main

# Better diffs and colors
git config --global color.ui auto
git config --global core.pager cat
Pro Tip: Run git config --list to verify settings. Use --local instead of --global for project-specific settings.
2

Git Fundamentals

Beginner

Git has three areas where your files live. Understanding this workflow is crucial:

💻

Working Directory

Your local files

📦

Staging Area

Pre-commit snapshot

💾

Repository

Permanent history

$ mkdir my-project && cd my-project
$ git init
Initialized empty Git repository in /my-project/.git/
$ echo “# My Project” > README.md
$ git status
Untracked files: README.md
$ git add README.md
$ git commit -m “Initial commit”
[main (root-commit) a1b2c3d] Initial commit
🔍 Daily Commands
git status              # Check current state
git add filename        # Stage specific file
git add .               # Stage all changes
git commit -m "msg"     # Save with message
git log --oneline       # View compact history
git diff                # See unstaged changes
Common Mistake: Forgetting to git add before git commit. Always check git status to see what’s staged!
3

Branching Strategy

Intermediate

Branches let you work on features, fixes, and experiments in isolation without breaking the main codebase.

Visual Branch Structure

M
a1b2c3d — Initial commit on main
F
e4f5g6h — Created feature/login branch
F
i7j8k9l — Added authentication logic
M
m0n1o2p — Main branch continues independently
🌿 Branch Commands
git branch              # List branches (* = current)
git branch new-name     # Create branch
git checkout new-name   # Switch to branch (old)
git switch new-name     # Switch to branch (new)
git switch -c new-name  # Create and switch
git branch -d name      # Delete merged branch
git branch -D name      # Force delete
🌿

Git Flow

Complex but structured: main, develop, feature, release, and hotfix branches. Best for scheduled releases.

🚀

GitHub Flow

Simple: main + feature branches. Create Pull Request, review, merge. Perfect for continuous deployment.

🎯

Trunk Based

Short-lived branches (1-2 days max). Requires feature flags. Fastest for high-performing teams.

4

Merging & Rebasing

Intermediate

Combining work requires strategy. Choose the right approach for clean, understandable history.

🔀 Merge Options
# Standard merge (preserves history, creates merge commit)
git switch main
git merge feature/login

# Squash merge (combines all feature commits into one)
git merge --squash feature/login
git commit -m "Add login feature"

# Fast-forward (linear history, no merge commit)
git merge --ff-only feature/login
🔄 Rebasing (History Rewriting)
# Move feature branch to tip of main
git switch feature/login
git rebase main

# Interactive rebase (edit/squash/reorder commits)
git rebase -i HEAD~3

# If conflicts occur during rebase
# 1. Fix conflicts, 2. git add ., 3. git rebase --continue
git rebase --abort  # Cancel rebase
Golden Rule: Never rebase commits that have been pushed to shared remotes! It rewrites public history and breaks others’ work.
When should you use rebase instead of merge?
A
When you want to preserve exact branch divergence history
B
When you want clean, linear history before merging to main
C
When collaborating on a shared branch with teammates
5

Remote Repositories

Intermediate

Connect your local work to the world. Push, pull, and collaborate with teams anywhere.

$ git remote add origin https://github.com/user/repo.git
$ git branch -M main
$ git push -u origin main
Enumerating objects: 3, done.
To https://github.com/user/repo.git
* [new branch] main -> main
$ git fetch origin
$ git pull origin main
🌐 Remote Commands
git remote -v                    # View remotes
git fetch origin                 # Download changes (no merge)
git pull origin main             # Fetch + merge
git push -u origin feature       # Push and track branch
git push origin --delete feature # Remove remote branch
git clone https://...            # Copy repository
Fetch vs Pull: Use fetch to preview changes before merging. pull immediately merges, which might surprise you with conflicts!
6

Advanced Techniques

Advanced

Emergency recovery, debugging, and power-user workflows. These tools separate experts from beginners.

🗑️

Undo Anything

git reset, revert, restore, and reflog for every undo scenario

🔍

Bisect Debugging

Binary search through history to find which commit introduced a bug

📦

Stashing

Save work-in-progress without committing messy code

🏷️

Tagging

Mark release points with semantic versioning (v1.0.0)

🆘 Emergency Recovery
# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, unstage changes
git reset HEAD~1

# Undo last commit, discard all changes (DANGER)
git reset --hard HEAD~1

# Create new commit that reverses specific commit
git revert a1b2c3d

# THE LIFESAVER: View all recent HEAD positions
git reflog
git checkout -b recovery HEAD@{2}
🧠 Stashing
git stash push -m "WIP: login form"    # Save with message
git stash list                        # View saved stashes
git stash pop                         # Apply and remove
git stash apply stash@{1}             # Apply specific stash
git stash branch new-branch           # Create branch from stash
Reflog is Magic: Git keeps a log of every HEAD movement for 30 days. Even if you delete a branch or reset –hard, you can recover it with git reflog!
7

Complete Cheat Sheet

Reference
🚀 Essential Commands
git init
Initialize repository
git clone [url]
Copy remote repo
git status
Check status
git add [file]
Stage file
git commit -m “msg”
Commit changes
git push
Upload to remote
git pull
Download & merge
git branch
List branches
git checkout -b [name]
Create & switch
git merge [branch]
Combine branches
git stash
Temporarily save
git log –oneline
Short history
⚡ Pro Aliases (add to ~/.gitconfig)
[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --oneline --graph --decorate --all
    last = log -1 HEAD
    unstage = reset HEAD --
    undo = reset --soft HEAD~1

🎉 You’ve Mastered Git!

From initialization to advanced recovery techniques, you now have the skills to version control like a professional.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *