git stash apply version
Categories:
Mastering Git Stash: Applying Specific Stashes and Handling Conflicts

Learn how to effectively use git stash apply
to reintroduce stashed changes, manage multiple stashes, and resolve common conflicts.
Git stash is an incredibly useful command for temporarily saving changes that are not ready to be committed. It allows you to quickly switch contexts without committing half-finished work. While git stash apply
is straightforward for the most recent stash, understanding how to apply specific stashes and gracefully handle conflicts is crucial for efficient workflow.
Understanding Git Stash Basics
When you run git stash
, Git takes your modified tracked files and staged changes, saves them on a stack of unfinished changes, and then reverts your working directory to match the HEAD
commit. This creates a 'stash entry' that you can later reapply. Each stash entry is indexed, starting from stash@{0}
for the most recent one.
git stash save "Work in progress: Feature X"
git stash list
Saving a stash with a message and listing existing stashes.
Applying a Specific Stash
By default, git stash apply
will reapply the most recent stash (stash@{0}
). However, if you have multiple stashes, you might need to apply an older one. You can specify which stash to apply by providing its identifier as an argument.
# Apply the most recent stash
git stash apply
# Apply a specific stash (e.g., the second oldest one)
git stash apply stash@{1}
Applying the most recent or a specific stash entry.
git stash apply
keeps the stash entry in your stash list, allowing you to reapply it multiple times if needed. If you want to apply and then remove the stash from the list, use git stash pop
instead. However, pop
also defaults to stash@{0}
and can lead to issues if you're not careful with older stashes.Handling Merge Conflicts During Stash Application
Just like with merging or rebasing, applying a stash can lead to conflicts if the changes in the stash overlap with changes made in your working directory since the stash was created. Git will pause the application process and mark the conflicting files. You'll need to resolve these conflicts manually.
flowchart TD A[Start: Working Directory Clean] --> B{Make Changes}; B --> C[git stash save]; C --> D[Switch Branches / Make New Commits]; D --> E{git stash apply stash@{N}}; E --> F{Conflicts?}; F -- Yes --> G[Resolve Conflicts]; G --> H[git add .]; H --> I[git stash drop stash@{N} (Optional)]; F -- No --> I; I --> J[End: Stash Applied];
Workflow for applying a stash and resolving conflicts.
1. Identify Conflicts
After git stash apply
, Git will report any conflicts. Use git status
to see which files are in a conflicted state.
2. Resolve Conflicts
Open the conflicted files in your editor. Git will mark the conflicting sections with <<<<<<<
, =======
, and >>>>>>>
. Manually edit the file to combine the desired changes from both the stash and your current working directory.
3. Stage Resolved Files
Once you've resolved the conflicts in a file, stage it using git add <filename>
.
4. Complete the Stash Application
After all conflicts are resolved and staged, the git stash apply
operation is considered complete. You can then commit your changes as usual. If you used git stash apply
, the stash entry remains. If you used git stash pop
, it would have been removed automatically upon successful application (or left if conflicts occurred).
5. Clean Up Stash (Optional)
If you used git stash apply
and no longer need the stash entry, you can remove it with git stash drop stash@{N}
.