91

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?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
AdvilUser
  • 3,142
  • 3
  • 25
  • 15

11 Answers11

126

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
  • 15,589
  • 9
  • 43
  • 57
  • 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
  • 15
    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
  • 5
    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
81

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
  • 309,089
  • 65
  • 217
  • 230
57

A colleague of mine answered this for me:

git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
Kara
  • 6,115
  • 16
  • 50
  • 57
AdvilUser
  • 3,142
  • 3
  • 25
  • 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
17

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

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

kitingChris
  • 658
  • 6
  • 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,956
  • 5
  • 47
  • 47
antonagestam
  • 4,532
  • 3
  • 32
  • 44
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,421
  • 12
  • 21
  • 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
  • 951,095
  • 183
  • 1,149
  • 1,285
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
  • 324
  • 4
  • 8
0

Use of the --short parameter to rev-parse removes the need for parsing the response via awk etc.

git rev-parse --short=7 HEAD

(from the repo root dir; examples above show how to use --git-dir and variants to reference other directories).

…produces just the sha to 7 digits (or however many you indicate):

5efca96

Dad
  • 6,388
  • 2
  • 28
  • 34