XOR Media

Git - Check to see if a Commit has been pushed

If you run in to a situation where you’d like to determine if a commit has been pushed to a remote git branch there’s a quick and easy command-line way to do so.

git branch -r --contains <committish>

The above command will list all of the remote branches that contain the commit in question. You may want to run a git fetch (or fetches) beforehand to make sure you’re up to date with the latest changes in all of the remotes.

If you’re asking yourself why would I need to know if a commit has been pushed, well I’m sure there are other uses, but I find it helpful in making sure that a commit has been pushed out to origin/master before I allow it to be built and packaged (and thus pushable to production.) I make use of fabric to manage hosts, do builds, and push code to those hosts. For the most part that’s a story for another time, but what is relevant is how I use the above command to make sure not to build something that isn’t safely in origin master.

local('git fetch origin')
local('git branch -r --contains {version} 2> /dev/null | grep -q "origin/master"'
      .format(version=version))

All you need to know about local is that it runs the command passed to it on the local machine and throws an exception if it fails. In this case we’re making use of it to pull down the latest code from origin and then to check and make sure the version (committish) we’re about to build exists in master there. If not we’ll abort the build.