Last active
January 5, 2022 19:15
-
-
Save sgaulding/33b5f47bc610f9ccfd2d1ac1dbf77cc8 to your computer and use it in GitHub Desktop.
PowerShell Script to combine GIT repositories and keep their history
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
$ErrorActionPreference = "Stop" | |
$repos = ( | |
'', | |
'' | |
) | |
$baseUrl = '' | |
$currentDir = '' | |
$tempDir = '' | |
Set-Location $currentDir | |
foreach ($repo in $repos) { | |
$url = $baseUrl + $repo + '.git' | |
$tempDirRepo = $tempDir + $repo | |
$moveDirRepo = $tempDirRepo + '\' + $repo + '-repo' | |
Set-Location $tempDir | |
git clone $url $repo | |
Set-Location $repo | |
git cob main | |
Set-Location $tempDirRepo | |
New-Item $moveDirRepo -ItemType "directory" | |
$filesToMove = Get-ChildItem *.* -File -Exclude .\.git\, $tempDirRepo | |
$dirsToMove = Get-ChildItem -Directory -Exclude .\.git\, $tempDirRepo | |
if ($filesToMove) { | |
Move-Item $filesToMove $moveDirRepo | |
} | |
foreach ($directory in $dirsToMove) { | |
if ($directory.ToString() -like '*-repo') { | |
continue | |
} | |
Move-Item $directory.ToString() $moveDirRepo | |
} | |
$comment = 'Move to Repo Folder ' + $repo | |
git cm $comment | |
Set-Location $currentDir | |
git remote add $repo $tempDirRepo | |
git fetch $repo --tags | |
git merge --allow-unrelated-histories $repo/main | |
git remote remove $repo | |
Remove-Item $tempDirRepo -Force -Recurse | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment