How to get SHA of the latest commit from remote git repository?

前端 未结 10 1609
温柔的废话
温柔的废话 2020-12-04 13:58

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:

10条回答
  •  抹茶落季
    2020-12-04 14:41

    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
    

提交回复
热议问题