Reputation: 6713
Is it possible to check whether remote repository had any changes since last check without cloning whole repository?
In my scenario I do not need to keep the source files on my device, I just need to know whether remote git repository had any new changes.
I know that I can do that if I have checked out repository:
git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD
But that requires to keep the source files on the device.
Upvotes: 0
Views: 313
Reputation: 45659
Depending on how your git repository is hosted, you might check whether the hosting software has an API that would help. Using just git commands, the following should work.
You can get a listing of all of the refs on the remote, along with their current commit hash, by saying
git ls-remote <remote-url>
where <remote-url>
is the same URL you would use to clone.
If you store the results of this, and then run it again later, you can see if any refs have been added, removed, or received an update (because in the case of an update, the hash will change).
(Note that a changed hash indicates that the history of the ref has changed - which does not necessarily mean that the content is different. You could shallowly fetch just the "old" and "new" commits using the refs' hashes, and examine their tree hashes, if you want to know if the content has changed.)
If you only care about branches (i.e. don't care about new tags, etc.) you could say
git ls-remote --heads --refs <remote-url>
If you don't want to read through all that output, you can put together some sort of script. The documentation doesn't seem to guarantee an order in which the branches will be shown, but it appears to be lexical order in my tests. If you're worried about that you could add a sort command, because order will matter with what I'm about to suggest. Anyway, instead of storing the full list of output, you could has the output and compare the result hash from one run to the next, to see if anything has changed (but you won't be able to tell what):
git ls-remote --heads --refs <remote-url> |git hash-object --stdin
Upvotes: 2