Home > Net >  How to programatically detect if local and remote branches are different?
How to programatically detect if local and remote branches are different?

Time:11-18

I have a master branch, and I want to have a script that detects if the remote master has updated. I know I can do this:

git fetch

And now when I run

git diff master origin/master

I get input.

The question is, is the best way to detect this in a script is to analyze the textual output? Feels not so stable.. Any ideas?

CodePudding user response:

Use the --exit-code option

if git diff --exit-code master origin/master > /dev/null; then
   echo "Up to date"
else
   echo "Differences found"
fi

CodePudding user response:

Ok I figured that I can use the commit hash to verify this:

$ LOCAL=$(git rev-parse master)
$ REMOTE=$(git rev-parse origin/master)
if ! [ $LOCAL == $REMOTE ]; then
    echo "Remote has updated"
fi
  •  Tags:  
  • git
  • Related