简体   繁体   English

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

[英]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?有没有聪明的方法来做到这一点?

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 

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 桌面,很简单!

  1. First go to your repository on github desktop initial screen after selecting a repository选择存储库后,首先转到 github 桌面初始屏幕上的存储库

  2. Then go to History Hisotry of pushes in that repo然后转到该回购中推送的 History Hisotry

  3. Then, right click on the push you want SHA key of, and then copy the SHA key, from the pop up menu.然后,右键单击您想要 SHA 密钥的推送,然后从弹出菜单中复制 SHA 密钥。

Menu after right click, to get SHA key右键菜单,获取SHA key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从远程存储库获取特定分支中的最后一次提交SHA - How to get last commit SHA in a specific branch from a remote repository 如何在 Windows 中提取远程 git 存储库的最新提交哈希? - How to extract latest commit hash for a remote git repository in Windows? 如何从远程获取最新的提交哈希? - How to get latest commit hash from remote? 如何提交到远程 git 存储库 - How to commit to remote git repository Git推送到远程存储库,丢弃最新提交并替换为本地提交 - Git push to a remote repository, discard the latest commit and replace with local commit 如何从远程存储库获取最新更新,然后稍后添加并提交我的文件? - How to get latest update from remote repository then add and commit my file later? 如何从git存储库中获取特定文件夹的特定提交,同时保持其余文件夹为最新? - How to get a specific commit of a particular folder from a git repository while keeping rest of the folders as latest? 如何从git中的remote / master转到最新的提交? - How do I go to the latest commit from remote/master in git? 如何为远程存储库中的提交哈希获取git标签? - How to get git tag for a commit hash in a remote repository? 如何从远程git存储库一次提取一个提交? - How to pull one commit at a time from a remote git repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM