Courses/Git/Git Basics: Clone, Add, Commit

    Lesson 2 โ€ข Beginner

    Git Basics: Clone, Add, Commit ๐ŸŒฟ

    Master the three core commands every developer uses daily โ€” clone repositories, stage changes, commit snapshots, and inspect your history.

    What You'll Learn in This Lesson

    • โ€ข git clone to download existing repositories
    • โ€ข git add and git commit โ€” the daily workflow
    • โ€ข git log and git diff โ€” inspecting history and changes
    • โ€ข Undoing mistakes with restore, reset, and revert
    • โ€ข Writing meaningful commit messages

    1๏ธโƒฃ git clone โ€” Download a Project

    git clone downloads an entire repository from GitHub (or any remote) to your machine โ€” including all files, history, and branches. It's how you start working on an existing project.

    Try It: git clone

    Download and explore an existing repository

    Try it Yourself ยป
    JavaScript
    // git clone โ€” Download an Existing Repository
    console.log("=== git clone ===");
    console.log("Clone copies an entire repository from a remote server.");
    console.log();
    console.log("  $ git clone https://github.com/user/project.git");
    console.log("  Cloning into 'project'...");
    console.log("  remote: Enumerating objects: 156, done.");
    console.log("  remote: Counting objects: 100% (156/156), done.");
    console.log("  Receiving objects: 100% (156/156), 2.4 MiB, done.");
    console.log();
    console.log("Wh
    ...

    2๏ธโƒฃ git add & git commit โ€” The Daily Loop

    The add-commit cycle is the heartbeat of Git. Edit files โ†’ git add to stage โ†’ git commit to save. You'll do this hundreds of times per project. Stage related changes together for clean, logical commits.

    Try It: Add & Commit

    Stage files and create commits step by step

    Try it Yourself ยป
    JavaScript
    // git add & git commit โ€” The Daily Workflow
    console.log("=== The Add-Commit Cycle ===");
    console.log("This is what you'll do dozens of times per day:");
    console.log("  1. Edit files");
    console.log("  2. git add (stage changes)");
    console.log("  3. git commit (save snapshot)");
    console.log();
    
    console.log("=== git add โ€” Staging Files ===");
    console.log("  git add index.html        โ† Stage one file");
    console.log("  git add src/              โ† Stage entire folder");
    console.log("  git add .      
    ...

    3๏ธโƒฃ git log & git diff โ€” Inspect History

    git log shows your commit history. git diff shows exactly what changed โ€” line by line, red for removed, green for added. These are essential for understanding what happened in your project.

    Try It: Log & Diff

    View commit history and see line-by-line changes

    Try it Yourself ยป
    JavaScript
    // git log & git diff โ€” Inspecting History
    console.log("=== git log โ€” View Commit History ===");
    console.log();
    console.log("  $ git log --oneline");
    
    const commits = [
      { hash: "d4e5f6g", msg: "Add dark mode toggle", author: "Alice", time: "2 hours ago" },
      { hash: "a1b2c3d", msg: "Fix navbar spacing", author: "Bob", time: "5 hours ago" },
      { hash: "x9y8z7w", msg: "Add user profile page", author: "Alice", time: "1 day ago" },
      { hash: "m3n4o5p", msg: "Set up React Router", author: "Charlie"
    ...

    4๏ธโƒฃ Undoing Changes

    Everyone makes mistakes. Git provides multiple ways to undo: restore for uncommitted changes, reset for local commits, and revert for safely undoing pushed commits.

    Try It: Undo Changes

    Learn restore, reset, amend, and revert

    Try it Yourself ยป
    JavaScript
    // Undoing Changes โ€” restore, reset, revert
    console.log("=== Undo Toolkit ===");
    console.log("Git gives you multiple ways to undo mistakes.");
    console.log();
    
    console.log("=== 1. Discard Working Directory Changes ===");
    console.log("  $ git restore index.html");
    console.log("  โ†’ Throws away all uncommitted changes to index.html");
    console.log("  โ†’ File goes back to the last committed version");
    console.log("  โš ๏ธ This is PERMANENT โ€” changes are lost!");
    console.log();
    
    console.log("=== 2. Unstage
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Using git add . blindly โ€” This stages everything. Always run git status first to see what you're adding.
    โš ๏ธ
    Vague commit messages โ€” "fix stuff" or "update" tells you nothing. Write what you changed and why.
    ๐Ÿ’ก
    Pro Tip: Commit early, commit often. Small, focused commits are easier to review, revert, and understand than massive ones.

    ๐Ÿ“‹ Quick Reference

    CommandPurpose
    git clone URLDownload a repository
    git add fileStage changes
    git commit -m "msg"Save a snapshot
    git log --onelineView commit history
    git diffSee uncommitted changes
    git restore fileDiscard changes

    ๐ŸŽ‰ Lesson Complete!

    You've mastered the daily Git workflow! Next, we'll learn branching โ€” how to work on features in isolation.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service