31

I want to get the hash of last commit that has happened in a remote repo without cloning it. Is there a way to do this ? I found several methods but for all of them to work, I need to clone the repo first and then issue the commands to get the last commit hash.

Is there a way I can get the last commit hash from a remote git without cloning it ?

Note:

user3678812
  • 417
  • 2
  • 6
  • 9

3 Answers3

74
$ git ls-remote https://github.com/gturri/dokuJClient.git 
2fb540fc8c7e9116791638393370a2fa0f079737    HEAD
2fb540fc8c7e9116791638393370a2fa0f079737    refs/heads/master

This command can be run from any directory.

If you only want the last sha1, eg to use it in a script, you could then do:

git ls-remote https://github.com/gturri/dokuJClient.git HEAD | awk '{ print $1}'
gturri
  • 13,807
  • 9
  • 40
  • 57
  • Thank you so much. This is what I was looking for. But when I run the command, it says "Permission denied (publickey)". Why is that happening? Should I add ssh key to the repo before doing this ? I need to use this in a script which will be used by several people. – user3678812 Jul 15 '14 at 05:30
  • I edited my answer to use a public url in my examples. Now you should be able to run them. If you want to distribute your script, you should ensure each potential user has read permission on the url you use – gturri Jul 15 '14 at 05:40
  • 3
    To get only HEAD (or some branch) you can just do `git ls-remote https://github.com/gturri/dokuJClient.git HEAD | awk '{ print $1}'` No grep is needed – BugaBuga Sep 12 '18 at 10:25
  • Pretty nice @BugaBuga. I just edited my answer accordingly. – gturri Sep 12 '18 at 14:11
  • 4
    I would replace `awk '{ print $1}'` by `cut -f1`, as `awk` is heavier than `cut`. – Vladimir Bauer Aug 05 '19 at 04:57
  • Ans in case someone is looking for the short commit. Simply `| awk '{print substr($1, 0, 7)}'` – aitorhh Jul 21 '21 at 21:05
  • rather than ` | awk '{print substr($1, 0, 7)}' `, you can simply use ` | head -c7 ` – WiringHarness Mar 14 '23 at 19:23
-1

Just a note in addition to @gturri's answer, that you can also use the name of the remote (as opposed to the url).

E.g. if you push with something like: git push heroku master, then you could use

git ls-remote heroku

This example assumes the name of your remote is 'heroku'.

You can replace 'heroku' above with whatever your remote is called.

stevec
  • 41,291
  • 27
  • 223
  • 311
-3

One way would be the following:

  1. Initialize your local repo: git init

  2. Add your the remote to it: git remote add myRemote "https://myremoterepo"

  3. Fetch the repo and check the history for the last commit: git fetch remote

Alternatively, you could also go to the repo page on github (I assume from your tag) and check the commits tab. It will show you the latest commit and its sha.

phuclv
  • 37,963
  • 15
  • 156
  • 475
sTodorov
  • 5,435
  • 5
  • 35
  • 55
  • `git fetch remote` dooes not work, and if you change to `git fetch ` it will actually transfer all files to your local machine. – cbaldan Aug 21 '17 at 17:59
  • 1
    @cbaldan `git fetch` will update the git db locally, if you really want to transfer all files to your local you would need to use `git pull origin ` – Ciasto piekarz Jun 13 '18 at 07:31
  • 1
    this effectively is just the same as cloning because it still fetches the whole history unnecessarily – phuclv Aug 28 '22 at 10:45