Tech dobz

preparing a tiny bit of magic...

    Overview

    Fixing Code Without the Chaos — Git Stash Explained

    Understand when to stash, when to commit — and how to keep your Git history neat.

    Problem

    You're working on a new feature (without using a separate branch), and you realize there's a bug in your existing code. Now what?

    • Comment out your in-progress changes?
    • Commit half-finished work just to fix the bug?

    Neither option is ideal. Fortunately, there's a better approach.

    Solution

    Use git stash to temporarily hide your current changes. Then fix the bug and bring your changes back cleanly.

    Command

    git stash
    # Hides all your uncommitted changes (Work in Progress)
    

    Fix the bug and commit the fix:

    git commit -am "Fix bug in processing function"
    

    Bring your changes back:

    git stash pop
    

    Why This Works

    • git stash saves both staged and unstaged changes
    • It doesn't create a commit — won't appear in git history
    • It's local only — doesn't affect collaborators

    Real Example

    import time
    
    def debug(data_length=10):
        for data_index, data in enumerate(range(data_length), start=1):
            print(f'[+] Processing Data At : {data_index}')
    
            # Bug: Issue appears at index > 5
            if data_index > 5:
                raise Exception('System Broken!')
    
            time.sleep(1)
    

    You commit this version:

    git add debug.py
    git commit -m "Data processing function added!"
    

    Now you begin work on a new feature:

    def processing_debug(data):
        # New feature in progress
        pass
    

    Then you realize there's a bug in debug(). You stash your changes:

    git stash push -m "New Processing Feature Work"
    

    Now your file reverts to the clean, committed version:

    import time
    
    def debug(data_length=10):
        for data_index, data in enumerate(range(data_length), start=1):
            print(f'[+] Processing Data At : {data_index}')
    
            # Bug handled safely
            if data_index > 5:
                pass
    
            time.sleep(1)
    

    You fix the bug:

    git add debug.py
    git commit -m "Fixed data processing bug"
    

    Then restore your feature:

    git stash pop
    

    Bonus

    • List all stashes:
      git stash list
    • Inspect contents of a stash:
      git stash show -p