How to Add Source Control to Your Powershell Module
In my last post, I went through my process of how I start a new Powershell module. I ended the post with having created the module folder structure and developed the first cmdlet to connect to the Meraki Dashboard APIv1. If you haven’t checked out the post you should definitely give it a read How to get started developing a powershell module In this post i will go through the process of adding the powershell module to source control on GitHub.
Why use source control?
Source control is a vital component when developing any kind of software. Together with a source code management system such as GitHub, it can provide tools for tracking your code, performing code revisions, backup your code to a remote repository, and revert back to previous versions of your code. If you are working together with a team it is also a perfect tool for collaborating with your colleagues on the code.
To follow along with this guide you will need to have the following two things in place:
Git installed on your system
- You can read the docs on how to install git here
An account on Github
- You can create an account here
First time git setup
If you haven’t already configured your GitHub account on your system, you will need to do so. To do this you can run the following two commands:
git config --global user.name "John Doe" git config --global user.email "[email protected]"
If you want to read more on initial configurations for git you can read the git docs.
Once you have set the username and email for your account, you are ready to initialize your module folder into source control.
Creating the repository on GitHub
The first thing I always do is creating a new repository in Github. Click on your profile picture in the right corner, and then click on “Your repositories”
Then once on your Repositories site, click on the New button
Now because I have already made my repository with my code on my local machine I will not initialize the repository on Github, instead, i will just create the Github repository and then initialize it locally on my machine.
Once you click on “Create Repository” you will be provided with a guide on how to initialize your local module.
In this case i will create a new repository from the commandline:
Initialising the repository from the commandline
first, I will open my Powershell terminal and navigate to the root of my module directory. from there I will run the following commands to initialize the repository
echo "# SCPSMeraki" >> README.md git init git add README.md git commit -m "first commit" git branch -M main git remote add origin https://github.com/ScriptingChris/SCPSMeraki.git git push -u origin main
Pushing your code to GitHub
To push the code to Github you can simply run the following commands from the root of your module:
git add . git commit -m "push all the code" git push
Developing in different branches
One of the top points of using source control is the ability to create multiple branches to develop on. A branch is like a complete copy of your entire repository, this gives you the ability to develop and mess with the entire code base without actually affecting any of your code in the “main” branch. So if you can always just delete the branch and create a new branch from the main branch.
So usually I will create a branch named “dev” make the changes to my code, for example, create a new cmdlet to the module or do some bug fixes. Once I am ready to actually add the code into the main branch I will create what’s called a Pull Request.
Creating a new branch
git checkout -b dev
To see which branches are available you can run the following command:
git branch --list
The branch with the ‘*’ is the current selected branch.
To change to the main branch again you can run the following:
git checkout main
Creating a Pull Request
There are two ways you can easily combine your code from the dev branch into the main branch.
The first way is through the command line. Make sure to first switch to the main branch and afterward merge the dev branch into the main branch.
git checkout main git merge dev
The second way is to create a Pull Request. This way you can do a code review and see all the changes you are merging into the main branch.
To best explain how a GitHub Pull Request works i strongly recommend you read the GitHub Docs – Pull Request