Git

How to Recover Stashed Uncommitted Changes

Most of the time, developers make changes to the specific Git local branch and want to switch to another branch without saving changes into the repository. To do so, they generate stashes that temporarily hold the staging area modifications. After completing work on other branches, they want to switch back and recover the stashed uncommitted changes. In this situation, executing the “$ git stash pop” command is required.

In this article, we will briefly discuss recovering stashed uncommitted changes.

How to Recover Stashed Uncommitted Changes?

To recover stashed uncommitted changes, follow the below steps:

  • Move to the Git particular repository.
  • Make and track new text file.
  • Commit added changes to the repository for saving purposes.
  • Open and update the existing text file.
  • Generate a stash to hold the changes temporarily.
  • Use the “$ git stash pop” command for recovering stashed uncommitted changes.

Let’s check the implementation of the given scenario!

Step 1: Go to Git Repository
Run the “cd” command with the desired repository path and navigate to it:

$ cd "C:\Users\nazma\Git\Test_10"

Step 2: Generate New File
To make a new text file, use the “touch” command and specify the new file name:

$ touch file1.txt

Step 3: Track Generated Files
Next, add the file to the staging index by executing the provided command:

$ git add file1.txt

Step 4: Commit Changes
Next, save all added changes into the repository through the “git commit” command along with the commit message by using the “-m” option:

$ git commit -m "1 file added"

Step 5: Update Existing File
Now, run the ”start” command to update the existing file:

$ start file1.txt

After executing the above-stated command, the file will be open with a default text editor, add changes, save, and close the file:

Step 6: Add Modification Into Staging Area
Then, add all the added changes into the staging area by utilizing the “git add” command:

$ git add file1.txt

Step 7: Temporarily Save Changes
Next, save the working directory and index temporarily by running the “git stash” command:

$ git stash

Step 8: View Repository Status
Now, execute the “git status .” command to check the current repository status:

$ git status .

Step 9: Recover Stashed Uncommitted Changes
Finally, to recover the stashed uncommitted changes, run the “git stash pop” command:

$ git stash pop

As you can see, the modified “file1.txt” file is recovered successfully:

Step 10: Verify Recovered Uncommitted Changes
Lastly, check the repository’s current status to ensure the recovered uncommitted changes:

$ git status .

We have provided the easiest way to recover stashed uncommitted changes.

Conclusion

To recover stashed uncommitted changes, go to Git particular repository. Generate and add a new text file. Then, commit to the repository for saving purposes. Next, update the existing file and add changes to the staging. Generate a stash to hold the changes temporarily. Finally, run the “$ git stash pop” command to recover stashed uncommitted changes. This article explained the procedure of recovering stashed uncommitted changes.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.