BudiBadu Logo
Samplebadu

Git by Example: Stash Apply

2.43.0

Learn how to reapply stashed changes to your working directory without removing them from the stash list. This is useful for applying the same changes to multiple branches.

Code

# 1. List available stashes
git stash list

# Output:
# stash@{0}: WIP on main: 1a2b3c Fix login bug
# stash@{1}: WIP on feature: 4d5e6f Add user profile

# 2. Apply the most recent stash (stash@{0})
git stash apply

# 3. Apply a specific stash
git stash apply stash@{1}

# 4. Verify the stash is still in the list
git stash list

# Output:
# stash@{0}: WIP on main: 1a2b3c Fix login bug
# stash@{1}: WIP on feature: 4d5e6f Add user profile

Explanation

The git stash apply command takes the changes stored in a stash entry and reapplies them to your current working directory. The key characteristic of "apply" is that it does not remove the stash from your list after applying it. The stash entry remains available for future use.

This behavior makes git stash apply incredibly flexible. You can use it to apply the same set of changes to multiple different branches. for example, if you fixed a bug on one branch and want to test that same fix on another branch without committing it yet, you can stash it, switch branches, and apply it again.

Because the stash isn't automatically dropped, you might end up with a cluttered stash list if you don't manage it manually. If you decide you no longer need the stash after applying it, you must run git stash drop separately. This extra step provides safety, ensuring you don't accidentally lose your work if the application process encounters conflicts.

  • Reapplies changes to working directory
  • Keeps the stash in the list
  • Allows applying changes to multiple branches
  • Requires manual cleanup with git stash drop

Code Breakdown

10
Applies the changes from the top of the stack (stash@{0}). Your working directory now contains those modifications.
13
Applies a specific stash from the history. This is useful if you have multiple saved states and want to pick a specific one.
16
Checking the list confirms that the stashes are still there, unchanged.