PowerShell Friday: Backup, source control, versioning
You finally created that script that saves you a lot of time. Suddenly it doesn’t work anymore. Now the panic slowly kicks in. Where did you put that backup? Did you backup it? Or did you forget?
The Solution
The solution is simple, of course: Make backups. Lots of them. But how do you keep them all apart from each other? Which file goes with which other file? And what do you do before you are finished? The real solution is to use a version or source control system.
Version Control
Version control is a system that records all changes to a file, so that you can retrieve specific versions later. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a version control system also generally means that if you screw things up or lose files, you can easily recover.
Microsoft has written an article on protecting your PowerShell scripts with version control , but not everybody wants to use Team Foundation Server or has access to Visual Studio. Luckily for us there are alternatives, like the open source Git with Posh-Git.
If you are one of those that think: “Version Control is for programmers/developers, but not for me”, think again. When you write larger scripts you should at least want to need a way to keep track of your changes. You should be wanting to be in control of your source, hence: Git
Git Basics
There are a lot of good (e-) books on Git, so I won’t go into too much details. There are a couple of basics you have to learn. The following is from the Getting Started portion of the Git website.
Git thinks of its data more like a set of snapshots of a miniature filesystem. Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. To be efficient, if files have not changed, Git doesn’t store the file again, just a link to the previous identical file it has already stored. Git thinks about its data more like a stream of snapshots.
and
Git has three main states that your files can reside in: committed, modified, and staged. Committed means that the data is safely stored in your local database. Modified means that you have changed the file but have not committed it to your database yet. Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
Installation of Git
Step 1: Download Git from https://git-scm.com/download/win and install it.
If you’re using version 5 or higher of PowerShell you can use the following line to install posh-git
Find-module posh-git | install-module
You need administrator privileges to install posh-git.
If you are using a lower version of PowerShell you can get PoSh-Git with:
(new-object Net.WebClient).DownloadString(“http://psget.net/GetPsGet.ps1") | iex
for installing PSGet, and to install posh-git itself:
install-module posh-git