Skip to content

Instantly share code, notes, and snippets.

@tahir-hassan
Created May 25, 2025 15:08
Show Gist options
  • Save tahir-hassan/f7cd687e9b270a23bc8f8317c1c5e05b to your computer and use it in GitHub Desktop.
Save tahir-hassan/f7cd687e9b270a23bc8f8317c1c5e05b to your computer and use it in GitHub Desktop.
PowerShell Script to copy new and modified files from a Git repo into another folder.
function Copy-GitChanges {
param (
[string]$Source,
[string]$Destination
)
if (!(Test-Path $Source)) {
throw "Source path '$Source' does not exist."
}
if (!(Test-Path (Join-Path $Source ".git"))) {
throw "'$Source' is not a Git repository."
}
Push-Location $Source
try {
# Get modified and untracked files
$files = git ls-files --modified --others --exclude-standard
if (!(Test-Path $Destination)) {
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
}
foreach ($file in $files) {
$sourceFilePath = Join-Path $Source $file
$fileDestination = Join-Path $Destination $file
$fileDestinationParent = Split-Path $fileDestination -Parent
if (!(Test-Path $fileDestinationParent)) {
New-Item -ItemType Directory -Path $fileDestinationParent -Force | Out-Null
}
Copy-Item -Path $sourceFilePath -Destination $fileDestination -Force -Verbose
}
}
finally {
Pop-Location
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment