Powershell

Use Git with Visual Studio Code

Learning version control is a great way to move your PowerShell skills forward. Let's look at how to Use Git with Visual Studio Code to keep up with changes

Visual Studio Code is a great PowerShell development platform and since moving off of PowerShell ISE, I can say that I haven’t really looked back since making the move. ย Visual Studio Code provides tight integration with Git so it is an excellent way to start using version control if you haven’t already with you PowerShell code. ย Let’s run through just a quick basic tutorial on how to use Git with Visual Studio Code. ย We will walk through a basic clone of a repository and synchronizing of files in Visual Studio Code using the integrated GUI Git tools that are installed by default. ย Let’s dive in and take a look at a couple of basic tasks most will want to accomplish – cloning and synchronizing files.

Use Git with Visual Studio Code

The first thing I like to do is install the Git for Windows installation that allows us to work with Git from the command line. ย Below are screenshots of the installation, although I left everything at defaults. ย The screenshots will show what those default are during the installation.

Git-Hub-for-Windows-Installation-begins
Git Hub for Windows Installation begins
Choose-installation-directory-for-Windows-Git-Hub
Choose installation directory for Windows Git Hub
Select-Git-Hub-for-Windows-components
Select Git Hub for Windows components
Name-the-start-menu-program-group
Name the start menu program group
Choose-PATH-statement-configuration-and-command-line
Choose PATH statement configuration and command line
Choose-SSH-executable-for-Git-Hub-Windows
Choose SSH executable for Git Hub Windows
Choose-HTTPS-transport-backend
Choose HTTPS transport backend
Configure-line-endings-format
Configure line endings format
Configure-Git-Hub-terminal-emulator
Configure Git Hub terminal emulator
Configure-Git-Hub-extra-options
Configure Git Hub extra options
Completing-Git-Hub-for-Windows-installation
Completing Git Hub for Windows installation

Cloning Git Repository Visual Studio Code

For the purposes of the Git Hub Visual Studio Code walkthrough, I simply created a Test repository. Here we are logged into Git Hub and are getting the repository URL. We use this URL to clone the repository on our Visual Studio Code workstation.

Clone-Git-Hub-Repository-URL
Clone Git Hub Repository URL

Now that we have our repo URL, we can clone the repository on our Visual Studio Code workstation to work with the code locally. ย To do this we launch the Git CMD utility. ย ย Note the directory you are currently inside of in the Git CMD Window is where the repository will be created.

Use-Git-Clone-command-to-clone-remote-repository
Use Git Clone command to clone remote repository

As we can see our files that exist on the remote repository now exist in our local repository folder.

Remote-Git-Hub-Repo-cloned-locally
Remote Git Hub Repo cloned locally

Open Local Git Repository Visual Studio Code

Now, to Visual Studio Code. ย We click toย Open Folder. ย Point Visual Studio Code to the folder of your repository which for me was c:users%username%documentsgittestrepo.

Open-Clone-Git-Hub-repo-folder
Open Clone Git Hub repo folder

As we see below, we have our repository folder open and we see the correct files as they exist on GitHub. ย I want to create a new Powershell file. ย Click the new file with the + symbol to create a new file to work with.

Create-new-Git-Hub-file-local-repository
Create new Git Hub file local repository

Stage and Commit changes from Visual Studio Code to Git

After creating a new file calledย test.ps1 and entering a few PowerShell lines, we want to stage our changes.

Creating-a-test-Git-Hub-file-local-repository
Creating a test Git Hub file local repository

Hovering over theย + sign next to our test.ps1 file we see theย Stage Changes button. ย Click it. ย If you don’t choose to stage changes, you can still commit. ย You will see a “warning-ish” message that you have no changes staged to commit, “would you like to automatically stage all your changes and commit them directly?”. ย Staging first is the best practice for GitHub workflow.

Stage-changes-local-Git-Hub-repository
Stage changes local Git Hub repository

Now, our changes are staged and ready to commit. ย Next to theย Source Control: GIT area at the top, we should see a “check mark” we can click. ย If we hover over it, we will see theย Commit button popup.

Commit-Git-Hub-staged-changes
Commit Git Hub staged changes

To commit our changes, typeย commit in the box that pops up and hitย Enter as the directive mentions.

Type-commit-and-enter-to-finalize-the-commit
Type commit and enter to finalize the commit

After doing that, we will notice at the bottom left hand corner, we will see the “1” show up in the up arrow or changes to be synchronized to our remote repository. ย Click the littleย synchronize changes arrow to sync the changes.

Changes-show-to-be-synchronized-to-Git-Hub
Changes show to be synchronized to Git Hub

A Visual Studio Code warning will pop up alerting this action will push and pull commits to and from ‘origin/master’.

Verify-push-and-pull-commits-to-GitHub
Verify push and pull commits to GitHub

We will be prompted to login to our GitHub account.

Login-to-GitHub
Login to GitHub

After providing credentials and waiting just for a moment, we can refresh our GitHub repository and we should be able to see our new file that was synchronized.

Verify-our-test-file-is-synchronized-with-GitHub
Verify our test file is synchronized with GitHub

This works in reverse as well. ย I created aย test2.ps1 file on the remote GitHub repository and synchronized changes. ย After synchronizing, I saw theย test2.ps1ย file locally in Visual Studio Code.

File-created-on-GitHub-remote-is-synchronized-locally
File created on GitHub remote is synchronized locally

Thoughts

Visual Studio Code is now my preferred PowerShell code editor of choice. ย Features such as the tight integration with Git, make it an excellent choice for version control. ย There are a lot of PowerShell features and functionality built in and it is sure to only get better. ย New versions of Visual Studio Code are being released quite regularly. ย I love the look and feel of the interface as well. ย Do yourself a favor if you haven’t checked Visual Studio Code out yet to grab a copy and start playing around with the features/functionality, especially with Git integration.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.