git: check if commit xyz in remote repo?

后端 未结 3 1769
旧巷少年郎
旧巷少年郎 2020-12-05 03:50

I have a commit xyz in my local branch that I want to check if it is included in a remote release repository; can I do that in some easy way? I could clone the remote repo,

3条回答
  •  余生分开走
    2020-12-05 04:18

    The existing answers require the entire remote repository to be downloaded locally. If the remote has many commits that are not yet cloned locally, this could take a very long time. An example is something like the linux-stable repository, which has many independent branches that are never merged. Someone tracking a stable kernel might clone only the single branch for that kernel. Needing to fetch all the branches, for every stable series kernel, to see if the commit exists would require downloading much more data.

    There does not appear to be a good way to do this without fetching the entire remote repo. The ability is there, based on the way git fetch-pack and git send-pack work, but there doesn't seem to be a way to use it in the way desired.

    Pushing a branch to a remote repository does not upload commits the remote already has, and this is done without downloading the entire remote repository first. Trying to fetch a remote commit doesn't need to download the entire remote repository to determine if the requested commit exists or not.

    The latter can be used to achieve what was asked for in some cases.

    git fetch origin 
    

    If the remote does not have that commit, then this fails. The remote repository does not need to be cloned locally to do that. If it the remote does have the commit, then it fetches it. There's no option just see if the fetch would work but not fetch anything. Of course, if the commit is already available locally, then nothing needs to be fetched, and this is not a costly operation.

    Some remote repositories will not allow something that is not the head of a branch or a tag to be requested. In that case this won't work.

提交回复
热议问题