How Do I Recover a Dropped Stash in Git?

Better Stack Team
Updated on June 24, 2024

Recovering a dropped stash in Git involves identifying the commit associated with the dropped stash and creating a new branch or applying the stash directly. Here's how you can do it:

  1. Identify the Dropped Stash: First, you need to find the commit associated with the dropped stash. You can do this by inspecting the reflog:

    git reflog

    Look for entries that mention "stash" or "drop" to find the commit where the stash was dropped. Each entry in the reflog includes a commit hash associated with a stash action.

  2. Recover the Stash: Once you've identified the commit hash associated with the dropped stash, you can either create a new branch at that commit or apply the stash directly.

    Option 1: Create a New Branch: Create a new branch at the commit where the stash was dropped:

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

    Replace <new-branch-name> with the desired name for the new branch, and <commit-hash> with the hash of the commit where the stash was dropped.

    Option 2: Apply the Stash: If you want to apply the dropped stash directly to your working directory, you can use the git stash apply command with the commit hash of the dropped stash:

    git stash apply <commit-hash>

    Replace <commit-hash> with the hash of the commit where the stash was dropped.

  3. Review and Resolve Conflicts (if any): After recovering the stash, review the changes and resolve any conflicts that may arise. Use git status to check the status of your working directory and git diff to inspect changes.

  4. Commit Changes (if necessary): Once you've resolved any conflicts and verified the changes, you can commit them to the repository:

    git add .
    git commit -m "Recover dropped stash"
  5. Push Changes (if necessary): If you're working with a remote repository and you want to push the recovered changes, use the git push command:

    git push origin <branch-name>

    Replace <branch-name> with the name of the branch you're working on.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github