Git: checking efficiently that work tree, staging area, local repo and remote repo all match

  Kiến thức lập trình

I regularly work on the same set of git repos on multiple computers. Both at the beginning of a work session, and at the end of a work session, I run a script which will report if any of the git repos on the computer have “unstaged,” “uncommitted,” “unpushed” or “unpulled” contents. I guess using better git terminology I want to make sure that on that computer,

“work tree” = “staging area” = “local repo” = “remote repo”,

Some time ago I parsed, from sources I can no longer remember, a script that seems to accomplish this task. In the script below, environment variable GITS_DB refers to a text file which contains one line for each directory containing a git repository.


NC='33[0m' # no color

for g in $(< ${GITS_DB})
        cd ${g}
        git fetch --all &> /dev/null || 
            echo "warning: unable to fetch ${g}, getting local status"
        if [ $(git status --porcelain | wc -l) != "0" 
            -o $(git rev-list HEAD@{upstream}..HEAD | wc -l) != "0" 
            -o $(git rev-list HEAD..HEAD@{upstream} | wc -l) != "0" ];  then
            echo -e "${RED}${g} is not clean${NC}"

If the script reports that a repo is not clean, I cd into the repo and fix the issue. Fixing could mean commit, push or pull.

I have used this script for some time, and it seems to work. However, it is quite slow, even with a fast network connection. So

Is there a faster (simpler) way to achieve what the script does?