git stash
temporarily saves your working directory changes (modified and added files not yet committed) and restores the working directory to the latest commit.
✅Use it when:
🔄 You need to switch branches, but have uncommitted changes you don't want to lose or commit yet.
🧪 You want to experiment, but first save your current work state.
⚠️ Git prevents checkout due to uncommitted changes.
💥 You need to temporarily undo changes for testing or debugging without losing your work.
🛠️ Basic Commands and Usage
🔐 Save changes to stash:
git stash
To also include untracked (new) files:
git stash -u # atau --include-untracked
View list of stashes:
git stash list
Example output:
stash@{0}: WIP on feature/login: 3fa2b2a Add login form
stash@{1}: WIP on main: 1a2b3c4 Fix typo
📦 Apply a stash back to the working directory:
git stash apply
Apply a specific stash:
git stash apply stash@{1}
🧹 Apply and remove stash from the list:
git stash pop
❌ Delete a specific stash:
git stash drop stash@{0}
🧼 Clear all stashes:
git stash clear
✅ Simple Use Case Example
- You're working on the
develop
branch with some uncommitted changes. - Suddenly you need to switch to the
main
branch to fix a bug.
git stash # Save current changes
git checkout main # Switch to main
# ... make the fix and commit
git checkout develop # Return to develop
git stash pop # Restore your saved changes
🏷️ Stash with Custom Message
✅ Format:
git stash push -m "Pesan custom kamu"
🧾 Example:
git stash push -m "QA: fix login validation"
To also include untracked files:
git stash push -u -m "WIP: registration feature"
Top comments (0)