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.
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.
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.