Skip to main content

Command Palette

Search for a command to run...

A git diff with all changed and new unstaged files

Updated
1 min readView as Markdown
A git diff with all changed and new unstaged files
T

A Web Developer based in Belgium, specialized in PHP & JS development

I recently needed to collect all changes in one file for reporting purposes. Found this article: https://ratfactor.com/cards/git-diff-with-new-files

In case you need something similar on Windows, here you go :-)

https://gist.github.com/lekoala/1821845aa85770939fde49517aca7a81

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"