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.

  • How to recover the file permissions to what git “thinks” the file should be?
  • How can I push to a git-svn repo?
  • git log — how to see *merge dates* for changesets?
  • Restoring git merge conflict flags
  • Git - Link other repository to my repository
  • git pulled files give 500 error
  • git ls-remote ssh:// <branch>

  • How can I print my git tags in Capistrano as part of my deploy task?
  • No such keg: /usr/local/Cellar/git
  • Git has no idea files are the repository. I cannot remove them
  • Git command to remove local branches that have a deleted upstream?
  • Recursively add the entire folder to a repository
  • best way of setting up a phonegap multiplatform project repository
  • 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.


    $ git ls-remote --symref HEAD | \
        head -1 | \
        awk '{print $2}'

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

    > git ls-remote --symref 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.