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

前端 未结 10 1611
温柔的废话
温柔的废话 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:36

    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;}'

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-04 14:47

    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.

    0 讨论(0)
  • 2020-12-04 14:50

    As mentioned in comments above this should be the best solution:

    $ git ls-remote <URL> | head -1 | cut -f 1

    0 讨论(0)
提交回复
热议问题