Created
February 24, 2019 22:00
-
-
Save kpietralik/46bfcff49bf65c2e6ed4d1f5ab88f9c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Re-set git url's using new name | |
| $remoteName = "origin" | |
| $oldName = "AAA" | |
| $newName = "BBB" | |
| $folders = Get-ChildItem -Directory | |
| $renamedCount = 0 | |
| $reposCount = 0 | |
| foreach ($dir in $folders) | |
| { | |
| Write-Host("--------------------------------------") | |
| cd $dir | |
| Write-Host("Folder: $dir") | |
| # Test for git repo | |
| # hint: could also check for ".git" folder presence | |
| $testCmd = "git status" | |
| (Invoke-Expression -Command $testCmd -ErrorAction SilentlyContinue 2>1) | Out-Null | |
| if ($LASTEXITCODE -ne 0) | |
| { | |
| Write-Host("Not a git repository. Skipping...") | |
| cd .. | |
| continue | |
| } | |
| # Is git repo | |
| $reposCount++ | |
| $url = git remote get-url --all $remoteName | |
| Write-Host("Old url: $url") | |
| if (-NOT ($url -like "*$oldName*")) | |
| { | |
| Write-Host("Name not found in repo.") | |
| } | |
| # Re-setting remote's url | |
| $newUrl = $url -replace "$oldName", "$newName" | |
| Write-Host("Setting new url: $newUrl") | |
| git remote set-url $remoteName $newUrl | |
| $renamedCount++ | |
| cd .. | |
| } | |
| Write-Host("Script complete. Renamed $renamedCount out of $reposCount repositories in $($folders.Count) folders.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment