如何从远程 git 存储库获取最新提交的 SHA?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1161869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:46:28  来源:igfitidea点击:

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

git

提问by AdvilUser

Does anyone know how to get the latest SHA of a given branch from outsidea git repository?

有谁知道如何git 存储库外部获取给定分支的最新 SHA ?

If you are inside a git repository, you can do:

如果您在 git 存储库中,则可以执行以下操作:

git log origin/branch_X | head -1

However, I am not inside a git repository, and I would like to avoid having to clonea repository just to get the latest SHA of a tag/branch. Is there a clever way of doing this?

但是,我不在 git 存储库中,我想避免clone为了获取标签/分支的最新 SHA 而不得不使用存储库。有没有聪明的方法来做到这一点?

回答by gprasant

Use rev-parse

使用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 

回答by Jakub Nar?bski

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

如果您想检查远程存储库中给定分支的 SHA-1,那么您的答案是正确的:

$ 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:

但是,如果您在同一个文件系统上,更简单的解决方案(不需要从输出中提取 SHA-1)将很简单:

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

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

有关“ ”选项的说明,请参见git(1)联机帮助页--git-dir

回答by AdvilUser

A colleague of mine answered this for me:

我的一个同事为我回答了这个问题:

git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>

回答by dch4pm4n

Using a git URL:

使用 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>

回答by antonagestam

This should do the trick git ls-remote REMOTE | awk "/BRANCH/ {print \$1}"

这应该可以解决问题 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.

将 REMOTE 替换为远程存储库的名称,将 BRANCH 替换为分支的名称。

回答by kitingChris

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

正如上面的评论中提到的,这应该是最好的解决方案:

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

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

回答by kitingChris

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:

如果您只想要本地存储库当前签出的分支中的 SHA-1,则可以指定 HEAD 而不是 origin/branch_X:

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

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

回答by skensell

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

回答by Greg Hewgill

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:

对分支头的引用存储在.git/refs/树中。因此,您应该能够在以下位置找到最新提交的哈希值:

cat .git/refs/remotes/origin/branch_X

Your path may differ slightly.

您的路径可能略有不同。

回答by Ana Marjanica

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

我建议获取仅与给定分支相关的信息,然后解析以获取最新的 sha:
git ls-remote <url> --tags <branch_name> | awk '{print $1;}'