Corrado's Blog 2.0

Online thoughts of a technology funatic

Learning Git with Visual Studio #3

In this post we’re going to talk about History.
In the previous post we saw how to commit changes so that git can track them, but without an history browser this list of changes wouldn’t be available and we couldn’t do anything regarding commit history.
Git of course let you see all commits history and so the Visual Studio Plugin of course.
From Team Explorer, inside the Changes Tab, select the Actions button and click View History

image

and the list of commits will open

image

Let’s now add a new Class1.cs file to our project, commit the change, then let’s add a property named P1 to the same class and commit the change again.
If we now refresh the list using leftmost button we will see two new entries

image

One of the interesting features of history is that we can see what has changed in each commit, just select a commit entry, right click it and select View Commit Details, the Team Explorer tab will change to Commit Details and will show all files involved, in our case just Class1.cs.

image

Double click the Class1.cs and a diff viewer window will open showing what has changed compared with latest commit in our case we’ll see that property P1 has been added.

image

If you want to compare two specific commits instead just select them and click View Commit Details.

Another interesting feature is Reset, with this option you can delete a series of commits and rollback to a specific point in history.
Right click the commit you want to reset to, select Reset and select Delete Changes, this will remove all changes committed after that point resetting your project to that exact moment.
Reset will remove all changes, including any file added that commit, so use it with care because your job will be definitely lost.
In my case the Reset Hard operation deleted the Class1,cs file from both the project and the disk.

image

under Reset menu there’s another option: Reset (Keep Changes).
After last Reset Hard I’ve re-added Class1.cs, committed, then added P1 property again and committed, putting the git history in the same status as it was before the Reset operation.

image

If I now right click commit with Id 0206669f and select Reset (Keep Changes) it looks like that nothing has changed since both Class1 and P1 are already there and if i add a new P2 property and I commit I see history changing appropriately and Class1 contains both P1 and P2.
Still not totally clear to me honestly, comments welcome.

If you want to delete a specific commit your can right click it and use Revert.
To understand how it works, let’s add P3 and P4 properties to Class1 committing each addition, thus generating following history

image

Let’s now say that we want to delete commit with ID a5aa04f5, right click it and select Revert, we’ll now see Team Explorer tab to prompt something like this

image

and if we click Merge will see that merge tool will try to remove P3 from Class1

image

Click Accept Merge in the upper left corner and P3 property will be no longer present in Class1.

What if you want to undo some modification did to a file returning it to last commit state? Just go to Changes tab, right click changed file, select Undo Changes… and you’re done.

image

No Responses to “Learning Git with Visual Studio #3”

RSS feed for comments on this post. TrackBack URL

Leave a Response