Stash Pop Conflict
pop conflict pe stash nahi mitta. Apply zyada safe hai pop se. Aur yaad rakhna, stash short-term ke liye hai, long-term nahi!
git stash pop applies your stashed changes and removes them from the stash list. Simple, right? But what if the current branch has changed since you stashed?
A stash pop conflict occurs when the stash changes conflict with changes made on the current branch since the stash was created. Both modified the same lines, and Git does not know which version to keep.
When git stash pop conflicts, Git applies the changes but marks them as unresolved conflicts. The conflict markers (<<<<<<<, =======, >>>>>>>) appear in the affected files, just like a merge conflict.
IMPORTANT: On conflict, git stash pop does NOT remove the stash entry. It keeps it as a safety net. This is a critical Git behavior that saves you from data loss.
# Stash some work
echo "stash change" >> app.js
git stash push -m "my work"
# Make conflicting changes on the same branch
echo "branch change" >> app.js
git add app.js && git commit -m "branch work"
# Try to pop โ CONFLICT!
git stash pop
# CONFLICT (content): Merge conflict in app.js
# Check status
git status
# Unmerged paths:
# both modified: app.js
# The stash is NOT dropped on conflict
git stash list
# stash@{0}: On main: my work (still here!)
git stash pop only drops the stash when the apply succeeds. On conflict, the apply is incomplete, so Git keeps the stash. This is a safety net โ if you mess up the resolution, you still have the original stash to try again.Resolving a stash pop conflict is identical to resolving a merge conflict. Same markers, same process, same commands.
Step 1: Open the conflicted file and look for the conflict markers.
Step 2: <<<<<<< Updated upstream = your current branch version.
Step 3: >>>>>>> Stashed changes = the stashed version.
Step 4: Edit the file to resolve the conflict. Choose one side, the other, or combine both. Remove all conflict markers.
Step 5: git add <file> to mark the conflict as resolved.
Step 6: Since pop does not create a commit, you just need to add the resolved files. No commit needed unless you want to commit separately.
Step 7: git stash drop to remove the stash entry (since pop failed to do so on conflict).
# See the conflict
cat app.js
# <<<<<<< Updated upstream
# branch change
# =======
# stash change
# >>>>>>> Stashed changes
# Resolve: choose one, the other, or combine
echo "branch change and stash change" > app.js
# Stage the resolved file
git add app.js
# Drop the stash (since pop did not drop it on conflict)
git stash drop stash@{0}
# Verify
git status
# Changes to be committed: (or nothing if already clean)
git stash apply is the safer cousin of git stash pop. The difference is simple but critical.
pop = apply + drop (if successful). If the apply fails with a conflict, the drop does not happen.
apply = apply only, always keep the stash. Whether it succeeds or conflicts, the stash stays in the list.
With apply, you explicitly decide when to drop the stash after verifying the result. This gives you a safety net โ you can always re-apply if something goes wrong.
Best practice: Use apply for important work. Use pop only for trivial stashes you are sure will not conflict.
# Safer: use apply instead of pop
git stash apply stash@{0}
# If conflict:
# Resolve, git add, then manually drop
git stash drop stash@{0}
# If no conflict:
# Verify the result first
git status
# Everything looks good? Now drop it.
git stash drop stash@{0}
# Apply a specific stash
git stash apply stash@{2}
git stash apply instead of git stash pop. Verify the result, then drop the stash manually. It takes one extra command but eliminates the risk of losing your stashed work.If you know your stash will conflict with the current branch, there is a clean escape: git stash branch.
git stash branch <branchname> does two things:
- Creates a new branch at the commit where the stash was originally created.
- Applies the stash changes to the new branch.
Since the branch is created at the stash parent commit, the apply is guaranteed to be conflict-free. The stash changes are being applied to the exact state they were created from.
After creating the branch, you can commit your stashed work and then merge or rebase it back to your target branch.
This is the cleanest way to recover a long-forgotten stash that will definitely conflict.
# Instead of risky pop/apply, create a branch
git stash branch stash-recovery
# This creates a new branch "stash-recovery" at the stash parent
# AND applies the stash automatically (no conflict!)
# Your stashed changes are now in the working directory of the new branch
git status
# Changes to be committed: (stash changes are staged)
# Commit the stash work
git commit -m "feat: work from old stash"
# Now merge this branch back to main (resolve any conflicts in the merge)
git checkout main
git merge stash-recovery
git stash branch also drops the stash entry automatically after creating the branch and applying it. You do not need to manually drop the stash afterward.The best way to handle stash conflicts is to prevent them entirely. Here is how:
The longer a stash sits, the more likely it is to conflict when you pop it. This is the single most important thing to remember about stash management.
- Do not use stash as long-term storage. Stash for short breaks (minutes to hours), commit for long-term (days to weeks).
- Always specify a message:
git stash push -m "descriptive message". Without a message, you get a generic one based on the last commit, making it hard to find the right stash later. - Before popping, check what is in the stash:
git stash show -p stash@{0}. This shows the full diff of what the stash will apply. - If you are switching branches, commit your work on a feature branch instead of stashing. A branch is a permanent, named reference. A stash is a temporary, numbered entry.
Stash is a temporary shelf, not a permanent closet. Put something there for a few minutes, not a few days.
# Prevention 1: Always add a message
git stash push -m "WIP: login feature"
# Prevention 2: Preview before popping
git stash show -p stash@{0}
# Shows the diff of what the stash will apply
# Prevention 3: Commit instead of stash for long-term
git checkout -b feature/login-wip
git add -A
git commit -m "WIP: login feature"
# Safer than stashing for days
# Prevention 4: Keep stashes short-lived
# Good: stash -> fix bug -> pop (minutes later)
# Bad: stash -> work on 3 other features -> pop (days later)
Lo kar liya โ Key Points:
git stash popcan cause conflicts if the current branch has changed since the stash was created- On conflict,
git stash popdoes NOT drop the stash โ it keeps it as a safety net - Resolve stash conflicts like merge conflicts: edit file, remove markers,
git add, thengit stash drop git stash applyis safer thanpopbecause it always keeps the stash, letting you verify before droppinggit stash branch <name>creates a new branch from the stash, avoiding conflicts entirely- Don't use stash as long-term storage โ the longer a stash sits, the more likely it will conflict
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login