Git
Version Control
Coding
Software Development
Data Recovery

How to recover stashed uncommitted changes

Interview Questions practice on Codemia

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

Browse interview questions

When working with version control systems like Git, it's common to use the stash feature to temporarily store uncommitted changes and clean your working directory. This can be particularly useful when you need to switch contexts quickly, say from fixing a bug to updating a feature. However, what do you do when you're ready to continue where you left off, or when you accidentally stash changes you weren't ready to set aside? This article will guide you through the process of recovering stashed, uncommitted changes in Git, including some tips on how to manage multiple stashes efficiently.

Understanding Git Stash

Git stash temporarily shelves changes you've made to your working directory so you can work on something else, and then come back and re-apply them later on. The stashed changes are stored on a stack, meaning you can stash multiple changesets and Git keeps them in order where the last stash you made is on top.

Creating a Stash

To stash changes, you use the git stash command or its variations:

bash
git stash save "My stash message"

This command takes all the modifications (staged and unstaged changes, but not new files or files removed) and saves them into a stash.

Listing Stashes

You can list all your current stashes with:

bash
git stash list

This will show you a list, each line representing a stash, prefixed with stash index, starting from stash@{0}.

Recovering Stashes

To bring back your stashed changes, there are several commands depending on your need:

Pop vs Apply

  • git stash pop: This command re-applies the most recently stashed changes back to your working directory and then removes the stash from the stack.
  • git stash apply: Similar to pop, but it leaves the stash in your stash list for possible future reuse.

Specific Stash Recovery

If you have multiple entries in your stash list, you can specify which stash to apply:

bash
git stash apply stash@{2}

This command applies the third latest stash (as the index starts at 0).

Handling Conflicts

Sometimes, when you reapply a stash, it can conflict with changes made since the stash was created. Git handles these conflicts similarly to a merge conflict. You must manually resolve these conflicts and then commit the resolved changes.

Best Practices for Using Stash

  • Use descriptive messages when stashing. This helps in identifying the correct stash to apply later.
  • Regularly clean up your stash list to avoid an unmaintainable pile-up.

Cleaning up

To remove a specific stash after applying it or if it's no longer needed:

bash
git stash drop stash@{1}

To clear all stashes:

bash
git stash clear

Summary

Here is a table summarizing the main Git stash commands and their uses:

CommandDescription
git stash save "msg"Creates a stash with a message for future reference.
git stash listLists all stashes.
git stash applyApplies a stash but keeps it in the stash list.
git stash popApplies a stash and removes it from the list.
git stash dropRemoves a specific stash from the list.
git stash clearRemoves all stashes.

Additional Tips

  • If you are frequently stashing, consider whether a feature branch would be more appropriate for managing multiple lines of development.
  • Regularly review your stashed changes, as it’s easy to forget the context of older stashes.

Using stashes effectively can greatly enhance your workflow with Git, allowing you to switch between tasks swiftly and keep your work organized without committing half-done work.


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.