Use 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.

  1. More about Raghav - Step 1: Install git on your system2:46 Step 2: Create account on github - https://github.com/3:27.
  2. Using GitHub with Visual Studio Code lets you share your source code and collaborate with others. GitHub integration is provided through the GitHub Pull Requests and Issues extension. Install the GitHub Pull Requests and Issues extension. To get started with the GitHub in VS Code, you'll need to create an account and install the GitHub Pull Requests and Issues extension.

Git History, Search and More (including git log) View and search git log along with the graph and details. View a previous copy of the file. View and search the history View the history of one or all branches (git log) View the history of a file; View the history of a line in a file (Git Blame). View the history of an author; Compare: Compare. Code actions to create issues from 'todo' comments. Getting Started. It's easy to get started with GitHub Pull Requests for Visual Studio Code. Simply follow these steps to get started. Install the extension from within VS Code or download it from the marketplace. Open your desired GitHub repository in VS Code.

Code

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

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:

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




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.

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.

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

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:





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

Git is a distributed version control system that allows teams to work on the same documents simultaneously. This means there is a single server that contains all the files, but whenever a repository is checked out from this central source, the entire repository is cloned locally to your machine.

There are many remote hosts that allow you to work with Git for version control, however the most common host is GitHub. The following example uses a GitHub host, but you can use any Git host for version control in Visual Studio for Mac.

If you wish to use GitHub, make sure that you have an account created and configured before following the steps in this article.

Creating a remote repo on GitHub

The following example uses a GitHub host, but you can use any Git host for version control in Visual Studio for Mac.

To set up a Git repository, execute the following steps:

  1. Create a new Git repo at github.com:

  2. Set Repo Name, description, and privacy. Do not initialize Repo. Set .gitignore and license to None:

  3. The next page gives you an option to display and copy either the HTTPS or SSH address to the repo you have created:

    You'll need the HTTPS address to point Visual Studio for Mac to this repo.

Publishing an existing project

If you have an existing project that is not already in version control, use the following steps to set it up in Git:

Tip

Use a .gitignore file to control which folders and files are tracked and published with Git. You may want to exclude build directories, binaries, or generated files. Learn more in the GitHub docs on ignoring files.

  1. Select the Solution name from the Solution Window in Visual Studio for Mac.

  2. In the Menu bar, select Version Control > Publish in Version Control to display the Clone Repository dialog:

    If this menu item appears greyed out in the menu, make sure you have selected the Solution name.

  3. Choose the Select from Registered tab and press the Add button:

  4. Enter the name of the repository as you would like it to display locally, and paste in the URL from step #3. Your Repository Configuration dialog should look similar to the following. Press OK:

    It is also possible to use SSH to connect to Git.

  5. To attempt to publish the app to Git, select the repository, and ensure that both Module Name and Message text fields are completed:

  6. Click Okay, and then Publish from the alert dialog.

  7. In the Git Credentials window, enter your GitHub username and password.

Note

If your account has two-factor authentication (2FA) enabled, you will need to create an Access Token, which is used in place of a password. If you have not created an access token, follow the steps in the Git Access Token documentation.

  1. Enter the username and Personal Access Token, and press Okay:

  2. After a few seconds, the Solution should be published with its initial commit. Confirm it has been published by browsing the Version Control menu item, which should now be populated with many options:

  3. Once you start to make additional changes, first use the Version Control > Review and Commit menu to open status view. After you've selected and committed changes, select Push to push the changes to the remote repository. This will allow all appropriate users to view it on github.com:

Publishing a new project

The new project dialog can be used to create a new project with a local git repository. To enable it, select the Use git for version control checkbox, as illustrated in the following screenshot. This will initialize your repo and add an optional .gitignore file:

Follow the steps below to push your new local repository to a new GitHub repository:

Note

If you have not already created a GitHub repository, refer to the Creating a remote repo on GitHub section.

  1. Create your first commit by going to Version Control > Review and Commit in the Menu Bar.

  2. In the Status tab, choose Commit in the top left.

  3. Write a commit message, for example 'First Commit', then click on Commit:

  4. Next, in the Menu Bar go to Version Control > Manage Branches and Remotes.

  5. Go to the Remote Sources tab, then click Add.

  6. In the Remote Source window, add the details of your previously created GitHub repository and click OK:

  7. Close the Git Repository Configuration window, then in the Menu Bar go to Version Control > Push Changes.

  8. In the Push to Repository window click on the Push Changes button:

  9. When prompted, enter your GitHub username and password.

Note

If your account has two-factor authentication (2FA) enabled, you will need to create an Access Token, which is used in place of a password. If you have not created an access token, follow the steps in the Git Access Token documentation.

Visual Studio for Mac will now push the changes to your remote GitHub repository:

Disable Git In Visual Studio Code

Clone an existing repository

It's likely that you'll have to work with a GitHub repo that exists only on the remote, not on your local machine. Visual Studio for Mac allows you to clone this repo quickly. Follow the steps below to clone it to your machine:

  1. In the Menu bar, select Version Control > Clone Repository:

  2. This displays the Connect with Url tab:

  3. On the GitHub page of the remote repository, press the Clone or Download button and copy the URL provided:

  4. Replace all the text in the URL entry field in the Connect with Url tab. This will populate most other fields in this tab for you, as illustrated in the image in step #2.

  5. Enter the directory that you want to clone the repo into and press Clone.

Note

Register Git In Visual Studio Code

You may experience issues if the repo is over 4 GB in size.

Troubleshooting

If you have issues with initializing your project with an empty remote repository, you can try the following steps:

  1. Go to your solution folder.
  2. Press Command + Shift + . to show the hidden files and folders.
  3. If there's a .git folder, delete it.
  4. If there's a gitignore file, delete it.
  5. Press Command + Shift + . to hide the files and folders.
  6. Open your solution in VS for Mac.
  7. On the solution Window, select your solution node.
  8. Browse to the Version Control menu and choose Publish in Version Control.
  9. Follow the steps of the above tutorial starting from the step 6.

Visual Studio Code And Git

See also