9

Possible Duplicate:
git bash : how to check if there’s a new commit available

I am writing a script where I would like to compare the latest local commit to the latest upstream commit in order to tell the user there are commits to be pulled.

The latest local commit SHA is displayed with git log | head -n 1 | awk '{print $2}'.

Is there an equivalent for printing the SHA1 of the latest upstream commit?

Community
  • 1
  • 1
  • `git rev-parse HEAD` == `git log | head -n 1 | awk '{print $2}'` – miku Jan 03 '13 at 08:20
  • for check new commits in remotes I use this [git-prompt](https://github.com/juanpabloaj/git-prompt), this [lines make the job](https://github.com/juanpabloaj/git-prompt/blob/master/git-prompt.sh#L522-550) – JuanPablo Jul 20 '14 at 04:52

1 Answers1

22

Local head:

$ git rev-parse HEAD

Remote head:

$ git ls-remote <url> <refs>

Displays references available in a remote repository along with the associated commit IDs.

Example:

$ cd ~/github/scrapy/scrapy
$ git rev-parse HEAD
9f003a73daec59a73c23a2214b1b8d15a4391a2f
$ git ls-remote git://github.com/scrapy/scrapy.git HEAD
9f003a73daec59a73c23a2214b1b8d15a4391a2f

You can use diff to compare the output of the two:

$ diff <(git ls-remote git://github.com/scrapy/scrapy.git HEAD) \
       <(git rev-parse HEAD) 
miku
  • 181,842
  • 47
  • 306
  • 310