Mark III Systems Blog

How I Use VSCode and Git

Discussing how my workflows are with other IT folk is always tough. I find I do most things in a unique way. Which is probably why I am always automating. Without it I will do the same thing three different ways and end up with the same outcome. I am definitely not a creature of habit, but more of someone who likes to take all of the paths. So when it comes to SCM and VSCode I expect that maybe the same.

Below gives you a general idea of how I use VSCode and Git together for development and day-to-day workflows.

First- For you Windows guys out there. If you don't use VSCode as an IDE or simply a working terminal for your day-to-day activities then you should pay close attention. This simply combines a text editor, PowerShell terminal, ad-hoc putty and a super powerful IDE all in one. Almost everything has a PowerShell module you can pull down quickly from PSGallery and start doing some cool admin tasks.

For the Unix guys out there, I think you might like this too.

Get Git - Install Windows Git

  1. Download and install git for Windows

https://gitforwindows.org/

  1. Accept all of the defaults
  2. Be sure the Credential Manager is set

Install VSCode

  1. Get your WMF updated to the latest version of 5. I think 5.1 is the latest as of this writing.
  2. Download and install VSCode

https://code.visualstudio.com/download

  1. I check all of these boxes

  1. Complete the install and open VSCode

  1. I keep a REPO folder in My Documents that houses my scratch repo and all other locally cloned repos.

  1. That is the PowerShell escape character (the "tick") after SHIFT.

  1. Hit CTL+SHIFT+` on your keyboard
  2. This should open a terminal into the REPOS folder
  3. Clone you Scratch repo from github

Below is reference for how and why I use a scratch repo

http://bryan-ops.com/getting-started-with-source-control-management-for-the-sysadmin/

  1. CD into scratch from the terminal

Now you have your working folder with your scratch directory.

Create new script in scratch

  1. Crete a new file in scratch

Now you have a new file in the local copy of your scratch repo, the file should be open in the text editor and you are in the terminal in the same working directory.

Notice VSCode already shows that the file is untracked in Git by using the Yellow "U"

  • VSCode will run the entire active script in the text editor with F5
  • VSCode CTRL+F5 will run the entire script without debug
  • F8 will run the selection or where your cursor is

Use the code below for testing

# Paste this in your testfile.ps1 and CTRL+S it
cd ~
gci | ?{$_.mode -like "d*"} | %{ ##Press F5 to run the whole thing
    gci $_  #Put cursor here and run F8
}
cd ~\Documents\REPOS\Scratch
## Watch the output in the terminal 
## Assuming you have stuff in your home folder

Start tracking the new file in the GitHub repo

  1. Run the git add command on the new file

  1. Commit the file changes since the last commit

  1. Push your changes to the Scratch repo

Changing existing files in the repo

Changes to existing files are handled the same way.

You will want to make sure that as you change files, your commits are targeted. What I mean by this is that you don't want to get lazy and "git add ." When you have a lot of changes you should get in the habit of adding and committing as you move along. Queuing up commits until you are ready to push.

Let's say I add some comments to a file and during this process I have altered another file at some time before committing.

"git status" will unveil the changes I have from the origin.

You see here I have two files that I worked on at different times. What I should do is add the files one at a time and comment the change to each.

This allows the two files changes to be tracked independently inside of GitHub commits.

And I basically do this jumping around between repos and working in the terminals and text editor.

Push when you are ready!