Skip to content

Instantly share code, notes, and snippets.

@milnak
Created November 10, 2025 01:27
Show Gist options
  • Select an option

  • Save milnak/377f61a01dd7c59e00fd5c616018a4b6 to your computer and use it in GitHub Desktop.

Select an option

Save milnak/377f61a01dd7c59e00fd5c616018a4b6 to your computer and use it in GitHub Desktop.
Outputs the Git configuration grouped by sections and sorted.
<#
.DESCRIPTION
Formats and outputs the Git configuration in a structured manner.
.EXAMPLE
.\Format-GitConfig.ps1
Outputs the Git configuration grouped by sections and sorted.
#>
$config = foreach ($line in git.exe --no-pager config --global --list | Sort-Object -Unique) {
if ($line -match '^\s*(?<section>\w*)\.(?<subsection>\w*)\.(?<key>\w*)\s*=\s*(?<value>.+)') {
# e.g. difftool.winmerge.name=WinMerge
[PSCustomObject]@{
Section = $matches['section']
Subsection = $matches['subsection']
Key = $matches['key']
Value = $matches['value']
}
}
elseif ($line -match '^\s*(?<section>\w*)\.(?<key>\w*)\s*=\s*(?<value>.+)') {
# e.g. color.ui=auto
[PSCustomObject]@{
Section = $matches['section']
Subsection = $null
Key = $matches['key']
Value = $matches['value']
}
}
else {
Write-Warning "Invalid line? $line"
}
}
# $config | Sort-Object Section, Variable | Format-List; return
foreach ($line in $config | Group-Object -Property Section, SubSection | Sort-Object Name) {
$section, $subsection = $line.Group[0].Section, $line.Group[0].Subsection
if ($subsection) {
"[$section ""$subsection""]"
}
else {
"[$section]"
}
foreach ($item in $line.Group) {
"`t{0} = {1}" -f $item.Key, $item.Value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment