[英]How to get SHA of the latest commit from remote git repository?
Does anyone know how to get the latest SHA of a given branch from outside a 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 clone
a repository just to get the latest SHA of a tag/branch.但是,我不在 git 存储库中,我想避免为了获取标签/分支的最新 SHA 而必须clone
存储库。 Is there a clever way of doing this?有没有聪明的方法来做到这一点?
If you want to check SHA-1 of given branch in remote repository, then your answer is 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-dir
”选项的说明,请参阅git(1)联机帮助页。
A colleague of mine answered this for me:我的一位同事为我回答了这个问题:
git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
Using a git URL:使用 git 网址:
$ 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>
As mentioned in comments above this should be the best solution:正如上面评论中提到的,这应该是最好的解决方案:
$ git ls-remote <URL> | head -1 | cut -f 1
This should do the trick git ls-remote REMOTE | awk "/BRANCH/ {print \$1}"
这应该可以解决问题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 替换为分支的名称。
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
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
References to branch heads are stored in the .git/refs/
tree.对分支头的引用存储在.git/refs/
树中。 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.您的路径可能略有不同。
I recommend fetching info related only to a given branch, and then parse to get the latest sha:我建议获取仅与给定分支相关的信息,然后解析以获取最新的 sha:
git ls-remote <url> --tags <branch_name> | awk '{print $1;}'
with gituhb desktop, it's easy!使用 gituhb 桌面,很简单!
First go to your repository on github desktop initial screen after selecting a repository选择存储库后,首先转到 github 桌面初始屏幕上的存储库
Then go to History Hisotry of pushes in that repo然后转到该回购中推送的 History Hisotry
Then, right click on the push you want SHA key of, and then copy the SHA key, from the pop up menu.然后,右键单击您想要 SHA 密钥的推送,然后从弹出菜单中复制 SHA 密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.