What Is Git Stash?
Git stash is a built‑in Git utility that temporarily saves modifications in the working directory and index to a stack‑like storage area called the stash. The saved changes are removed from the working tree, allowing you to switch branches or work on other tasks without committing incomplete work.
Why Use Git Stash?
- Maintain a clean commit history – keep unfinished work out of the main branch.
- Context switching – quickly move to urgent bug fixes or code reviews without losing current changes.
- Dependency management – work on feature B, discover you need feature A first, stash B, implement A, then restore B.
- Safety net – untracked or staged changes can be saved and retrieved later.
How to Use Git Stash
The following commands cover the most common stash operations.
- Pushing changes to the stash
git stash push– saves all tracked modifications and resets the working directory to the last commit.git stash push -m "message"– adds a descriptive message.git stash push -uor--include-untracked– also saves untracked files.git stash push -sor--staged– saves only staged changes.
- Listing stash entries
git stash list– shows the stack with index numbers and messages.
- Applying or popping changes
git stash apply [– restores changes without removing the stash entry.] git stash pop [– restores changes and removes the entry (pop).]
- Viewing stash details
git stash show– summary of changes for the latest stash.git stash show -p– full diff.
- Dropping or clearing stashes
git stash drop– deletes a specific entry.git stash clear– removes all entries.
- Creating a branch from a stash
git stash branch– creates and checks out a new branch starting from the commit where the stash was made, then applies the stash.[ ]