Checking Git Merge Status

Categories: Programming, Git

One of the software applications I work on uses Git as version-control and has a traditional (if somewhat old-fashioned) branching strategy: a branch per feature/bugfix, an integration branch, a release-testing branch, and release branches. Different users/tools are responsible for merging between these branches so it can sometimes be rather difficult to know whether a local Git branch has been merged to any specific target.

I put the following very simple script in ~/bin/isgitmerged:

#!/bin/sh
DST=$1
SRC=$2
HASH=`git rev-parse $SRC`
COMMON=`git merge-base $DST $SRC`
if [ "$COMMON" = "$HASH" ]; then
  echo "merged"
else
  echo "not merged"
fi

Usage is simply:

  • git fetch --all
  • isgitmerged {targetbranch} {localbranch}

eg: isgitmerged origin/release/1234 bugfix/myfix