是否有人知道如何获得一个给定分支 从外面的最新 SHA 一个 git 存储库?
如果您在 git 存储库中,您可以这样做:
git log origin/branch_X | head -1
但是,我并不在 git 存储库中,而且我希望避免仅仅为了获得标记/分支的最新 SHA 而必须使用 clone存储库。有什么聪明的办法吗?
clone
A colleague of mine answered this for me:
git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
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.
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.
--git-dir
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
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.
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
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>
As mentioned in comments above this should be the best solution:
$ git ls-remote <URL> | head -1 | cut -f 1
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;}'
git ls-remote <url> --tags <branch_name> | awk '{print $1;}'
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
with gituhb desktop, it's easy!
First go to your repository on github desktop initial screen after selecting a repository
Then go to History Hisotry of pushes in that repo
Then, right click on the push you want SHA key of, and then copy the SHA key, from the pop up menu.
Menu after right click, to get SHA key