Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Creating Git Commit Messages with PowerShell

Posted on June 21, 2016

As part of my process of learning an using Git I am trying to get in the habit of using meaningful commit messages. Sure, you can get by with a single line comment which is fine when running git log --oneline. But you can use a multi-line commit message. However, this requires a little planning which is probably not a bad thing. Because  my Git projects are PowerShell related and I most often and in the PowerShell ISE I came up with a little trick that works for me.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

In my PowerShell projects I try to include a change log file. The file lists what changes were made for each version which will always coincide with a commit. An entry might look like this:

v0.0.1
Import tasks from XML file
Get tasks from XML file with options.
added core module files

This is the exact text I want to include in my full commit message.  I don't want to have to a lot of copy and pasting or re-typing so I wrote a simple PowerShell function that creates my commit message.

#New-GitMessage.ps1

<#
Copy selected text into a here string that can be
used as a git commit message
#>

Function New-GitMessage {
[cmdletbinding()]

Param([string]$Text = $psise.CurrentFile.Editor.SelectedText)

#insert the selected text into a global variable.
$global:msg = $text

}
#create a shortcut in the ISE
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Commit Msg",{New-GitMessage},$null) | Out-Null

The script is intended to be used in the PowerShell ISE. The concept is very simple and probably doesn't really need a function.  The assumption is that I will select a portion of text in the changelog and run New-GitMessage command. All the command does is insert the text into a global variable. Even though everything in the ISE runs in the same scope I'm using the global prefix so my intentions are clear. When I'm ready to commit I can use an expression like this:

git commit -a -m $msg

Simple, but effective.

The last line in the script file adds an option to the AddOns menu. I'm not using a keyboard shortcut, but you could add one if you wish. In my PowerShell ISE profile script, I dot source this script file and I'm ready to go.

Here's the command in action.

image

This is hardly a groundbreaking bit of PowerShell code but I have found that every little shortcut I can take adds up in the long run.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d