The Git Stash Functionality in Visual Studio 2019

If you have looked already at the Changes page of Team Explorer in Visual Studio 2019, you might have seen that it has a new Stashes section. It is shown in the screenshot below. But for what is that Stashes section?

Let’s say you are working on a feature. But you’re not ready to commit it, as you didn’t reach yet the code quality you normally write. But you want to save the functionality for now and look at the original branch without changes. To do this, you can stash the changes.

Just enter a message in the Changes window of Team Explorer. The message is optional for a stash, I entered in the screenshot below the message “First draft of customer login”. Now click on the Stash button:

From the drop down, click on “Stash All”. This will create a stash that contains all the uncommitted changes. So, a stash is like a local copy of your uncommitted changes. Clicking on “Stash All” will also undo all those uncommitted changes in your branch. If you want to keep the changes in the branch that you have already staged for a commit, you select “Stash All and Keep Staged”. But in my case above, I haven’t staged the Program.cs file. So both stash options will have the same effect. Let’s select “Stash All”.

After clicking on “Stash all”, you can see now the created stash, and the branch doesn’t have any changes anymore.

Now you can switch for example to another branch. Later you can come back to your feature branch, and there you can right-click on that stash in the Changes page of Team Explorer. From the context menu that you get then (you can see it below) you can view the changes. Instead of using the context menu to view the changes, you can also double-click the stash.

As you can see in the picture above, beside the “View Changes” menu the context menu of the stash has also the following options:

  • Apply – this will apply the changes to the branch and it will keep the stash
  • Drop – this will delete the stash
  • Pop – this will Apply the changes and Drop the stash

Under the menu items Apply and Pop you find two options. You can see them in the screenshot above for the Pop menu:

  • Pop/Apply and Restore Staged – this will pop/apply the changes to your branch and it will also ensure that files that you had staged before are staged again
  • Pop/Apply All as Unstaged – this will pop/apply the changes to your branch and it won’t stage the files that you had staged before.

If you hadn’t any staged files before you created the stash, then you won’t see a difference between the two options. So let’s select in my case “Pop All as Unstaged”, and then we’re back at the state that we had at the beginning of this blog post. A change in the Program.cs file:

So, stashing is great for storing changes that you do not want to commit yet.

Another thing where stashing is quite useful is when you want to switch to another branch. This works without a problem if the other branch points to the same commit as the branch that you’ve checked out. But if the other branch doesn’t point to the same commit, you have to commit your local changes first to checkout the other branch. But what if you are not ready yet to commit? Instead of undoing or committing your changes, you can also stash them, and this is what Visual Studio actually suggests when you try to checkout another branch and the other branch points to another commit and you have made local changes that you haven’t committed yet (yes, I know, a lot of conditions in this sentence. :-)).

Let’s see this in action. As you saw in the screenshot above, I have made changes to the Program.cs file. In the picture below you see the branches page of Team Explorer. The checked out branch feature/customerLogin has local changes, and it also does not point to the same commit as the master branch (You can’t see that in the screenshot, so just believe me :)). So let’s see what happens when we try to checkout the master branch by using the context menu like below:

Visual Studio notices the problem and shows this window:

In this window you can do a force checkout, which means Visual Studio will undo the local changes and checkout the master branch. That means your uncommitted changes will be lost. But Visual Studio shows you another dialog after clicking on “Force Checkout” to ensure that you’re really sure to undo your local changes.

Instead of doing a force checkout, you can also click on “Stash and Continue”. This will stash your uncommitted changes from the feature branch, it will checkout the other branch, which is in this sample the master branch. Then it will apply the changes from the stash to the master branch. If you don’t want to apply the changes to the master branch, you should click “Cancel” in this dialog. Then create a stash manually like shown in this blog post, and after that you can checkout the master branch smoothly, as you won’t have any uncommitted local changes anymore after you created the stash.

Btw., when applying a stash, you can get conflicts in a way like when merging a branch. You can solve these conflicts exactly with the same options that you know from merging branches. You have options like merge, take theirs, or take yours.

I think for most developers branching is enough. But stashing is a powerful and popular git feature to store some local changes that you don’t want to commit yet. And now we have it integrated in Visual Studio.

If you want to learn more how to use Git in Visual Studio 2019, check out my Pluralsight course Using Git for Source Control in Visual Studio 2019. It shows you how to commit, push, and pull changes, how to work with branches and tags, how to handle pull requests in Visual Studio, and how to view the history of your code.

I hope this helps.

Happy coding,

Share this post

Comments (2)

  • Dan Reply

    Thank you so much for an article that explains this concept in a very straightforward manner. As a long time TFS user and new to Git, I’ve had a difficult time finding explanations that made sense. Prost!

    August 28, 2019 at 9:42 pm

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.