Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created April 24, 2026 17:38
Show Gist options
  • Select an option

  • Save steviecoaster/752c72c31f7f6d307176bcd4b5305886 to your computer and use it in GitHub Desktop.

Select an option

Save steviecoaster/752c72c31f7f6d307176bcd4b5305886 to your computer and use it in GitHub Desktop.
Git Config backup
function Import-GitConfig {
<#
.SYNOPSIS
Restores git configuration from a CliXml file created by Save-GitConfig.
.DESCRIPTION
Reads a CliXml file produced by Save-GitConfig and applies each
key-value pair using 'git config'. Global settings are applied with
'--global' and all other settings with '--local'. The 'Global'
property on each object is used only to determine scope and is not
written as a git config key.
.PARAMETER InputFile
Path to the CliXml file previously created by Save-GitConfig.
.EXAMPLE
Import-GitConfig -InputFile C:\Backup\my-gitconfig.xml
Restores git settings from the specified file.
.EXAMPLE
Import-GitConfig -InputFile .gitconfig.export -WhatIf
Previews all git config commands that would be run without applying them.
#>
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory)]
[String]
$InputFile
)
end {
$collection = Import-Clixml -Path $InputFile
foreach ($entry in $collection) {
$scope = if ($entry.Global) { '--global' } else { '--local' }
$entry.PSObject.Properties |
Where-Object { $_.Name -ne 'Global' } |
ForEach-Object {
if ($PSCmdlet.ShouldProcess("git config $scope $($_.Name) '$($_.Value)'")) {
git config $scope $_.Name $_.Value
}
}
}
}
}
function Save-GitConfig {
<#
.SYNOPSIS
Exports the current git configuration to a CliXml file.
.DESCRIPTION
Collects global and all-scope git configuration settings using
'git config --show-origin', and serializes them as two PSCustomObjects
(one with Global = $true, one with Global = $false) into a CliXml file
for later restoration with Import-GitConfig.
.PARAMETER OutputFile
Path to the CliXml file to write. Defaults to
'$env:USERPROFILE\Documents\.gitconfig.export'.
.OUTPUTS
System.IO.FileInfo. The exported file.
.EXAMPLE
Save-GitConfig
Exports git config to the default path.
.EXAMPLE
Save-GitConfig -OutputFile C:\Backup\my-gitconfig.xml
Exports git config to a custom path.
#>
[CmdletBinding()]
Param(
[Parameter()]
[String]
$outputFile = (Join-Path $env:USERPROFILE -ChildPath 'Documents\.gitconfig.export')
)
end {
$collection = [System.Collections.Generic.List[pscustomobject]]::new()
$globalHash = [ordered]@{ Global = $true }
git config --global --list --show-origin | ForEach-Object {
if ($_ -match '^[^\t]+\t([^=]+)=(.*)$') {
$globalHash[$matches[1]] = $matches[2]
}
}
$collection.Add([pscustomobject]$globalHash)
$localHash = [ordered]@{ Global = $false }
git config --list --show-origin | ForEach-Object {
if ($_ -match '^[^\t]+\t([^=]+)=(.*)$') {
$localHash[$matches[1]] = $Matches[2]
}
}
$collection.Add([pscustomobject]$localHash)
$collection | Export-Clixml -Path $outputFile -Force
Get-Item $outputFile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment