Stash In Git Using Visual Studio 2019

Introduction

 
Stash in Git is really useful if we have accidentally made some unwanted changes in a branch, especially when the change we made is unnecessary currently but can be useful in the future. Or, if we have made code changes to branch A while it should be in branch B. Using stash, we can easily move the changes from branch A to B. In this article, we are going to see how to work with Stash in Git using Visual Studio 2019.
 
Tooling
 
Visual Studio 2019
 

Stash in Git

 
I opened a new ASP.NET MVC project using Visual Studio 2019.
 
I added the project to the source control as shown in the below figure and it is published in one of my Azure DevOps project repositories.
 
Stash in Git using Visual Studio 2019
 
Stash in Git using Visual Studio 2019
 
Right now, I’m in the master branch and I added some comments in the About function, as shown in the below figure.
 
Stash in Git using Visual Studio 2019
 
After the changes, I realize this is not required for this current commit but maybe I need these changes in the future. So, somewhere I need to maintain these changes. This is where stash will come into the picture. In Team >> Explore changes section, you can find the Stash option, as shown in the below figure.
 
Stash in Git using Visual Studio 2019
 
Select "Stash". It will keep all the changes in the stash and revert the current change from the source as shown in the below figure. 
 
Stash in Git using Visual Studio 2019
 
Later, we can add these changes in the branch from stashes, as shown in the below figure.
 
Right-click on the stash -> Apply-> "Apply and Restore Staged" which will apply the changes in the current branch.
 
The stash which is done is one branch can also be applied to another branch, as shown in the below figure.
 
Stash in Git using Visual Studio 2019
 
From the above figure, you can notice the stash is done and the develop branch is applied to the master branch.
 

Summary

We saw what stash is and how to use it with Git in Visual Studio 2019.