Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: check if commit xyz in remote repo?

Tags:

git

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!

like image 515
Jonas Byström Avatar asked Apr 05 '11 08:04

Jonas Byström


People also ask

How do you check if a commit is present in git?

For example, git rev-parse "$VAR^{commit}" will make sure $VAR names an existing object that is a commit-ish (i.e. a commit, or an annotated tag that points at a commit). To make sure that $VAR names an existing object of any type, git rev-parse "$VAR^{object}" can be used. -q, --quiet Only meaningful in --verify mode.


2 Answers

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...

like image 87
Mark Longair Avatar answered Oct 26 '22 08:10

Mark Longair


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

like image 24
sehe Avatar answered Oct 26 '22 07:10

sehe