Skip to content

Git Stash Workflow

Easy ~25 min

What you'll build

A practical workflow that uses stash to context-switch between branches without committing half-done work.

Goals

  • Save uncommitted changes with git stash
  • List, apply, and drop stashes
  • Stash specific files with --keep-index
  • Pop a stash and resolve conflicts

Prerequisites

  • Basic Git commits and branches
  • Working directory vs staging area
  • Command line comfort

Steps

  1. 1

    Create uncommitted work

    On a feature branch, make some changes to a file but don't commit them. This simulates being mid-task when you need to switch context.

    Code
    git checkout -b feature/work
    echo 'half-done change' >> app.js
    # Now you get a bug report on main and must switch immediately

    Expected result

    app.js has uncommitted changes; git status shows 'modified: app.js'.

  2. 2

    Stash the changes

    Run git stash to save your changes and restore the working directory to HEAD. Your work is saved but not committed, and the branch is clean.

    Code
    git stash
    git status  # working tree clean

    Expected result

    Working directory is clean; git stash list shows one entry.

  3. 3

    Switch context and fix the bug

    Now that your branch is clean, you can switch to main, fix the bug, commit, and switch back. Without stash, this switch would fail or carry your changes over.

    Code
    git checkout main
    echo 'bug fix' >> bug.js && git add . && git commit -m 'Fix urgent bug'
    git checkout feature/work

    Expected result

    You fixed the bug on main and returned to your feature branch.

  4. 4

    Pop the stash to resume work

    Run git stash pop to restore your saved changes. If there are conflicts (because main changed the same lines), resolve them before continuing.

    Code
    git stash pop
    # If conflict:
    #   edit the conflicted file
    #   git add <file>
    #   git stash drop  (the pop already removed it from the list on success)

    Expected result

    Your half-done changes are back in the working directory.

  5. 5

    Manage multiple stashes

    Stash a second set of changes, then use git stash list to see both. Apply a specific stash with git stash apply stash@{1} and drop it with git stash drop stash@{1}.

    Code
    echo 'more changes' >> other.js && git stash
    git stash list
    # stash@{0}: WIP on feature/work: more changes
    # stash@{1}: WIP on feature/work: half-done change
    
    git stash apply stash@{1}  # restore the first without removing it
    git stash drop stash@{1}   # now remove it from the list

    Expected result

    You can selectively apply and remove stashes from the list.

Try it yourself

What you learned

You mastered the stash workflow — the standard way to context-switch in Git without committing half-done work or losing changes.

Related Resources

Related Lessons

  • Branching

    Stash enables clean branch switching.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic