Accidentally deleting or dropping a stash in Git can be stressful, especially if the stash contains important changes. Luckily however, Git provides mechanisms to recover these stashes under certain conditions. This guide will explore how to recover a Git stash,including how to recover deleted and dropped stashes.
Understanding Git stash recovery
Git stash operates on a LIFO (last in, first out) basis and is a temporary storage for changes that are not yet ready to be committed. If a stash is dropped or deleted, it's removed from the list of stashes but can often still be found in the repository's reflog, unless it has been pruned or garbage collected by Git.
Steps to recover a dropped or deleted stash
Step 1: Access the Git reflog
Git maintains a log of where your HEAD and branch references have been recently, which includes stashed changes. You can access this log with:
git reflog
Look through the output for entries related to stashing, such as "stash@{1}: WIP on...". Each entry is associated with a commit ID.
Step 2: Identify the lost stash
Once you find the entry corresponding to the dropped or deleted stash, note the commit ID associated with it. This ID is essential for the recovery process.
Step 3: Creating a new branch for recovery
To safely inspect and recover the stash, create a new branch pointing to the selected commit ID:
git branch recover-branch <commit-ID>
Switch to this new branch:
git checkout recover-branch
Step 4: Check the stash content
Before fully integrating the recovered stash into your project, inspect the changes:
git log -p
This command shows the diff of what was in the stash. Using the diff, examine these changes and confirm that these are the changes you want to recover.
Step 5: Apply the stash changes
If the changes are what you were looking for, you can apply them to your branch. Since you're on a dedicated recovery branch, you can merge these changes back into your main working branch or re-stash them:
git stash apply
Or, if you are ready to integrate these changes:
git checkout maingit merge recover-branch
Step 6: Clean up
Once recovery is successful and you've merged the changes back to your main branch, you can delete the temporary recovery branch:
git branch -d recover-branch
Best practices and tips
- Frequent backups: Regularly push your stashes to a remote repository or create patches as backups.
- Reflog housekeeping: Remember that the reflog is maintained for a default of 90 days in most configurations. Recover lost stashes within this period.
- Avoid garbage collection: Be cautious with commands like git prune that force garbage collection, as these may permanently delete stashes that could otherwise be recoverable.
Summary
Recovering a git stash involves using the reflog to find the lost stash's commit ID, creating a new branch from this commit, and then applying or merging the changes as needed. This process can help you retrieve valuable stashed changes that were accidentally dropped or deleted. Always ensure to keep regular backups of important stashes to mitigate the risks of loss.
For further reading, see this guide on stashing in Git.