Does anyone know how to get the latest SHA of a given branch from outside a git repository?
If you are inside a git repository, you can do:
I recommend fetching info related only to a given branch, and then parse to get the latest sha:
git ls-remote <url> --tags <branch_name> | awk '{print $1;}'
Heres a copy-paste solution which works inside the repository.
origin_head=$(git ls-remote --heads $(git config --get remote.origin.url) | grep "refs/heads/master" | cut -f 1)
if [ $origin_head != "$(git rev-parse HEAD)" ]; then
echo >&2 "HEAD and origin/master differ."
exit 1
fi
This should do the trick git ls-remote REMOTE | awk "/BRANCH/ {print \$1}"
Replace REMOTE with the name of the remote repository and BRANCH with the name of the branch.
As mentioned in comments above this should be the best solution:
$ git ls-remote <URL> | head -1 | cut -f 1