Beckhoff#Let’s try Source-Control with Git

In this Tutorial I will show you how to use git as a Source Control to your project.Thanks for the Integration of Visual studio, the common tools can be used.

Now there are so many source control tools to help you for project management, git is just one of these.

Before Git..

Let me ask you a question – how do you work with multiple users to edit a same project?

The Common way may be to copy and rename the files. But it is very easy to make mistakes.

If the rules of file naming are not firmed, other user will not know it is the latest one or not.And Also we will not understand what kind of modification is done inside.

In the worst case, other users will overwrite the latest one again and again without any warning.

What is Git?

Let’s refer from Wiki:

https://en.wikipedia.org/wiki/Git

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows (thousands of parallel branches running on different systems).

Git is a tools with other kernel developers contributing to its initial development.It will show when/what/who provides each commit and allow for the comparison.

And Also, Warning is shown while your commit is conflict to the last commit on the server.


KeyWord

In this tutorial, there are some new keywords and I will explain the meaning here simply.

Repository

A repository is a data structure that stores metadata for a set of files or directory structure.The main purpose of a repository is to store a set of files, as well as the history of changes made to those files.Exactly how each version control system handles storing those changes, however, differs greatly. For instance, Subversion in the past relied on a database instance but has since moved to storing its changes directly on the filesystem.These differences in storage techniques have generally led to diverse uses of version control by different groups, depending on their needs.

From Wiki

https://en.wikipedia.org/wiki/Repository_(version_control)

Commit

Commit is your basic operation to Add your modification to the repository, and git will “upload” the difference as compared to the last commit.

Stage

Stage is a feature to register the modification into the index, and git will ignore the commit outside the State.


Push

An operation to upload your local Repository to Remote server.

pull

An operation to Download the Repository from a remote server to your local environment.

Branches

A branch represents an independent line of development and isolating each branch will not affect each other. For Example, Valve Control Program is developed by TeamA, and the On/Off Count of Valve is a sub branch of Valve, developed by TeamA EngineerA…Etc.

Engineers can work in the same time and Merge these work in the future.

Revert

Revert is used to redo the last Commit.


CherryPick

cherry-pick is the features that take a commit to your current commit.

Merge

Merge is an operation to merge the Bifurcation of different branches.

.gitignore

A description file to control the files that you would like to ignore.

Git Install

Let’s install the git first.Access the follow link;

https://git-scm.com/downloads

Practice with Visual studio

It may be a little different to start with twinCAT if you do not know how to operate the git.

Let’s create a new c# project and practice it!

Setup

Go to Tools>Options.

Go to Source Control>Current source control plug-in and Choose Git as your source control Plugin.

Go to Source Control>Git Global Settings and enter your user information.

You can see Git is displayed in your Visual Studio.

New Repository

Now we need to create a new Repository.

Go to File>New>Repository.

we will use Local in this time only, choose Local Only>Local Path and set up your Repository Path.

New Project

Go to File>New>Project to create a new project that is managed by that Repository.

Choose Console Application>Console Application and Next.

Create the project path inside the Repository that you created before.

Create!

A Console Application is created.

Activate Useful WorkSpace

There are some views that are used in git operation.

Let’s activate it.

View>Git Changes.

It is the view for commit and compare your modification.

View>Git Repository.

A view that allows you to view all the modification history and Branch.


Your First Commit!

Good!Let’s create your first Commit.

Go to Git Changes View.

The modifications are shown.

Message must be entered before you commit the modification.

This is a description of why/what/how/who changes it.

Please google Git Commit Format to get more information.

Press the + Button.

your changes will shift to the Staged Changes area.

Enter some message for your commit.

I put “init” only  in the message field and press the Commit Staged button to commit your Modification to Repository.

Good! your first commit is finished.

View the History

Now is the time to check your commit history.

Go to Git>View Branch History.

Sure you can right click the program and choose Git>View History.

All the modification logs are shown here.

Commit Some Changes

Let’s add some lines into your program.

Go to Git>Commit or stash.

Right Click and choose Compare with Unmodified.

Visual Studio will show the modification point very clearly.

press + button to Stage all.

Commit is Finished.

You can view the modification log – the last commit is shown.

Compare with Previous Version

The Next step, we will try to use the compare function and It is a very important function.

Right click the Log in the History<Compare with Previous.

Visual Studio will show the result between the last modification.

Revert

Let’s revert the modification that you committed last.

Add some lines and commit it once.

Add more lines and commit again.

There are 2 commits in History.

Now we would like to revert the last commit. Right Click>Revert.

Yes.

Visual Studio will show a warning that Conflicts exist .

Open the Unmerage Changes area, Right Click and choose compare current and incoming.

If the changes are ok in here, Choose Take Incoming(Soure).

Done!The last commit is reverted.

Cherry Pick

Let’s Cherry pick the last commit.

Go to History view and Right Click>Cherry-Pick.

Done!Git will take the difference between the commit and your current project.

Branches

We imagine that now there are several teams in your project.

Branches can be created to manage each modification and isolate each other.

Go to Git>Mange Branches.

All Branches are shown in the current Repository.

Now we only have a master Branch.

Add Local Branches

Right Click the Master Branches and Choose New Local Branch From.

Enter your Branch.

And because we only have Master, just press the Create button.

TeamA Branch is created.

Commit from Local Branches

Checkout to TeamA and modify your project.

Some LInes are inserted.

Commit!

Now the Commit from TeamA is shown.

Of course this commit came from TeamA, you can see the modification in TeamA Branch.

But How about Master Branch?

There is nothing changed in the Master Branch.

Merge

If you need to reflect the modification to another master branch, a merge operation is necessary.Right click the Branch of Team A>Merge “TeamA” into Master.

Yes

Now the Modification from TeamA is reflected to Master and merged.

You can checkout to the Master Branch to check it.

Create Branches from other Branches

Now we will create a sub branch inside TeamA.

Right Click TeamA >New Local Branch From.

In the Based On option, choose TeamA.

Create it.

 Branch TeamAClass1 is created.

Now we use this Branch to add some code.

And Commit it.

Because this commit is not merged to TeamA, the modification will not be reflected.

As same as Master.

Merge

Let’s Merge the Branch of TeamAClass into TeamA, Go to Merge “TeamAClass1” into “TeamA ”.

Yes.

TeamAClass1 is integrated into TeamA.

Now TeamA has the same project as TeamAClass1.

But Master is not reflected.

Let’s integrate TeamA into Master.

Yes.

Now you can see these 3 Branches are in the same Line.

The Master had the same modification now.

Play with TwinCAT!

After the practice, now is the time to play with TwinCAT.

File Structure in TwinCAT Project

Here is a table to show all the usage of TwinCAT Project Files.


Setup in Visual Studio

There are some Git settings that you need.

Go to Tools>Options.

Please go to Source Control>Plug in Selection to confirm if git is chosen or not.

Then Click the TwinCAT>XAE Environment>File Settings.

Here is a setting for saving the project files into Multiple files or not.

If you want to use git as your source control in here, multiple files is necessary.

And Also, you can right click the File and choose Independent Project File.

After you set the new Repository, All the Axis had a Blue or red small icon, and also a floppy.

Then click the PLC Project Instance.

Go to the Object Tab and Check the “Keep Unrestored Link Info”.

Setup in TCCompre

In the previous step we used Visual Studio to compare the Code. The TwinCAT code are created by XML Format, so it is not easy to compare it directly.

Here is a Great tool from TwinCAT that is named TwinCAT Project Compare!

Tools>Configure User Tools.

Export Configuration.

Plugin>GIT.

If a Configuration file exists in your OS, “Config is found!’ is shown.

New Repository

Now we can create a new Repository for the TwinCAT Project.

You can see a Git in the toolbar.

Git>Create Git Repository.

Set up your Path.

Commit!

Let’s Commit it.

Here is a small red mark to indicate which file is changed.

Right Click>Git>Commit or Stash.

These operations are the same as the previous step, I will not explain in here.

Check the Commit History.

Done!

Fatal Error

If there is a Fatal Error while Commit, I will show you how to solve it.

What Happen?

Let’s Check what happened here. Go to the directory of your Repository>Right Click>Git Bash Here.

git status

.project.~u is updated frequently here.

we need to configure a file to ignore it.

Add .gitignore File

Add a file with .gitignore extension.

Please copy the following description and save it.

Here is the Reference link:

https://alltwincat.com/2021/05/16/official-gitignore-for-twincat-3-on-github/

# TwinCAT files
*.tpy
*.tclrs
*.compiled-library
*.compileinfo

*.project.~u
*.tsproj.bak
*.xti.bak
/TwinCAT Project1/TwinCAT Project1.project.~u~parent of 2dcc9fc (ok)

Tools>Options.

Source Control>Git Repository Setting>Git Files, the ignore file is added anc read in the  Visual Studio also.

While you click the edit button, the same content is shown.

If Git can not refresh..

If the .gitignore file can not be reflected after editing, please use the following command to flesh it.

git rm -r –cached . 

Finally

Now your modification should be committed !

What About the TCCompre?

Now I will show you how to use TCCompre.

While you commit your code, a M icon is shown.

Here is the meaning of each letter:

M:Modification

A:New

D:Delete

R:File name is changed.

These letters are the standard of Git but not only TwinCAT.

While you click the M letter, the ST editor is opened and shows you the difference.

As I said before, TwinCAT saves the File in XML format.

And it is not easy to compre.

Finally

Please ask me if you have any question;)!

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

コメント

  1. Adrian Jenkner says:

    Great job!