Version Control with Git for MP Authors – part 3

Our little journey into Version Control for MP Authors continues. You can find the other part(s) here:

This part covers

  • how to sync your local to a remote repository in Visual Studio Team Services (former Visual Studio Online)
  • how to clone a remote repository on a remote machine
  • how to use the remote repository as a hub to sync changes between two local repositories

or better shown as an overview picture:

SyncOverview

What requirements do we need for this part?

  • a Windows machine with Visual Studio 2013/2015 and VSAE installed
  • latest Git release installed (like described in part 1)
  • a local demo MP project (as created in part 2)
  • an account for Visual Studio Team Services (VSTS)

 

Which remote repository should I use?

This is completely up to you. You can either build your own remote repository server or use popular ones like GitHub or our own Microsoft Visual Studio Team Services (former Visual Studio Online). Both products are well integrated into your local Visual Studio. For this post I will choose VSTS.

So, let’s get started. Log on to your VSTS account …

GIT03-01-Accounts

… and either select an existing account or create a new one. My selected account does not contain any projects yet:

GIT03-02-EmptyProject

Publish your local repository to Visual Studio Team Services - the easy way

There are multiple ways to accomplish that, but the easiest one is by using the built-in capabilities of Visual Studio. Open up your MP project and switch to the Team Explorer’s Home view. There you will find a “Sync” button:

GIT03-06-SyncInVS
This leads to the Synchronisation menu where you can find multiple ways to publish your local repository: VSTS, GitHub and other remote repositories.

GIT03-03-SyncOverview

Select VSTS, log on to VSTS, select your account and you will see, that Visual Studio will create a new Repository when you click on “Publish”:

GIT03-04-Publish

When you switch to VSTS in your browser you can now see the newly created project and navigate straight to it by clicking on the name:

GIT03-05-PublishTS
VSTS offers you a bunch of things you can do with your project online, e.g. exploring the code or comparing it by selecting a specific file and click on “compare”:

GIT03-06-DemoOfProjectInTS

This is really handy because it gives you anywhere access to your MP projects even if you do not have access to your local Visual Studio!

 

Publish your local repository to Visual Studio Team Services - a more complicated way

If you try to sync your local repository to VSTS and all you see is a prompt for a remote repository URL...

GIT03-13-SyncNew

... you can create an empty repository in VSTS manually and get the necessary URL from there. To do that, log on to your VSTS and create a new project. It is recommended to give it the same name as your local repository name!

GIT03-13a-SyncNewGIT03-13b-SyncNewGIT03-13c-SyncNew

After the project has been created, use the "Navigate to project" to navigate to your newly created project folder and switch to the "Code" view:

GIT03-13d-SyncNew

There you can find the necessary URL. Klick on the copy symbol, copy the URL and paste it into your Team Explorer:

GIT03-13e-SyncNew

Just click on "Publish" and your are also done. A bit more complicated but also possible :)

 

Syncing changes between local and remote repository

Now that we have a local and a remote repository, how can we sync changes between both? Let’s begin with the remote repository:
VSTS allows you to change files online. I will edited my PowerShell script online…

GIT03-07a-EditingInVSTS
and click on the Disk symbol to save and commit the change:

GIT03-07b-CommitInVSTS

When you refresh your local Visual Studio repository you will see, that there is an incoming commit:

GIT03-08a-PullIntoVS
Git uses the concept of “fetching” or “pulling” data from remote repositories and Visual Studio supports that as well. Maybe later I will write another article about the differences. For our purposes here we can simply use the Sync command to have the data fetched from the remote repository into our local version.
If you have a close look at the icon in front of our local script file, you will notice that Visual Studio considers the modified file as “uncommitted”, so we have to commit it to our local repository:

GIT03-08b-PullResult GIT03-08c-CommitLocal GIT03-08d-Sync

After it has been committed, we can sync the setting back to our remote repository as well:

GIT03-08e-Sync GIT03-08f-SyncResult

You can control the whole process by looking at the history of our project and compare the local history…

GIT03-09a-HistoryVS

… with the remote history in VSTS by clicking the history link of our project:

GIT03-09b-HistoryInTS
.

Syncing repositories between two different machines

Syncing repositories between different machines can be accomplished in multiple ways. At the moment we are still at the base level and I want to show you how you can sync repositories for the same user between multiple machine.
An example for this scenario is:
You develop a MP on your own Development machine and want to synchronize the project with a customer on-site machine.

We will use VSTS as a hub for synchronizing the local repositories. To accomplish that, I will show you two steps:

  • Step 1: How you can clone a repository from VSTS to a new Windows box (let’s assume, that this is the customer on-site machine)
  • Step 2: How you sync changes between both local repositories

I will call the two machines PCa (for the original machine) and PCb (for the new, second machine).

Step 1: Clone a repository

Cloning means to create a copy of a remote repository. On PCb open up Visual Studio, start Team Explorer and connect to your VSTS team project by clicking on “Select Team projects”. In my case you will see the only Git project available in this account:

GIT03-11a-ConnectInVS

You can select the Git.Demo.MP project by double clicking and Visual Studio immediately asks you, if you want to clone the remote repository:

GIT03-11b-CloneInVS

Just enter your preferred root folder and click on “Clone”:

GIT03-11c-CloneInVS

After the successful cloning…

GIT03-11d-CloneInVSResult

… you can see the files in your local Explorer in the specified folder:

GIT03-11e-CloneInVSResult

Step 2: Syncing changes

On PCb I will modify my PowerShell script and insert a new line of code:

GIT03-12a-Sync

After editing the file, you have to locally commit the change and then sync the commit to the remote repository like I showed you before.
In VSTS you can see the change:

GIT03-12b-Sync

On PCa, my original machine, I simply have to sync the remote repository as well to see the change:

GIT03-12c-Sync

Looking at the history on PCa shows us that the commit from PCb was successfully synched to our local repository:

GIT03-12d-HistoryOnFirstMachine

Summary

In this post I showed you

  • How to create a remote Git repository and sync the local repository to it
  • How you can sync changes between local and remote repositories
  • How you can clone (create a copy) a remote repository to a new machine
  • How to use the remote repository to sync changes between two local repositories

Only in combination with a remote repository you can unlock the real power of Git. As a single user you can use that as some kind of backup of your local repositories and to sync repositories between different machine (test and production, local dev and on-site customer machine etc.).

I hope you will find this post useful and stay tuned for the next steps.