git
version control
stash recovery
lost changes
developer tips

How do I recover a dropped stash in Git?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Understanding Git Stashes

When working with Git, one common task developers might need to perform is temporarily saving changes that are not yet ready to be committed. This is efficiently managed by the git stash command. Simply put, a stash allows you to put away your current changes so you can work on something else, and then bring those changes back when you're ready.

What Happens When You Drop a Stash?

A common concern arises when you accidentally drop a stash — using the command git stash drop or git stash pop (which applies a stash and then drops it). When a stash is dropped, it is removed from the stash list, making it appear as if it's lost. However, it may still be recoverable, provided that you act promptly.

Recovering a Dropped Stash

Step 1: Identify the Lost Stash

When you drop a stash, it’s crucial to first identify the SHA-1 hash of the dropped stash. Git will not immediately remove the stash from your repository's history. Instead, it lingers in your reflog for some time.

To view your reflog, you can use the following command:

bash
$ git reflog

Your reflog consists of all your local reference updates. By searching through it, you can find the commit hash related to your stash entries.

Step 2: Restoring the Stash

Once you’ve identified the appropriate hash, you can use the following command to create a new branch from the stash:

bash
$ git checkout -b <new-branch-name> <commit-hash>

Alternatively, you can apply the stash directly using:

bash
$ git stash apply <commit-hash>

Detailed Explanation with Example

Let's take an example to see this in action:

  1. Assume you stashed your changes:
bash
   $ git stash

You can verify your stash with:

bash
   $ git stash list
  1. You then accidentally drop it:
bash
   $ git stash drop
  1. Find the Stash in Reflog:
bash
   $ git reflog

You might see output similar to this:

 
   ecfb123 HEAD@{0}: checkout: moving from master to feature-branch
   3e94567 HEAD@{1}: stash: saving work in progress

Here, 3e94567 is the hash for your stash.

  1. Recover the Stash:
    You can either apply the stash:
bash
   $ git stash apply 3e94567

Or, create a new branch to continue your work:

bash
   $ git checkout -b recover-stash 3e94567

Best Practices for Using Git Stashes

  1. Naming Stashes:
    • Consider using git stash save "meaningful-message" instead of the plain git stash. This way, each stash will be associated with a helpful context, making it easier to identify later.
  2. Understanding Stash Index:
    • Stashes have indices like stash@&#123;0&#125;, and using git stash show provides a preview of changes.
  3. Avoid Over-relying on Stashes:
    • Stashes are best for temporary interruptions. For longer-term work, consider creating a branch.

Table Summarizing Key Steps and Commands

ActionGit Command(s)Description
Save changes to a stashgit stashTemporarily saves your work.
List stashesgit stash listShows all stashed changes.
Drop the most recent stashgit stash dropDeletes the stash from the list.
Find a dropped stashgit reflogDisplays the reflog which may have the hash of your stash entry.
Apply/recover a stashgit stash apply <hash> git checkout -b <branch> <hash>Restores or creates a branch from a identified stash hash.

Additional Details

Cleaning Old Stashes

Stashes older than 90 days can be removed with:

bash
$ git stash clear

Limitations

Once a stash has been garbage-collected, recovery becomes virtually impossible without a backup.

Conclusion

Git stashing is a powerful tool for managing work-in-progress changes. If a stash is dropped, prompt action with the help of git reflog can facilitate recovery. Always remember to take note of your stash messages as a best practice, and lean towards branching for changes expected to last beyond an immediate context shift.

By understanding and following these guidelines, you'll be better equipped to handle Git's stash feature effectively, reducing accidental data loss and maximizing productivity in your development workflow.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.