Using Git In Visual Studio Code



So to start with coding scripts apparently “the” way to go is to use Git. Dale was using gitlab.com to share his scripts repository so I decided to go the same route.

I created an account and created my first “Project”.

You need to fill in some information and when you have done this, just create the project!

Once your project is created you will see that there is only a README file that we choose to create, but other than that it is empty.

Cloning your project using git

Code

Retain Context When Hidden: Specifies if the Git Graph view Visual Studio Code context is kept when the panel is no longer visible (e.g. Moved to background tab). Enabling this setting will make Git Graph load significantly faster when switching back to the Git Graph tab, however has a higher memory overhead. Most of the time the Git integration of Visual Studio Code will do fine. But for other functions of Git the command-line seems more efficient. Once you use git over the command-line you’ll find yourself sometimes using a terminal text editor. Which might be fine for you.

Git Use Visual Studio Code As Default Editor

Now that we have our project we can “clone” the project locally to our computer. So you need to copy the git URL from the GitLab project folder on the website:

GitLens supercharges the Git capabilities built into Visual Studio Code. It helps you to visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more. There are three ways to start using Git with Visual Studio to be more productive: Open an existing Git repository. If your code is already on your machine, you can open it by using File Open Project/Solution (or Folder) and Visual Studio automatically detects if it has an initialized Git repository. Create a new Git repository.

Open Visual Studio 2015 as an administrator, as shown below. Open the solution as shown below. Click ‘OK’ on the ‘Security Warning for Framework’ dialog box. This will open the Solution Explorer, as shown below. Build the solution. This will make Visual Studio to download all the Nuget packages.

Now we have the URL we can start the clone.I have a MacBook Pro with OSX so for me this looked like this:




Code

There is also a way to authenticate with a private/public SSH key, so you don’t have to put in your username and password every time you clone your project. But this is out of scope for this blog article. I will try to explain this in another blog article.

We see that the directory is still empty.

Using git in visual studio code

Opening the cloned project in Visual Studio Code

Now the next step is to work with a proper code editor. And that is where Visual Studio Code comes in. So obviously you will have to install Visual Studio Code first, and this is nothing more than download it and run the install here.

Opening Visual Studio Code for the first time can be overwhelming and confusing about where to start for the first time. But this blog is here to help you with that.

Open the folder you just created:

When you selected your folder, and click on “open” you will see the following screen. Notice the Explorer bar on the left. I suggest clicking around and get yourself familiar with the GUI and the code editor a bit more.

Now that we have everything set up we are ready to create and edit some scripts. Right-click the Explorer and select “New File”:

After creating the actual file I am going to place a comment in there as a quick test and used the short keys CMD + S to save the file locally.

Now make sure your new file is actually “part of the project” I needed to move my sample.tf file to the project folder. Once I did that a little “1” appeared in the “Source Control” button. This means that the project has 1 change this change can be committed. With “commit” we send our code to the gitlab.com cloud storage.

Notice the “U” and this has to do with that this is a new file, and the changes are “untracked. With an existing file, you will be able to track the changes.

Before we can commit we first need to “Stage Changes” by right-clicking the file.

We can commit the code/changes by browsing to “Source Control” by clicking on the “1”. And then select the “checkmark” button.

Vs Code Git Pull

When this is done you need to type in some kind of description with what you have changed.

When you hit enter, you will notice in the status bar a “1” with an arrow up. This means that there is one (staged) change to push over to gitlab.com. And you need to click on this arrow.

You will get a box with the actual confirmation. Click “OK”.

Ow, when I browse to the www.gitlab.com website again and do a refresh I will see the new file has been uploaded.

So now that we have learned how to use git with www.gitlab.com and Visual Studio Code we are ready to create some terraform scripts.

Using SSH key authentication

Visual

With pushing and pulling you need to type in your credentials every time you do this.To make this easier you need to use authentication with SSH keys.

Enable SSH Key authentication.

Generate an RSA key:


Look at the key and copy the output:


The key import can be verified here.

Now we can test a “pull” using SSH key authentication:


Using Git In Visual Studio Code




Retrieved from 'http://www.iwan.wiki/index.php?title=Gitlab,_Git_and_visual_Studio_Code&oldid=1657'

Utilize the tight integration of Visual Studio Code with the Git source-control management system.

Learning objectives

In this module, you will:

  • Perform common GitHub tasks by using the Command Palette in Visual Studio.
  • Monitor the status of your work
  • Commit your files to your repositories from the Source Control view

Prerequisites

  • Git, installed
  • Visual Studio Code, installed
  • A basic understanding of Git concepts and commands, such as working with repositories, forks, clones, and branches, staging and unstaging changes, and pushing commits.
  • You need a GitHub account.
  • Exercise - Clone a repository and create a branchmin
  • Stage, commit, and push changes to a remote repositorymin
  • Exercise - Stage, commit, and push changes to a remote repositorymin