Feedback

Chat Icon

Learn Git in a Day

Everything you need, nothing you don't

Oops - How to Undo Anything
29%

Put Your Work on Pause

Sometimes you're in the middle of working on something, but you need to switch to a different task quickly like fixing a bug in another branch, or maybe you just want to save your progress without committing it yet. You don't want to commit half-finished work, but you also don't want to lose what you've done.

This is where git stash comes in.

A stash takes your uncommitted changes, saves them aside, and gives you a clean working directory. Later, you can bring those changes back.

  Working Directory                          Stash
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    git stash       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚  uncommitted     │──────────────────> β”‚  saved       β”‚
 β”‚  changes         β”‚                    β”‚  changes     β”‚
 β”‚                  β”‚ <──────────────────│              β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    git stash pop   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       (clean)

Let's simulate this. Add a division function but don't commit it:

cat << 'EOF' > calculator.py
# calculator.py - A simple calculator

def add(a, b):
    """Add two numbers and return the result."""
    return a + b

def subtract(a, b):
    """Subtract b from a and return the result."""
    return a - b

def multiply(a, b):
    """Multiply two numbers and return the result."""
    return a * b

def divide(a, b):
    """Divide a by b and return the result."""
    if b == 0:
        return "Error: Cannot divide by zero"
    return a / b

# Try it out
result_add = add(5, 3)
result_sub = subtract(10, 4)

Learn Git in a Day

Everything you need, nothing you don't

Enroll now to unlock all content and receive all future updates for free.

Unlock now Β $9.99$7.49

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More