AdvilUser
AdvilUser

Reputation: 3442

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?

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?

Upvotes: 98

Views: 125459

Answers (11)

Dad
Dad

Reputation: 6468

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

Upvotes: 1

AdvilUser
AdvilUser

Reputation: 3442

A colleague of mine answered this for me:

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

Upvotes: 59

skensell
skensell

Reputation: 1451

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

Upvotes: 4

anamar
anamar

Reputation: 380

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

Upvotes: 0

kitingChris
kitingChris

Reputation: 678

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

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

Upvotes: 18

Jakub Narębski
Jakub Narębski

Reputation: 323344

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.

Upvotes: 84

gprasant
gprasant

Reputation: 16023

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 

Upvotes: 129

antonagestam
antonagestam

Reputation: 4789

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.

Upvotes: 15

dch4pm4n
dch4pm4n

Reputation: 341

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>

Upvotes: 23

Reputation:

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

Upvotes: 4

Greg Hewgill
Greg Hewgill

Reputation: 992707

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.

Upvotes: 2

Related Questions