代码之家  ›  专栏  ›  技术社区  ›  Daniel Stutzbach Edward Leno

如何以编程方式确定是否存在未提交的更改?

git
  •  182
  • Daniel Stutzbach Edward Leno  · 技术社区  · 14 年前

    在makefile中,如果有未提交的更改(在工作树或索引中),我希望执行某些操作。最干净和最有效的方法是什么?在一种情况下,返回值为零而在另一种情况下返回值为非零的命令将适合我的目的。

    我能跑 git status 通过管道输出 grep 但我觉得一定有更好的方法。

    8 回复  |  直到 6 年前
        1
  •  234
  •   VonC    7 年前

    Daniel Stutzbach in the comments git diff-index

    git diff-index --quiet HEAD --
    

    How to check if a command succeeded?

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

    commented Anthony Sottile

    git diff-index HEAD ...
    git diff-index $(git write-tree) ...


    porcelain commands
    plumbing commands

    Checking for a dirty index or untracked files with Git git status --porcelain

    from the new " require_clean_work_tree function " which is written as we speak

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

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

        3
  •  51
  •   Josh Lee ZZ Coder    14 年前

    git diff --exit-code git diff --quiet

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

    git diff --quiet HEAD
    

        4
  •  13
  •   Travis Reeder    8 年前

    if [[ -z $(git status -s) ]]
    then
      echo "tree is clean"
    else
      echo "tree is dirty, please commit changes before running this"
      exit
    fi
    
        5
  •  4
  •   Jeff Handley    8 年前

    git diff-index --quiet HEAD --
    

    HEAD

    #!/bin/bash
    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...
    

        6
  •  2
  •   stk    6 年前

    git config --global alias.unstaged 'diff --name-only'
    git config --global alias.staged 'diff --name-only --cached'
    

    [[ -n "$(git unstaged)" ]] && echo unstaged files || echo NO unstaged files
    [[ -n "$(git staged)" ]] && echo staged files || echo NO staged files
    

    PATH git-has

    #!/bin/bash
    [[ $(git "$@" | wc -c) -ne 0 ]]
    

    git has unstaged && echo unstaged files || echo NO unstaged files
    git has staged && echo staged files || echo NO staged files
    

    git config --global alias.untracked 'ls-files --exclude-standard --others'
    git config --global alias.ignored 'ls-files --exclude-standard --others --ignored'
    
        7
  •  0
  •   Pablo    8 年前

    git.Repo(path).is_dirty(untracked_files=True)
    

        8
  •  -3
  •   codyc4321    8 年前

    function git_dirty {
        text=$(git status)
        changed_text="Changes to be committed"
        untracked_files="Untracked files"
    
        dirty=false
    
        if [[ ${text} = *"$changed_text"* ]];then
            dirty=true
        fi
    
        if [[ ${text} = *"$untracked_files"* ]];then
            dirty=true
        fi
    
        echo $dirty
    }