XOR Media

Coding, Operations, Etc.

Git: Check to see if a Commit has been pushed

Posted on Tue 15 May 2012 by

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.

In misc, tagged: command-line, git, and linux.

About the Author

Ross McFarland Ross McFarland | | |

Ross is a 17 year veteran of the software industry with experience spanning low-level signal processing, web and mobile user interfaces, high-scale distributed web services, infrastructure, and networking. He has made extensive contributions to open source highlighted by his time as a primary maintainer of Gtk2-Perl and author of requests-futures and python-asynchttp libraries. (more)