How to check if there's nothing to be committed in the current branch?

后端 未结 9 1132
既然无缘
既然无缘 2020-12-22 15:57

The goal is to get an unambiguous status that can be evaluated in a shell command.

I tried git status but it always returns 0, even if there are items

9条回答
  •  [愿得一人]
    2020-12-22 16:38

    It's possible to combine git status --porcelain with a simple grep to perform the test.

    if git status --porcelain | grep .; then
        echo Repo is dirty
    else
        echo Repo is clean
    fi
    

    I use this as a simple one-liner sometimes:

    # pull from origin if our repo is clean
    git status --porcelain | grep . || git pull origin master
    

    Add -qs to your grep command to make it silent.

提交回复
热议问题