How do I programmatically determine if there are uncommited changes?

In a Makefile, I’d like to perform certain actions if there are uncommited changes (either in the working tree or the index). What’s the cleanest and most efficient way to do that? A command that exits with a return value of zero in one case and non-zero in the other would suit my purposes.

I can run git status and pipe the output through grep, but I feel like there must be a better way.

  • Git and Emacs23 is there a tutorial/guide?
  • Git error: Malformed input or input contains unmappable chacraters
  • Git: How to add files and subfolders in a gitignore'd folder
  • how do i find a list of files committed to a branch?
  • AWS Elastic Beanstalk (hook declined)
  • Commit without add and how to see remote branch log
  • A pretty and feature rich git GUI for Linux
  • How do I fix a failed git commit?
  • Git: can't undo local changes (error: path … is unmerged)
  • How can I unstage my files again after making a local commit?
  • How to get latest tag name?
  • Branch vs Tag.. Coming from SVN world
  • 7 Solutions collect form web for “How do I programmatically determine if there are uncommited changes?”

    UPDATE: the OP Daniel Stutzbach points out in the comments that this simple command git diff-index worked for him:

    git diff-index --quiet HEAD --

    You can then see “How to check if a command succeeded?” if you are using it in a bash script:

    git diff-index --quiet HEAD -- || echo "untracked"; // do something about it

    Note: as commented by Anthony Sottile

    git diff-index HEAD ... will fail on a branch which has no commits (such as a newly initialized repository).
    One workaround I’ve found is git diff-index $(git write-tree) ...

    “Programmatically” means never ever rely on porcelain commands.
    Always rely on plumbing commands.

    See also “Checking for a dirty index or untracked files with Git” for alternatives (like git status --porcelain)

    You can take inspiration from the new “require_clean_work_tree function” which is written as we speak 😉 (early October 2010)

    require_clean_work_tree () {
        # Update the index
        git update-index -q --ignore-submodules --refresh
        # Disallow unstaged changes in the working tree
        if ! git diff-files --quiet --ignore-submodules --
            echo >&2 "cannot $1: you have unstaged changes."
            git diff-files --name-status -r --ignore-submodules -- >&2
        # Disallow uncommitted changes in the index
        if ! git diff-index --cached --quiet HEAD --ignore-submodules --
            echo >&2 "cannot $1: your index contains uncommitted changes."
            git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2
        if [ $err = 1 ]
            echo >&2 "Please commit or stash them."
            exit 1

    While the other solutions are very thorough, if you want something really quick and dirty, try something like this:

    [[ -z $(git status -s) ]]

    It just checks if there is any output in the status summary.

    git diff --exit-code will return nonzero if there are any changes; git diff --quiet is the same with no output. Since you want to check for the working tree and the index, use

    git diff --quiet && git diff --cached --quiet


    git diff --quiet HEAD

    Either one will tell you if there are uncommitted changes that are staged or not.

    As pointed in other answer, as simple as such command is sufficient:

    git diff-index --quiet HEAD --

    If you omit the last two dashes, the command would fail if you have a file named HEAD.


    set -e
    echo -n "Checking if there are uncommited changes... "
    trap 'echo -e "\033[0;31mFAILED\033[0m"' ERR
    git diff-index --quiet HEAD --
    trap - ERR
    echo -e "\033[0;32mAll set!\033[0m"
    # continue as planned...

    Word of caution: this command ignores untracked files.

    Expanding on @Nepthar’s answer:

    if [[ -z $(git status -s) ]]
      echo "tree is clean"
      echo "tree is dirty, please commit changes before running this"

    With Python and GitPython:


    Returns True if repository is not clean

    Here is the best, cleanest way.

    function git_dirty {
        text=$(git status)
        changed_text="Changes to be committed"
        untracked_files="Untracked files"
        if [[ ${text} = *"$changed_text"* ]];then
        if [[ ${text} = *"$untracked_files"* ]];then
        echo $dirty
    Git Baby is a git and github fan, let's start git clone.