Git and Visual Studio 2017 part 3 : Resetting the changes

In previous article, we saved the changes to the solution to local Git repository. In this article, I explain how reset works both Git and Visual Studio.

Reset in Git

Reset is useful when you cancel commit(s). But how it  really works?

1. Currently there are multiple commits. Run ‘git log --oneline’.


2. I want to reset to previous commit. Before doing so, check HEAD and ref files. It simply pointing to the latest commit.


3. Run ‘git reset --soft HEAD~1’ or ‘git reset --soft 16a378d’. While HEAD points to current commit, HEAD~1 points to previous commit. You can also use HEAD^ but it didn't work in my environment. Seems like it was considered in different meaning. The soft option reset the commit, but do not remove objects from staging area. Run ‘git log’ and ‘git status’ once you reset. The commit 28e41d0 is gone.


4. Open Explorer to see if the commit object is really gone. And it still there!


5. How about HEAD and ref files? It now points to commit 16a378d.


6. Let’s try different option. Run ‘git reset --mixed 16a378d’. Mixed is the default option and it clears staging area, but not remove actual items. Run ‘git stauts’ after the reset. The class files are un-staged, but not removed from working directory.


7. Finally run ‘git reset --hard 16a378d’. Hard option removes items from working directory too. Therefore, no change in status results.


If you compare Solution Explorer and actual Explorer at this point, both Class2.cs and Class3.cs are gone.

Reset the reset in Git

If you want to revise the reset, you can simply reset to the previous commit. Please note that you shouldn't do this is you already ‘push’ the change to any remote. I will explain remote and why you don’t want to reset what you pushed in the future articles, but for now, just run ‘git reset --hard 28e41d0’. Confirm Class2.cs and Class3.cs file are back! Oh you forget the commit SHA1 hash? Then run 'git reflog' to see it.

Reset in Visual Studio

Now you are back to original status. It’s time to reset in VS.

1. Go to Team Explorer and click Changes. Team explorer is the place you can find Git related settings and actions a lot.


2. Click Actions | View History to open commit history.

3. Right click commit 16a378db and click RESET. You see mixed and hard options, but no soft option. Click hard option this time. This is equivalent to 'git reset --mixed | -- hard 16a378db'. So, you know what "Keep changes" means. It keeps the working directory, but un-stage items.


4. Go back to Solution Explorer and see Class2.cs and Class3.cs files are gone.


Reset the reset in Visual Studio

I couldn’t find a way to reset to deleted commit from VS. I also couldn't find a way to see reflog. But you can still use git commands to do it.


According to this article, reset command actually won’t delete anything for 90 days by default. Git stores snapshot, means it has everything intact, therefore no panic when you did reset by mistake! In the next article, I look into branches. Go to next article.