4

Suppose I wish to clone a large git repo from a URL, if and only if a commit was made to master within the last n (say 24) hours. I don't want to clone it, because that's rather time-consuming for such a large repo, nor do I wish to clone it once and then check e.g. git status periodically.

I want to be able to tell when the last commit was made prior to cloning. Is there a way of doing this in git? If not, what are my options? I know I can get the hash of the last commit via git ls-remote

innisfree
  • 2,044
  • 1
  • 14
  • 24

1 Answers1

6

One option could be to go ahead and clone the repo, but only with a depth of 1. This should make for a very fast download.

Then, you could check the tip of master's timestamp.

Example:

git clone --depth 1 https://github.com/jquery/jquery.git jquery

See this article for more information.

Git supports the notion of a “shallow clone”, which is a more succinctly meaningful way of describing a local repository with history truncated to a particular depth during the clone operation. By providing an argument of --depth 1 to the clone command, the process will copy only the latest revision of everything in the repository.

--

Another option, if your target repo is on GitHub, would be to use GitHub's REST API like this:

GET /repos/:owner/:repo/commits/:sha

You can then parse the response for the commit's timestamp.

More information: https://developer.github.com/v3/repos/commits/#get-a-single-commit

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115