Skip to content

Instantly share code, notes, and snippets.

@codyromano
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyromano/f900de0afffcf75de289 to your computer and use it in GitHub Desktop.
Save codyromano/f900de0afffcf75de289 to your computer and use it in GitHub Desktop.
Get the hash for the HEAD of your branch in 12 lines of code. Purely client-side; no dependencies
Current hash for master: <span id="currentHash"></span>
<script>
/**
* This displays the commit hash for the HEAD of your chosen Git branch.
* It helps you stay on the same page with other team members during
* development. If their hash matches yours, your branches are in the same state.
*
* This is useful for web projects that don't follow a standard versioning process.
* If you have an automated process for keeping track of versions, this code may
* complement it.
*
* @param {String} branch The Git branch you care about (e.g. master or develop)
* @param {Object} el DOM element where the result should be displayed.
*/
(function (branch, el) {
var xmlRequestObj = new XMLHttpRequest;
xmlRequestObj.onreadystatechange = function () {
if (xmlRequestObj.readyState == 4) {
window.addEventListener('load', function () {
el.innerHTML = xmlRequestObj.responseText.slice(0, 10);
}, false);
}
};
xmlRequestObj.open('GET', '.git/refs/heads/' + branch);
xmlRequestObj.send();
})('master', document.getElementById('currentHash'));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment