Corrado's Blog 2.0

Online thoughts of a technology funatic

Learning Git with Visual Studio #8

It’s inevitable, you or someone in your team make some changes and code doesn’t work as it should anymore and so is important to understand what has changed, when it has changed and maybe even who and why he made that change.
Since the team uses Git is easy to compare different commits and visually see what differs between them.
Let’s go to git history, let’s select the commits we want to compare and select Compare Commits…

image

Inside Team Explorer tab we’ll see a detail of what has changed including all files

image

Double clicking any of listed files I can see what has changes inside that file

image

Same result can be reached by selecting the file in solution explorer, right click, View History and then Compare

image

Of course is not necessary to Commit a file in order to compare it, you can compare any modified or staged file by just right clicking it and select Compare with unmodified menu

image

Of course comparison can be done between different branches as well

image

If you want to in deeper detail about changes related to a file, we can use the Blame (annotate) option, let suppose we did some changes to Program.cs  and we want to know who (and what) has changed that file, just select the file and select the menu Blame (Annotate)

image

A new window will open containing the content of the file and on the left the list of commits that changed those lines

image

To know what had been changed in a commit just click the commit, the changes tab inside Team Explorer will open, right click the file and select Compare with previous menu.

image

No rocket science here, but a feature used quite often during normal dev life Smile

Learning Git with Visual Studio #7

Until now, we worked with a local installation of git and everything was good but once we start working on a project together with someone else we’ll soon discover that we need a way to share our work with other members so working offline is no longer an option.
It’s now time to go online, and in this post we’ll use GitHub as service for host and share our projects.

In order to user GitHub we need to have an account, if you don’t have one, navigate to https://github.com and register:

image

once registration is completed, login and click Start a project button to create a brand new repository, fill form with your data

image

the click Create Repository to create it, after a moment you will have the uri to GitHub repository

image

Now is time to add to Visual Studio plugin the capability to connect to GitHub repository, if not already present go to Tools->Extensions and Updates, select Online on left column and using search box in upper right corner and type github.

The first result should be the extension you need:

image

Select it and click Download to install it, then restart Visual Studio to complete installation.

Now go to Team Explorer, click on the green plug in top toolbar and connect to your GitHub account

image

Sign in with GitHub credential to complete connection of Visual Studio to GitHub.
We can now create or clone a new github repository, let’s clone the repository we created before by clicking clone above GitHub connection.

image

we are now prompted with a list of our repositories, including the one we just created

image

select and click Clone, we’ll now have an empty GitHub project inside Visual Studio, time to add a blank solution using File->New->Project->Other Project Types->Visual Studio Solutions

image

We’ll now see our blank solution inside Solution Explorer

image

Time to add a new project, let’s add a Console Application Project, right click the Solution->Add New Project->Windows Classic Desktop->Console Application

image

and click Ok, the project will be listed under our solution now

image

From the “+” on the left of the items you will now recognize that items are added to local git repository but not committed, let’s add a comment and try to commit the changes

image

Wait! there’s a problem!, what are those files? we don’t need them to be tracked! but since we didn’t added a .gitignore file, git will track all files that are part of our solution.
Let add it before committing the project: In Team Explorer, click Settings->Repository Settings and click Add button next to Ignore File

image

If we now go back to Changes tab, we’ll see this status, and only files making up our solution will be now tracked

image

Let’s click Commit Staged to commit staged and the Commit to complete the commit phase.
Ok, our new project is now tracked by local Git, how to we sync with GitHub?
Let’s select Sync tab, we’ll see something like this picture

image

Let’s click Push to push changes to GitHub and let’s see how they appear on GitHub portal

image

It is an online copy of our local repository.
Let’s add a property to P1 to Program.cs, commit and push the change, we’ll now see the latest commit listed

image

Let’s make a branch dev, and let’s push it, if we have a look online we will see it listed under branches dropdown list

image

It is now evident that GitHub now acts as online hub for all push operation from different team members and relative pull operation when members need to retrieve changes pushed by other developers.
Of course, if two developers works on the same part of code during pull operation conflicts might happen and need to be fixed.
We’ll talk about that… Smile

Learning Git with Visual Studio #6

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.