Skip to content

Instantly share code, notes, and snippets.

@lekoala
Created July 27, 2026 14:19
Show Gist options
  • Select an option

  • Save lekoala/1821845aa85770939fde49517aca7a81 to your computer and use it in GitHub Desktop.

Select an option

Save lekoala/1821845aa85770939fde49517aca7a81 to your computer and use it in GitHub Desktop.
gdiff.ps1
param(
[string]$Output = "changes.txt"
)
$ErrorActionPreference = "Stop"
# Make sure we're in a Git repository.
git rev-parse --is-inside-work-tree *> $null
if ($LASTEXITCODE -ne 0) {
throw "Not inside a Git repository."
}
# Do this before creating the output file, otherwise changes.txt itself
# could appear as a new untracked file.
$untracked = @(
git ls-files --others --exclude-standard
)
try {
# Make untracked files visible to `git diff` without staging their content.
if ($untracked.Count -gt 0) {
git add --intent-to-add -- $untracked
if ($LASTEXITCODE -ne 0) {
throw "git add --intent-to-add failed."
}
}
# HEAD includes both staged and unstaged changes to tracked files,
# plus the intent-to-add files above.
git --no-pager diff --binary HEAD --output="$Output"
if ($LASTEXITCODE -ne 0) {
throw "git diff failed."
}
}
finally {
# Restore untracked files to their original untracked state.
if ($untracked.Count -gt 0) {
git reset --quiet HEAD -- $untracked
}
}
$fullPath = (Resolve-Path $Output).Path
Write-Host "Diff written to: $fullPath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment