Books/Git Essentials/Tracking Changes

    Tracking Changes

    Tracking Changes

    This is the heart of Git — the daily workflow you'll use constantly. It boils down to three commands: git status, git add, and git commit.

    The Three Areas

    Git has three areas where your files can live:

    Working Directory  →  Staging Area  →  Repository
       (your files)       (ready to save)   (saved history)
    

    Think of it like packing a box to ship:

    1. Working Directory: Your desk with all your stuff
    2. Staging Area: The box you're packing (choosing what to include)
    3. Repository: The shipped package (saved permanently)

    Step 1: Check What Changed — git status

    Always start by seeing where things stand:

    git status

    Git will show files in three categories:

    • Untracked files (red) — New files Git hasn't seen before
    • Changes not staged (red) — Modified files not yet added to the staging area
    • Changes to be committed (green) — Files ready to be saved in the next commit

    Step 2: Stage Your Changes — git add

    Move files from your working directory to the staging area:

    # Add a specific file
    git add README.md
    
    # Add multiple specific files
    git add README.md app.js
    
    # Add all changed files
    git add .

    After staging, run git status again to confirm:

    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    	new file:   README.md
    	new file:   app.js
    

    The files are now green — they're staged and ready to be committed.

    Why a Staging Area?

    The staging area lets you choose what goes into each commit. You might have changed 5 files but only want to save 3 of them right now. The staging area gives you that control.

    What to ask your AI: "I changed several files but only want to commit some of them. Can you help me stage just the files related to [feature]?"

    Step 3: Save a Snapshot — git commit

    Create a commit (save a snapshot) with a message describing what you did:

    git commit -m "Add README and app.js with initial setup"

    Output:

    [main (root-commit) a1b2c3d] Add README and app.js with initial setup
     2 files changed, 3 insertions(+)
     create mode 100644 README.md
     create mode 100644 app.js
    

    Writing Good Commit Messages

    Your commit message should answer: "What does this change do?"

    GoodBad
    "Add user login page""Update stuff"
    "Fix navigation links on mobile""Fix bug"
    "Remove unused API endpoint""Changes"

    Tips:

    • Start with a verb: Add, Fix, Update, Remove, Refactor
    • Keep it short (under 72 characters)
    • Be specific about what changed

    What to ask your AI: "I made these changes: [describe changes]. Can you suggest a good commit message?"

    The Full Workflow

    Here's the workflow you'll repeat every time:

    # 1. Make changes to your files (edit code, create files, etc.)
    
    # 2. See what changed
    git status
    
    # 3. Stage the changes you want to save
    git add .
    
    # 4. Commit with a message
    git commit -m "Describe what you did"

    Seeing What Changed — git diff

    Before committing, you might want to see the exact changes in your files:

    # See changes in your working directory (not yet staged)
    git diff
    
    # See changes that are staged (ready to commit)
    git diff --staged

    This shows you line-by-line what was added (+ in green) and removed (- in red).

    What to ask your AI: "I ran git diff and see these changes: [paste output]. Can you review them and tell me if anything looks wrong?"

    Viewing Your History — git log

    After making commits, you can see your project's history:

    # Full log
    git log
    
    # Compact one-line format
    git log --oneline

    Example output:

    a1b2c3d Add search functionality
    f4e5d6c Fix header alignment on mobile
    b7a8c9d Initial project setup
    

    Each line shows a commit hash (the ID) and the commit message. This is your project's timeline.

    Undoing Mistakes

    Made a mistake? Here's how to fix it:

    Unstage a file (remove from staging area):

    git restore --staged filename.txt

    Discard changes to a file (go back to last commit):

    git restore filename.txt

    Warning: git restore filename.txt permanently discards your uncommitted changes to that file.

    What to ask your AI: "I accidentally committed the wrong files. How do I undo my last commit without losing my changes?"

    Putting It All Together

    Here's a realistic example of a coding session:

    # Start working — check where you are
    git status
    
    # Edit some files...
    # Then check what you changed
    git status
    
    # Review the actual changes
    git diff
    
    # Stage everything
    git add .
    
    # Commit
    git commit -m "Add contact form with email validation"
    
    # Verify the commit was saved
    git log --oneline

    What's Next?

    You now know the core Git workflow! The next tutorial introduces branches — Git's powerful feature for working on multiple things at once without breaking your main code.


    🌐 www.genai-mentor.ai