At times I will make some quick changes to a project in Visual Studio to try something out knowing that I will undo the changes when done. Most of the time this works great with no problems, but a few times I am unable to get Visual Studio to undo the changes. Fist I am going to review the steps I normally use to undo and then I will cover the steps I use when files refuse to be undone. Just to be clear I am using Git base source control.
Normal undo changes
Normally when I am ready to undo a set of changes I open the Team Explorer window and select Changes from the drop down at the top of the window. Next I right click on the file, files, or directory I want to undo changes on and click Undo Changes.
This show a message box asking for confirmation before undoing the changes. Click Yes to undo the changes.
If all goes well the select files and/or directory will have reverted and no longer have any changes.
Undo failed now what?
If you undo didn’t clear the changes as expected now what? Well the best answer I have found is to open a command prompt and navigating to the directory that contains the file that needs to be undone and then run the following command. In this example the changes to gulpfile.babel.js will be undone. This will work for both added and changed files.
git checkout gulpfile.babel.js
If you want to undo all the changes in a branch it takes a couple of commands. This first command will remove all changes to tracked files including staged files.
git reset --hard
Next if you have any files that were added (or not already tracked by Git) the following command will remove them.
git clean -f
Your repo will be back to its starting state as if it had just been cloned.
When using the above commands be careful as any changes will be gone and Git will have no record of them. For added files the file will be removed and again Git will have no record of the file. This mean these operations are permanent and should be used with a great deal of caution.