4

I have a local git repo, and I have a remote git repo. What is a simple command to see what commit the remote repo is, and what commit the local repo is, so I can simple see if I'm up to date?

This is going to be automated in a program, so I don't want lots of complicated stuff that I would have to parse. Preferably it would be cool to have both local & remote output the same text, with only the commit changing between the two. Any ideas?

Yep
  • 653
  • 5
  • 20
  • This post here http://stackoverflow.com/questions/3258243/git-check-if-pull-needed seems be have what you need i think. – Roger Jun 24 '12 at 16:55

2 Answers2

6

A Programmatic Solution

If you want a programmatic solution, you can look at the commits stored in each head and compare them. For example:

remote=$(
    git ls-remote -h origin master |
    awk '{print $1}'
)
local=$(git rev-parse HEAD)

printf "Local : %s\nRemote: %s\n" $local $remote

if [[ $local == $remote ]]; then
    echo "Commits match."
else
    echo "Commits don't match."
fi

Sample Output

Local : 9e1b4dc286acb442f7f604be7916db660b9d70cd
Remote: 9e1b4dc286acb442f7f604be7916db660b9d70cd
Commits match.
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
1

Assuming that your remote repository is referred to by the remote origin, and you're interested in the branch master, you can do:

git fetch origin

And then compare the output of:

git rev-parse master

... and:

git rev-parse origin/master

If the object names that are output by those two commands are the same, then your master and the master in origin were the same at the point when the git fetch origin was run.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Any way to do this without actually downloading the repo? – Yep Jun 24 '12 at 17:45
  • Well, if you have a local repository (as you suggest in the question) then `git fetch` (with the common transports nowadays) is very efficient at only downloading the objects that you're missing. However, if you really don't want to do that, then you could use `git ls-remote origin master`. – Mark Longair Jun 24 '12 at 18:28