git: check if commit xyz in remote repo?

Git

Git Problem Overview


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, but I'm hoping for a nicer+faster way. git ls-remote seemed promising, but found nothing of value to me there. Thanks!

Git Solutions


Solution 1 - Git

Let's suppose that the remote that refers to the remote repository is called origin. In that case, first update all your remote-tracking branches with:

git fetch origin

Now you can use the useful --contains option to git branch to find out which of the remote branches contains that commit:

git branch -r --contains xyz

(The -r means to only show remote-tracking branches.) If the commit xyz is contained in one or more of your remote-tracking branches, you'll see output like:

  origin/test-suite
  origin/HEAD -> origin/master
  origin/master

If it's contained in your local repository, but not one of the remote-tracking branches, the output will be empty. However, if that commit isn't known in your repository at all, you'll get the error malformed object name and a usage message - perhaps a bit confusing if you're not expecting it...

Solution 2 - Git

Like Mark said,

 git branch -a --contains commitish

However, beware for branches that contain a cherry-picked/rebased/merged version of the commit.

This could come in handy

 git log --cherry-pick --left-right <commitish> ^remote/branchname

It will list the commit ONLY if it doesn't exist (as a cherrypick) in the remote branch. See the man page for log for an explanation on how --cherry-pick identifies equivalent commits

Of course merges/rebases with conflict resolutions or squashes cannot be automatically detected like this

Solution 3 - Git

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 <commit ID>

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonas Bystr&#246;mView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitseheView Answer on Stackoverflow
Solution 3 - GitTrentPView Answer on Stackoverflow