I’m not a developer, but I do enjoy programming and writing scripts. In an effort to be “more official”, I’ve decided I needed to work more with Git and source control. Coming from a more system administrator background, usually scripts are kept on your local drive or, if you’re lucky, a file share where others can access them. My goal moving forward is to keep more of my public scripts on GitHub for others to consume as well as having a central repository so I could access them from multiple systems.
It had been awhile since I used Git and Posh-Git, so I recently undertook the process of setting this environment, and, as usual, I struggled. I had a vague screenshot of how to set up the repository once everything was configured, but nothing of installing the necessary software or generating an SSH key. This blog post is an effort to document this, primarily for myself, but you are welcome to use. Also, if you are more experienced in this area and see something I should (or should not) be doing, please leave a comment below. I’m not going to pretend that I fully understand everything I did or if this is the best way to do it.
The first component that needs to be installed and configured is the Git client. Git is version control software used to check out files, make branches, and merge those changes back to a central repository. The client can be download at https://git-scm.com. I’m not going to go through a screen-by-screen how-to of its installation, but I usually accept the defaults and don’t have any issues (yet). One option that I believe you will need to select during the installation is “Use Git from the Windows Command Prompt”. This option will add a directory to your PATH variable in Windows, which the Post-Git module will need. If this directory is missing, the Posh-Git module will generate an error (I demonstrate this later in the post). For example, this is what my PATH variable looked like before and after installing the Git client:
Once the git client is installed, you’ll need somewhere to save your work. While is it possible to configure your own git server, I created an account at GitHub so I can publish scripts for others on the Internet to access. Go ahead and create a free account. Next we need to configure Git on my Windows 10 computer with an SSH key. This SSH key is used to connect and to authenticate to my GitHub repository without using a username or password. The SSH key is stored on my desktop, and the SSH key is attached to my GitHub account.
To generate the SSH Key, open the Git Bash program (this was installed as part of the Git client earlier). In the Git Bash window, generate a key by entering the command ssh-keygen. The command will prompt where to save the key. I accepted the default path in my Documents folder. It will also prompt for a password for the file. I entered in a password, but you can also leave this blank (I will point out where this password is used later).
Next, verify that the ssh-agent is running by typing in the command eval $(ssh-agent -s). It should return a processor ID (PID) value. Follow this up by adding the SSH private key to the ssh-agent by running the command ssh-add followed by the path to the key created previously. Enter the previously created passphrase if prompted.
Next, the SSH key generated on the computer will need to be added to your GitHub account. The key can be easily copied from the Git Bash window by using the clip command followed by the path to the key generated in previous steps.
Log into your GitHub account and select the menu in the upper right corner, select “Your Profile”, then select your avatar to go to your Personal Settings (is there an easier way to get here than this?). From here, select SSH and GPG Keys on the left menu. Click the New SSH Key, give the key a descriptive name, paste the contents of your clipboard into the box, and click Add SSH Key. The SSH key from your computer is now added to your GitHub account.
Follow this up by returning to the Git Bash command window and verifying the connection out to GitHub by typing the command ssh -T firstname.lastname@example.org. You will see one of two warnings each with a different fingerprint. If the fingerprint matches one of the following screenshots below, type yes and press Enter.
Whew, OK, now that Git is installed and configured, we can move onto installing Posh-Git. If you have PowerShell version 5 or higher, you can use the Install-Module posh-git -Scope CurrentUser command directly from a PowerShell window to install from the PowerShellGallery.com repository. The first time I ran it, I got a message to install the latest version of NuGet. I accepted this warning and also accepted to install the Posh-Git module from an untrusted repository.
Now we can import the module using the Import-Module posh-git command. If you attempt to do this first without installing the Git client, you will get a message that the git command could not be found and to create an alias or add it to your PATH variable. This is what I mentioned earlier when installing the Git client to include adding it to your PATH variable.
Before I could submit any commits to my repo, I had to set my email and username. I think this could have been done when generating the SSH key, but I could be wrong. To do this, run the git config –global user.email and git config –global user.name commands:
We’re finally getting somewhere. Once the module is imported, we can start setting up a local repository from our GitHub account. Since I already had existing projects in my repo, I wanted to pull these down to my local system. I went to my Skype repo and used the Clone or download button to copy the address for the repo.
Next I created a folder name JeffBrownTech to hold my repos on my local system. I changed my PowerShell prompt to make this folder my current directory and ran the git init command to create an empty Git repository. Next I ran the command git clone along with the address to the Skype repo copied earlier. This created a folder underneath the JeffBrownTech folder with the existing files from the online repo. The git status command shows that the Skype folder is new and has not been committed.
You will also notice on line 5 that it prompted me for the passphrase for my SSH key. I believe that if you leave the password blank during the SSH key generation, you may not get this prompt, or you can just press Enter to continue the process. Since I put a password on here, I had to enter it. If you want to save some keystrokes, don’t generate a password for the SSH key when generating it in earlier steps.
Next I used to git add Skype/ to add the cloned Skype folder to the list of content to include in my next commit. I used the git status command again to show that each file underneath the Skype folder is now added and will be uploaded on the next commit.
Finally, I ran git commit -m “<Comment>” to commit the changes with a comment (if you don’t use the -m to add a comment, it will switch you into vim in order to add the comment. I’ve never figured out how to do this correctly, and it’s a pain to exit vim, so just do it like this and save yourself some frustration). The window will show the files being uploaded to the repo and the status window showing there are no uncommitted changes in the directory.
I repeated the above steps for the Exchange repo, here is a screenshot with all the commands:
Finally, now I have local directories attached to my GitHub repos, and as I make changes and updates, the new versions can up pushed to the repo easily. Now, I realize some steps may have been skipped here as I already had an existing repo and probably set something somewhere in my profile to use SSH for encryption, but hopefully this can get someone else pointed in the right direction to get started with Git and PowerShell. Thanks!