Skip to content

Instantly share code, notes, and snippets.

@mhamri
Last active August 19, 2021 03:59
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 mhamri/042fb3960e48eabd8fa277375a425c43 to your computer and use it in GitHub Desktop.
Save mhamri/042fb3960e48eabd8fa277375a425c43 to your computer and use it in GitHub Desktop.
git mv any folder with it's sub folder to another folder in windows powershell
# git mv a folder and sub folders in windows
function Move-GitFolder {
param (
$target,
$destination
)
if (Test-Path $target -PathType Leaf){
write-output "it's a file ... "
git mv $target $destination
exit
}
Get-ChildItem $target -recurse |
Where-Object { ! $_.PSIsContainer } |
ForEach-Object {
$fullTargetFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $target))
$fullDestinationFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $destination))
$fileDestination = $_.Directory.FullName.Replace($fullTargetFolder.TrimEnd('\'), $fullDestinationFolder.TrimEnd('\'))
New-Item -ItemType Directory -Force -Path $fileDestination | Out-Null
$filePath = Join-Path $fileDestination $_.Name
git mv $_.FullName $filePath
}
Remove-Item -Recurse -Force $target
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment