DEV Community

ramadhan.dev
ramadhan.dev

Posted on • Edited on

git stash — Save Work-in-Progress Temporarily

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:

  1. 🔄 You need to switch branches, but have uncommitted changes you don't want to lose or commit yet.

  2. 🧪 You want to experiment, but first save your current work state.

  3. ⚠️ Git prevents checkout due to uncommitted changes.

  4. 💥 You need to temporarily undo changes for testing or debugging without losing your work.

🛠️ Basic Commands and Usage

🔐 Save changes to stash:

git stash
Enter fullscreen mode Exit fullscreen mode

To also include untracked (new) files:

git stash -u  # atau --include-untracked
Enter fullscreen mode Exit fullscreen mode

View list of stashes:

git stash list
Enter fullscreen mode Exit fullscreen mode

Example output:

stash@{0}: WIP on feature/login: 3fa2b2a Add login form
stash@{1}: WIP on main: 1a2b3c4 Fix typo
Enter fullscreen mode Exit fullscreen mode

📦 Apply a stash back to the working directory:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Apply a specific stash:

git stash apply stash@{1}
Enter fullscreen mode Exit fullscreen mode

🧹 Apply and remove stash from the list:

git stash pop
Enter fullscreen mode Exit fullscreen mode

❌ Delete a specific stash:

git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

🧼 Clear all stashes:

git stash clear
Enter fullscreen mode Exit fullscreen mode

✅ Simple Use Case Example

  1. You're working on the develop branch with some uncommitted changes.
  2. 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
Enter fullscreen mode Exit fullscreen mode

🏷️ Stash with Custom Message

✅ Format:

git stash push -m "Pesan custom kamu"
Enter fullscreen mode Exit fullscreen mode

🧾 Example:

git stash push -m "QA: fix login validation"
Enter fullscreen mode Exit fullscreen mode

To also include untracked files:

git stash push -u -m "WIP: registration feature"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)