简体   繁体   中英

Clone only if non-empty

Normally, git clone is happy to clone empty repositories, it just prints the warning

warning: You appear to have cloned an empty repository.

Is there any way to tell git to clone only non-empty repositories? Ideally, I would prefer git clone to simply fail (without creating a working copy) if the repository is empty.

My current solution is in three steps: (1) clone, (2) see if the clone is empty, (3) delete if it is empty. Does the job but hardly elegant (and it prints warnings that I will need to filter out). Is there a more straightforward solution?

There is not really a way to do that, but you could approximate it (somewhat poorly): set up a remote pointing to the repo you might clone, and then run git ls-remote on that remote. (You can do this with any git repository, including a temporary empty one.) If ls-remote finds no refs, cloning that repository would have created an empty repository.

There are two major defects with this method, and one minor:

  1. You need a place to add the test remote and do the ls-remote.
  2. This is as just much work as actually cloning the empty repository. You save time when compared to cloning a large non -empty repository, but you will do that (clone the non-empty repo) anyway.
  3. (Minor) The answer you get is, at least potentially, out of date by the time you get it. This is also true of cloning a repo and discovering that it's empty (maybe fetching will now find a non-empty repository), or even of cloning or fetching some other repository: by the time you get and inspect the result, which might be several whole seconds after you start the process, there could be dozens or even thousands of new commits added to the remote repo (this depends on how active the repository is).

I think you might as well just clone-and-maybe-remove, really. (Or just clone and don't remove: there's nothing wrong with an empty repo. I see from a comment that you don't want them; I just don't see the reason why you don't want them. "For automation" is a purpose, but not a reason.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM