Auto Update Git Aliases

Posted by Jon Bullen on 2019-04-02
Estimated Reading Time 1 Minutes
Words 277 In Total

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_WORK_EMAIL = ""
$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\"

# Update inc. 
if((Test-Path $aliasIncFile)) {
    Remove-Item -Path $aliasIncFile -Force | Out-Null
Write-Host "Updating the Alias Include"
Invoke-WebRequest -UseBasicParsing -Uri "" -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 =")) {
        Write-Host "Found include, skipping add."
        $hasAliasInclude = $true

if (!$hasAliasInclude) {
    Write-Host "Missing include, adding."
    $includeStatement = @"
`tpath =
    $includeStatement | Add-Content $env:userprofile\.gitconfig

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you!