84

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?

T. Zengerink
  • 4,189
  • 5
  • 29
  • 31
AdvilUser
  • 2,822
  • 2
  • 23
  • 15

11 Answers11

113

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 
gprasant
  • 14,691
  • 8
  • 42
  • 56
  • 1
    +1 Proved most useful along with appending | clip to drop the sha onto the clipboard (windows). – Darren Lewis Oct 24 '13 at 09:31
  • And for those mac users `git rev-parse origin/master | pbcopy` will drop the commit sha (with a `\n`) on your clipboard for you to paste at your convenience. – Aaron Dec 27 '13 at 20:56
  • 1
    `git rev-parse HEAD` returns the latest commit in the local copy, while `git rev-parse origin/master` returns the latest commit on remote, which is what's been asked here. This is my favorite answer, even if first command should be removed. – fedelibre Sep 20 '15 at 01:47
  • 2
    The question notes "I would like to avoid having to clone a repository just to get the latest SHA of a tag/branch". Is it possible to use `git rev-parse` against a repository that you don't have any information for locally? – Nick Chammas Nov 25 '15 at 04:38
  • 14
    I think git rev-parse origin/master only returns the correct answer if your local repo is up to date. To prove this, disconnect from the internet and try it; the command will succeed. In contrast, git ls-remote will fail because it tries to contact the remote repo. – Andy Stewart Feb 10 '16 at 12:16
  • Found this to be very useful if you only want to grab a specific branch such as master ```git rev-parse REMOTE_NAME BRANCH_NAME``` – CTS_AE Aug 18 '16 at 20:40
  • 4
    uhhh, no, it's `git fetch origin; git rev-parse origin/master`, isn't the fetch extremely important? – Alexander Mills Dec 28 '17 at 22:01
77

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.

Community
  • 1
  • 1
Jakub Narębski
  • 289,171
  • 61
  • 216
  • 230
50

A colleague of mine answered this for me:

git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
Kara
  • 5,996
  • 16
  • 49
  • 56
AdvilUser
  • 2,822
  • 2
  • 23
  • 15
23

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>
dch4pm4n
  • 341
  • 2
  • 6
16

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

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

kitingChris
  • 589
  • 5
  • 15
  • 1
    This is the only option that worked (for me) for getting the commit from an upstream remote in a forked repo. – Jalakoo Nov 11 '16 at 16:57
15

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.

Neeme Praks
  • 8,488
  • 5
  • 44
  • 46
antonagestam
  • 4,131
  • 3
  • 31
  • 42
4

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

4

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
skensell
  • 1,403
  • 12
  • 20
  • 1
    I would recommend `grep "refs/heads/master$"` so if anyone might asked for other branches than master or having an master2 branch this may break ;) – kitingChris Sep 20 '17 at 15:50
2

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.

Greg Hewgill
  • 890,778
  • 177
  • 1,125
  • 1,260
0

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

anamar
  • 114
  • 1
  • 5
-1

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