Learning Git with Visual Studio #4

Corrado Cavalli
Corrado Cavalli
Published in
4 min readMar 27, 2018

When you’re in a team and start working on a new implementation or when you just want to do some experiments with your code without worrying about introducing regression with actual code, the best thing to do is to use a git feature called Branching.
Branching can be considered a total independent copy of the last commit, so, since we’re on a copy, the original source remains safe and untouched.
While considered a copy, technically is not, because copying all files making up a solution would be not only slow, but also a total waste of disk space, so git handles all this internally, taking care of deltas, giving us the impression that we’re working on a physical copy.

To create a new local branch (we’re still working locally exclusively) let’s select Branches inside Team Explorer, right click the only branch we have now master, and select New Local Branch From…

image

name it dev and select Create Branch

image

This will create a new “copy” named dev and it will make it the default project we will work on, you can see what’s current branch and even switch quickly by looking at Visual Studio lower right corner

image

Let’s now add a new Class2.cs file and commit the change as usual.
If we have a quick look at the history we will see now a new Tag on the right indicating that that commit refers to dev banch

image

If we now, right click master branch and select Commit we will see that Class2 is not there, even on disk.
Let move back to dev branch and let’s add a property P1 to Class2.cs without committing and let’s try to switch back to Master branch.
We’ll see an error message stating that switching is not possible otherwise some changes will be lost, something that git tries to avoid anytime.

image

Let’s commit the change.
Let’s say we’re done with changes on dev branch and we want to bring them into master branch, together with any changes that might have taken place since we created the dev branch.
We can do that using Merge feature.
Right click the branch were we want to push the changes of the actual branch (master in our case) select checkout to switch to that branch, right click it again and select Merge From…

image

Leave the Commit changes option to commit the changes after the merge automatically.
Let’s have a look at Master branch history now

image

As you see you can’t see from the history there’s no trace that a merge operation occurred.
Let Reset the merge and let’s do it again but this time unchecking the Commit Changes flag.
This time we have a warning informing us that we have to commit changes to apply the merge

image

Let’s do it, giving a meaningful comment and let’s see how the history has changed

image

In this case the branch is evident including the additional comment we added after the merge.
If you like this option, as I personally, do, make it permanently unchecking it on plugin settings tab.

image

--

--

Corrado Cavalli
Corrado Cavalli

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