Auto Update Git Aliases
Because I am lazy I wrote a script so I can update all my machines from the one location which is the gist below. This works in powershell core. Again this is mostly for me but feel free to use as needed.
Update your $profile
to have th following environment variable set. This will allows the wid
and pid
aliases to work correctly.
$env:GIT_PERSONAL_EMAIL = "personal@personal.com"
$env:GIT_PERSONAL_USERNAME = "myname"
$env:GIT_WORK_EMAIL = "work@work.com"
$env:GIT_WORK_USERNAME = "myworknameoralias"
Script to update aliases. Add to your profile or a function in your profile that you can call when you want.
$gitConfig = Get-Content $env:userprofile\.gitconfig
$hasAliasInclude = $false
$aliasIncFile = "$env:userprofile\alias.inc"
# Update inc.
if((Test-Path $aliasIncFile)) {
Remove-Item -Path $aliasIncFile -Force | Out-Null
}
Write-Host "Updating the Alias Include"
Invoke-WebRequest -UseBasicParsing -Uri "https://gist.githubusercontent.com/sytone/03d9b90abb936830253f60f7f40a1a06/raw/.gitconfig" -OutFile $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERPERSONALEMAILADDRESS", $env:GIT_PERSONAL_EMAIL } | Set-Content $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERPERSONALUSERNAME", $env:GIT_PERSONAL_USERNAME } | Set-Content $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERWORKEMAILADDRESS", $env:GIT_WORK_EMAIL } | Set-Content $aliasIncFile
(Get-Content $aliasIncFile) | Foreach-Object { $_ -replace "ENTERWORKUSERNAME", $env:GIT_WORK_USERNAME } | Set-Content $aliasIncFile
for ($i = 0; $i -lt $gitConfig.Count; $i++) {
if ($gitConfig[$i].Contains("[include]") -and $gitConfig[$i+1].Contains("path = alias.inc")) {
Write-Host "Found include, skipping add."
$hasAliasInclude = $true
}
}
if (!$hasAliasInclude) {
Write-Host "Missing include, adding."
$includeStatement = @"
[include]
`tpath = alias.inc
"@
$includeStatement | Add-Content $env:userprofile\.gitconfig
}