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

后端 未结 9 1148
既然无缘
既然无缘 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:40

    If you are like me, you want to know if there are:

    1) changes to existing files 2) newly added files 3) deleted files

    and specifically do not want to know about 4) untracked files.

    This should do it:

    git status --untracked-files=no --porcelain
    

    Here's my bash code to exit the script if the repo is clean. It uses the short version of the untracked files option:

    [[ -z $(git status -uno --porcelain) ]] && echo "this branch is clean, no need to push..." && kill -SIGINT $$;
    

提交回复
热议问题