Back to Home
    GitGit v2.40Beginner

    Git

    Essential Git commands for version control, branching, merging, and collaboration workflows.

    8 min read
    gitversion-controlcommandsworkflow

    Getting Started

    2 topics

    Initial Setup

    bash
    # Set user info (required for commits)
    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
    
    # Set default branch name
    git config --global init.defaultBranch main
    
    # Check settings
    git config --list

    💡 Use --global for system-wide settings

    📌 Local config overrides global settings

    Create & Clone Repositories

    bash
    # Create new repository
    git init
    git init <directory>
    
    # Clone existing repository
    git clone <url>
    git clone <url> <directory>
    
    # Clone specific branch
    git clone -b <branch> <url>

    💡 Clone creates a full copy with history

    ⚡ Use shallow clone for faster downloads

    Making Changes

    2 topics

    Check Status & Differences

    bash
    # Check status
    git status
    git status -s  # Short format
    
    # View changes
    git diff              # Unstaged changes
    git diff --staged     # Staged changes
    git diff HEAD         # All changes

    💡 Status shows staged, modified, and untracked files

    ⚡ Use -s for compact output

    Stage & Commit Changes

    bash
    # Stage files
    git add <file>
    git add .              # All files
    git add -p             # Interactive staging
    
    # Commit
    git commit -m "message"
    git commit -am "message"  # Add + commit tracked files
    git commit --amend        # Modify last commit

    💡 Stage files before committing

    ⚠️ Amend rewrites history - don't amend pushed commits

    Branching

    2 topics

    Branch Management

    bash
    # List branches
    git branch            # Local
    git branch -r         # Remote
    git branch -a         # All
    
    # Create branch
    git branch <name>
    git checkout -b <name>  # Create and switch
    
    # Delete branch
    git branch -d <name>    # Safe delete
    git branch -D <name>    # Force delete

    💡 Use descriptive branch names

    ⚡ checkout -b creates and switches in one step

    Merging & Rebasing

    bash
    # Merge branch into current
    git merge <branch>
    git merge --no-ff <branch>  # Force merge commit
    
    # Rebase current onto branch
    git rebase <branch>
    git rebase -i HEAD~3  # Interactive rebase
    
    # Abort if conflicts
    git merge --abort
    git rebase --abort

    💡 Merge preserves history, rebase rewrites it

    ⚠️ Never rebase public/shared branches

    Remote Repositories

    1 topic

    Working with Remotes

    bash
    # Manage remotes
    git remote -v
    git remote add origin <url>
    git remote remove <name>
    
    # Fetch and pull
    git fetch origin
    git pull origin main
    
    # Push changes
    git push origin main
    git push -u origin main  # Set upstream
    git push --force-with-lease  # Safe force push

    💡 fetch downloads without merging

    ⚠️ Avoid --force, prefer --force-with-lease

    Stashing Changes

    1 topic

    Save Work Temporarily

    bash
    # Stash changes
    git stash
    git stash push -m "description"
    
    # View stashes
    git stash list
    
    # Apply stash
    git stash apply         # Apply latest
    git stash pop           # Apply and delete
    
    # Delete stash
    git stash drop stash@{n}
    git stash clear         # Delete all

    💡 Stash is a stack - newest on top

    ⚡ Use stash to quickly switch branches

    History & Undo

    2 topics

    View History

    bash
    # View log
    git log
    git log --oneline       # Compact view
    git log --graph         # Visual branches
    git log -n 5            # Last 5 commits
    git log --author="name" # By author
    
    # Show commit details
    git show <commit>

    💡 --oneline is great for quick overview

    ⚡ Combine --graph --oneline for best view

    Undo Changes

    bash
    # Discard working changes
    git checkout -- <file>
    git restore <file>
    
    # Unstage files
    git reset HEAD <file>
    git restore --staged <file>
    
    # Reset commits
    git reset --soft HEAD~1   # Keep changes staged
    git reset --mixed HEAD~1  # Keep changes unstaged
    git reset --hard HEAD~1   # Discard everything
    
    # Revert (safe undo)
    git revert <commit>

    ⚠️ --hard is destructive and cannot be undone

    💡 Use revert for public branches instead of reset

    Related Articles

    Background reading and deeper explanations for this sheet.

    Building Autonomous Agents Without Overengineering: A Practical Guide for Real-World Teams

    Autonomous agents can automate meaningful work, but many projects fail because teams overbuild too early. This practical guide shows you how to design, ship, and improve useful agents with simple patterns, clear guardrails, and real-world examples—without turning your stack into a science project.

    keyword overlap

    How to Host Small Models with Ollama on Your Local Machine (Beginner to Production)

    Want private, fast AI on your laptop without cloud costs? This practical guide shows you exactly how to host small models using Ollama in local machine setups, from installation to API integration and troubleshooting. You’ll get real commands, real-world examples, and a production-minded workflow you can use today.

    keyword overlap

    Create a Generative Email Triage App with FastAPI, Postgres, LangGraph, and Azure Foundry

    Learn how to build a production-ready generative email triage app using FastAPI, Postgres, LangGraph, and Azure Foundry. This guide walks you through architecture, data modeling, workflow orchestration, and practical implementation details with real-world examples. By the end, you’ll have a beginner-friendly but deep blueprint you can run, extend, and deploy.

    keyword overlap

    Generative AI Roadmap: A Practical, Beginner-Friendly Guide to Learning and Building Real Projects

    Generative AI can feel overwhelming, but you don’t need a PhD to get started and create useful applications. This roadmap breaks down what to learn, when to learn it, and how to apply each skill in real-world projects—from prompt engineering and APIs to RAG, evaluation, and production deployment.

    keyword overlap