Skip to content

Instantly share code, notes, and snippets.

@sgaulding
Last active January 5, 2022 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgaulding/33b5f47bc610f9ccfd2d1ac1dbf77cc8 to your computer and use it in GitHub Desktop.
Save sgaulding/33b5f47bc610f9ccfd2d1ac1dbf77cc8 to your computer and use it in GitHub Desktop.
PowerShell Script to combine GIT repositories and keep their history
$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