Learning Git with Visual Studio #6

Corrado Cavalli
Corrado Cavalli
Published in
3 min readApr 3, 2018

In previous post we talked a lot about branches and all the stuff involved (merge, rebasing, cherry-pick) but we didn’t covered a quite simple scenario: You’re working on a branch, and while in the middle of an important refactoring you’ve to urgently move to a different branch to fix a bug.
Answer is easy, commit changes and then checkout the other branch, but in this case we committed something that maybe doesn’t even compile, adding a sort of “ok, need to commit, sorry” entry into project history.
Git covers this need with a feature called Stashing, that is the option to ‘save’ your work without involving commit and retrieve it later.
Unfortunately this feature is not yet available into Git plugin for Visual Studio 2017, but with a little help from a 3rd party extensions we can stash changes without resorting to command line.
First of all, install this extension from Visual Studio Gallery.
The extension adds a new entry Stashes into Team Explorer home tab.

image

While on dev branch, let’s add a new property to Class2.cs and click Stashes, a new tab will open where we can add a comment and save current work.

image

Let’s click Create stash to confirm, we will see our change disappear and code revert back to last commit, don’t worry, nothing is lost.
If we have a look at Changes tab we’ll also note that there are no changes pending.
We can now checkout another branch, work on that and then go back to dev branch and click Stashes again.

image

Let’s now right click the Stash entry we want to load and select Apply stash option, this will get our changes back and we can continue working and, at the end, commit to history something that worth it and not clutter the history with meaningless commits.
Here’s the history after committing all changes, no trace of the temporary switch of branch,

image

When you’re no longer interested on a specific stash, just right click it and select Delete stash.

The extension sometime causes some Visual Studio slowness during stash/unstash operation but apart this small issue it works pretty well.

--

--

Corrado Cavalli
Corrado Cavalli

Senior Sofware Engineer at Microsoft, former Xamarin/Microsoft MVP mad about technology. MTB & Ski mountaineering addicted.