Get the branch name pointed at by HEAD in a remote repository without cloning the remote

I am currently looking for a workaround to retrieve branch-name pointed by HEAD of remote repository. I don’t want to clone the repository.

I directly need some git command to retrieve HEAD branch name. I tried
below command but it only gives SHA commit hash HEAD, but I want branch name instead.

  • Reusing a merged development branch / Remerging into an unchanged stable branch with git
  • git — How to schedule `git push` on linux?
  • rsync .git directory
  • Git for Windows - The Program can't start because libiconv2.dll is missing
  • Git checkout/pull doesn't remove directories?
  • How to save the configuration after changing lines from a git config --edit
  • git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
    

  • Can you get the number of lines of code from a Github repository?
  • git-clone and post-checkout hook
  • git: can a file be both staged and unstaged for commit?
  • What happens to a git branch after delete if it was not merged?
  • Transform a combined diff into a patch
  • Why doesn't git merge the conflict-free changes automatically into locally modified files?
  • 2 Solutions collect form web for “Get the branch name pointed at by HEAD in a remote repository without cloning the remote”

    Use git ls-remote --symref

    You don’t need to clone the remote repo to (programmatically) identify the current branch in that remote repo. ls-remote has a handy option called --symref:

    In addition to the object pointed by it, show the underlying ref pointed by it when showing a symbolic ref. Currently, upload-pack only shows the symref HEAD, so it will be the only one shown by ls-remote.

    With this knowledge, you could simply run

    git ls-remote --symref <remote_repo_url> HEAD
    

    and extract the name of the current branch using some sed-foo; because ls-remote is categorised as a “plumbing” Git command, you can safely use it in scripts.

    Example

    $ git ls-remote --symref https://github.com/Jubobs/gitdags HEAD | \
        head -1 | \
        awk '{print $2}'
    refs/heads/master
    

    Use the --symref option. The output shows that HEAD points at master.

    > git ls-remote --symref https://github.com/git/git-scm.com.git HEAD
    ref: refs/heads/master  HEAD
    df55dcad82c2aa8dd7760aadf92b132af1deb8ec        HEAD
    

    The concise syntax is git ls-remote --symref <repository> <refs>.

    Git Baby is a git and github fan, let's start git clone.