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

Git

Git Problem Overview


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:

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. Is there a clever way of doing this?

Git Solutions


Solution 1 - Git

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 

Solution 2 - Git

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.

Solution 3 - Git

A colleague of mine answered this for me:

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

Solution 4 - Git

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>

Solution 5 - Git

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

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

Solution 6 - Git

This should do the trick 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.

Solution 7 - Git

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

Solution 8 - Git

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

Solution 9 - Git

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.

Solution 10 - Git

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

Solution 11 - Git

with gituhb desktop, it's easy!

  1. First go to your repository on github desktop initial screen after selecting a repository

  2. Then go to History Hisotry of pushes in that repo

  3. 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdvilUserView Question on Stackoverflow
Solution 1 - GitgprasantView Answer on Stackoverflow
Solution 2 - GitJakub NarębskiView Answer on Stackoverflow
Solution 3 - GitAdvilUserView Answer on Stackoverflow
Solution 4 - Gitdch4pm4nView Answer on Stackoverflow
Solution 5 - GitkitingChrisView Answer on Stackoverflow
Solution 6 - GitantonagestamView Answer on Stackoverflow
Solution 7 - GitView Answer on Stackoverflow
Solution 8 - GitskensellView Answer on Stackoverflow
Solution 9 - GitGreg HewgillView Answer on Stackoverflow
Solution 10 - GitanamarView Answer on Stackoverflow
Solution 11 - Gitsajib_w3b_d3vView Answer on Stackoverflow