Home > database >  Is there a way to check if a remote branch exists for a repository without actually cloning it?
Is there a way to check if a remote branch exists for a repository without actually cloning it?

Time:12-18

I am triggering a downstream pipeline from an upstream pipeline in Gitlab CI.

I want to check if there is a branch with the same name as the upstream branch in the downstream repo, but I don't want to clone the whole downstream repo just to do a git branch --list or something.

Basically, I'm looking for a one liner that just checks if a branch exists in a remote repository.

Any advice would be appreciated. Thanks!

CodePudding user response:

Yes, there is. You can use git ls-remote. For example, if you want to see branches in Git's repository, you can do this:

$ git ls-remote https://github.com/git/git.git refs/heads/*
69a9c10c95e28df457e33b3c7400b16caf2e2962        refs/heads/main
e9d7761bb94f20acc98824275e317fa82436c25d        refs/heads/maint
69a9c10c95e28df457e33b3c7400b16caf2e2962        refs/heads/master
a2b2bfdf31eaee062391d5f278d6dd93cf7a3e4c        refs/heads/next
03adea34405149b89748ff7ea766e1c7f06430cd        refs/heads/seen
510d42bf652ebc22ceb54ba76b4d9023320194fa        refs/heads/todo

You can specify refs/heads/main if you want, and see if there's any output, for example. And this command works just fine outside of a repository. You can see the manual page for more options.

  • Related