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.
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:
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:
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:
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:
To clear all stashes:
Summary
Here is a table summarizing the main Git stash commands and their uses:
| Command | Description |
git stash save "msg" | Creates a stash with a message for future reference. |
git stash list | Lists all stashes. |
git stash apply | Applies a stash but keeps it in the stash list. |
git stash pop | Applies a stash and removes it from the list. |
git stash drop | Removes a specific stash from the list. |
git stash clear | Removes 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
- How to recover stashed uncommitted changes
- How to refer to relative paths of resources when working with a code repository
- How to remove a file from the staging area index cache in Git?
- How to remove all git origin and local tags?
- How to remove files from git staging area?
- How to remove files from git staging area?
- How to remove files that are listed in the .gitignore but still on the repository?
- How to remove origin from git repository
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.