Search code examples
gitgit-diffgit-fetchgit-status

Git: How to find whether origin/master is equivalent of (local) master?


git fetch goes over the network and fetches the latest snapshot from origin/master to the local host. It does not merge but just fetches the snapshot. Right after git fetch if we do git status, it will tell us whether (local) master is ahead or behind origin/master.

All I want is a git command that will just tell us whether the content of (local) master is the equivalent of the content of origin/master. Obviously, it has to go over the network to determine that but it should not print any messages about its findings (like the output of git fetch or any other git command) and finally, just output only yes/true (if equivalent) or no/false (if different) or something similar.

Is there any git internal commands or api's which could do this?!


Solution

  • I finally used the following snippet of code.

    output=`git fetch --dry-run 2>&1`
    
    if [ "$output" != "" ]; then
        echo "***********************************************"
        echo "*                    ERROR                    *"
        echo "* Your git source directory is not up to date *"
        echo "***********************************************"
    fi
    

    Had a problem parsing the output of git fetch --dry-run but found that the console messages belong to stderr Git: unable to redirect/parse the output of 'git fetch --dry-run' command