Check if Working Directory is Clean in Git

Check if Working Directory is Clean in Git

A clean Git working directory has no uncommitted changes, meaning tracked files have no modifications, and no new files are waiting to be staged. Checking the working directory status is useful before creating commits, switching branches, or running automated deployment tasks. This tutorial shows how to check if working directory is clean in Git.

The git status --porcelain command produces a concise output describing changes in the working directory and staging area. When the output is empty, the working directory is clean. The following command checks the result and prints Clean if no changes are detected or Dirty if changes exist:

git status --porcelain | findstr . >nul && echo Dirty || echo Clean
test -z "$(git status --porcelain)" && echo 'Clean' || echo 'Dirty'

Leave a Comment

Cancel reply

Your email address will not be published.