W3docs

How to Programmatically Determine if there are Uncommitted Changes in Git

If you’re searching for a right answer to the question of programmatically determining whether there are uncommitted changes or not, check this tutorial.

Sometimes, it is necessary to get the uncommitted changes of the working directory inside a script. The git status command outputs too much information that may not be necessary. Hence, the aim of this article is to check whether there are uncommitted changes or not.

Discovering uncommitted changes

In this section, we will show you how to discover the uncommitted changes of your working directory. Below, we consider several handy ways to do that.

Using the git diff-index command

You can get your uncommitted changes with the <kbd class="highlighted">git diff-index</kbd> command in the following way:

git diff-index

git diff-index HEAD --
Warning

The <kbd>git diff-index HEAD</kbd> command will fail on a branch without commits such as a newly created repository. note

This command only detects changes to tracked files. Untracked files are ignored.

Here is a small example of a bash script that determines whether there are uncommitted changes or not.

set -e
echo -n "Checking if there are uncommitted changes... "
trap 'echo -e "\033[0;31mCHANGED\033[0m"' ERR
git diff-index --quiet HEAD --
trap - ERR
echo -e "\033[0;32mUNCHANGED\033[0m"

Using the git status command

Also, you can use the git status command with the <kbd class="highlighted">--porcelain</kbd> option to get a clearer output:

git status

git status --porcelain
Info

The <kbd>--porcelain</kbd> flag gives the output in an easy-to-parse format for scripts, which is similar to the short output. However, it will remain stable across Git versions regardless of user configuration.

Using the git diff command

Also, you can use the git diff command with the <kbd class="highlighted">--name-only</kbd> attribute to list the changed files:

git diff --name-only

To programmatically check for uncommitted changes, you can test if the output is empty:

if [ -n "$(git diff --name-only)" ]; then
  echo "CHANGED"
else
  echo "UNCHANGED"
fi

The git diff-index Command

The <kbd class="highlighted">git diff-index</kbd> command is used for comparing the content mode of the blobs, detected inside a tree object with the matching tracked files within the working tree, or with the matching paths inside the index. In the case of the presence of the <paths> arguments, only the paths matching the patterns are compared. In other scenarios, all the tracked files are compared.

The git status Command

The <kbd class="highlighted">git status</kbd> command is used for demonstrating the state of the working directory and the staging area. It allows viewing the staged changes and the files that aren’t being tracked by Git. The Status output does not display any information about the committed project history. For this purpose, it is recommended to use the git log command. The <kbd class="highlighted">git status</kbd> command simply shows what has been going on with git add and git commit commands.

The git diff Command

The <kbd class="highlighted">git diff</kbd> is a multi-function Git command, which is used for comparing the committed changes. With the help of this command, you can take two input data sets and output the modifications between them. While executing, this command runs a diff function on Git data source. Commonly, it is used in combination with git status and git log commands to analyze the state of a git repository.