Git is a change-tracking system — a form of Version Control (Source Control). It tracks modifications, stores history, and manages collaboration. Git is optimized for line-by-line tracking, not binary files.


Core Advantages


Commit

A commit is a snapshot of your entire project at a specific moment.

Each commit includes:


Three Working Stages


HEAD Pointer

HEAD points to the latest commit of the currently checked-out branch.


Version Control Implementation

Repository

Distributed System

Every developer has a full copy of the repository.


Essential Git Commands


Repository Management

git init                      # Initialize a new repository
git status                    # Show current branch, staged changes, and unstaged changes

Basic Workflow

git add .                     # Stage all modified files
git add <file>                # Stage specific file
git commit -m "message"       # Commit staged changes
git diff                      # Show unstaged changes
git diff --staged             # Show staged changes

History & Logging

git log                       # Full commit history
git log --graph               # Visual branch diagram
git log --oneline             # One-line-per-commit view
git log --all                 # Logs from all branches
git reflog                    # Shows every HEAD movement (great for recovery)

Undoing Changes

git revert <commit>           # Safe: creates a commit that undoes another
git reset <commit>            # Dangerous: rewrites history

Reset Modes


File Recovery

git restore <file>            # Discard working directory changes
git restore --staged <file>   # Unstage a file

Branch Operations

git branch                    # List local branches
git branch -a                 # List local + remote branches
git checkout -b <name>        # Create and switch to new branch
git switch <branch>           # Modern branch switching
git checkout <branch>         # Traditional branch switching
git branch -d <branch>        # Delete merged branch
git merge <branch>            # Merge branch into current

Stashing Changes

git stash                     # Save uncommitted changes
git stash list                # View saved stashes
git stash push -m "message"   # Save stash with description
git stash pop                 # Apply & remove latest stash
git stash apply               # Apply stash without removing

Remote Operations

git push -u origin main       # Push commits to remote (+ set upstream)
git pull                      # Fetch + merge remote updates
git fetch                     # Fetch without merging

Branch Naming Conventions

Use consistent prefixes:

Reference: https://conventional-branch.github.io/


Solving Merge Conflicts

A merge conflict occurs when two commits modify the same part of a file.

Steps:

  1. Open conflict file
  2. Manually resolve
  3. Stage the file
  4. Commit the merge resolution

Git Rebase vs Merge

Merge

Rebase