Jepessen Jepessen - 8 days ago 4
Git Question

Check if git status is clean inside a Powershell script

I'm developing a Powershell script for create a program release.
In order to do it I want to check if the git working tree is clean and there are no uncommitted changes.

I've read this post but it's related to bash.

How can I check from inside a powershell script if the git working tree is clean and there's anything to commit? I don't care about untracked files.

I've tried something like

$gitOutput = (git status -z) | Out-String
if ($gitOutput) {
} else {
"no Output"

The problem is that I print
also when everything is committed but there are some untracked files, that's something that I'd like to avoid. I want to ignore untracked files and check only if all tracked files are committed.


Use git status --porcelain to get the output as a list that's easy to parse.

Untracked files are prefixed with the status ??, so you can easily filter those out and then check if there are uncommitted changes based on whether there is any output left:

if(git status --porcelain |Where {$_ -match '^\?\?'}){
    # not clean
else {
    # tree is clean