← Back to Git Course | Chapter 2: Basic Commands | Lesson 10 of 18

Git stash Command

What is Git Stash?

git stash takes your uncommitted changes (staged and unstaged) and stores them on a separate stack, resetting your working directory to match the last commit. This is the quickest way to switch tasks — check out another branch, fix an urgent bug — without committing half-finished work just to clear your tree.

Example: What is Git Stash?

bash
git stash

Listing and Viewing Stashes

git stash list shows every stash you've saved, most recent first, and git stash show -p stash@{n} lets you view the actual diff inside a specific one before deciding whether to bring it back. This matters once you have more than one stash sitting around and can't remember which is which.

Example: Listing and Viewing Stashes

bash
git stash list
git stash show -p stash@{0}

Applying and Popping Stash

git stash apply reapplies a stash's changes to your working directory but leaves it in the stash list for later reuse — handy if you want the same changes on multiple branches. git stash pop does the same but also removes it from the list, which is what you want for the common one-time case.

Example: Applying and Popping Stash

bash
git stash apply
git stash pop

Stashing Specific Files

You're not limited to stashing everything at once — git stash push -- <file> stashes only specific files, and git stash push --patch lets you interactively choose individual hunks, useful when only part of your working changes are ready to be set aside.

Example: Stashing Specific Files

bash
git stash push -- file.txt

Cleaning the Stash

Old stashes just sit there taking up space and cluttering the list if you never clean them up. git stash drop stash@{n} removes one specific entry, while git stash clear wipes the entire stash list — the latter is irreversible, so double-check you don't need any of them first.

Example: Cleaning the Stash

bash
git stash drop stash@{0}
git stash clear

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.