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

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

    If you just want the SHA-1 from the currently checked out branch of your local repo, you can just specify HEAD instead of origin/branch_X:

    git --git-dir=/path/to/repo/.git rev-parse --verify HEAD

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

    Use rev-parse

    git rev-parse origin/master # to get the latest commit on the remote
    
    git rev-parse HEAD          # to get the latest commit on the local 
    
    0 讨论(0)
  • 2020-12-04 14:28

    References to branch heads are stored in the .git/refs/ tree. So you should be able to find the hash of the latest commit at:

    cat .git/refs/remotes/origin/branch_X
    

    Your path may differ slightly.

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

    Using a git URL:

    $ git ls-remote <URL> | head -1 | sed "s/HEAD//"
    

    Using a directory on an accessible system:

    $ git --git-dir=/path/to/repo/.git rev-parse origin/<targeted-banch>
    
    0 讨论(0)
  • 2020-12-04 14:32

    A colleague of mine answered this for me:

    git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
    
    0 讨论(0)
  • 2020-12-04 14:33

    If you want to check SHA-1 of given branch in remote repository, then your answer is correct:

    $ git ls-remote <URL>
    

    However if you are on the same filesystem simpler solution (not requiring to extract SHA-1 from output) would be simply:

    $ git --git-dir=/path/to/repo/.git rev-parse origin/branch_X
    

    See git(1) manpage for description of '--git-dir' option.

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