1

I use the following command to know all the remote branches of a project:

git ls-remote https://github.com/nanashili/AuroraEditor --h --sort origin "refs/heads/*" | sed "s,.*${TAB}refs/heads/,,"

But I would like to know which is the main branch of the project, which can be different from main and master.

Can you give me a hand?

Paul
  • 3,644
  • 9
  • 47
  • 113
  • There's technically no main branch, it's just a convention. – Romain Valeri Sep 08 '22 at 13:47
  • 1
    Most probably you want the default branch; it's where `HEAD` points at on the remote repository; see https://stackoverflow.com/a/60188813/7976758 ; Found in https://stackoverflow.com/search?q=%5Bgit%5D+%22ls-remote%22+default+branch – phd Sep 08 '22 at 13:54
  • @RomainValeri: I know, but consider the following example. You have a project, there are several branches in this project, but none of them are called `main` and `master`. For example the following link: https://github.com/nanashili/AuroraEditor When you `git clone` the following project, the project is automatically checked out automatically on the one defined as main eg on` github`. – Paul Sep 08 '22 at 13:56
  • Yep, exactly: `git ls-remote -q --symref https://github.com/nanashili/AuroraEditor | head -1` -> "ref: refs/heads/development-main HEAD" – phd Sep 08 '22 at 14:00
  • @phd: I managed to do this: `git ls-remote -q --symref https://github.com/nanashili/AuroraEditor | head -1 | sed "s,.*${TAB}refs/heads/,,"` I get this: `development-main HEAD` I would like to get just the name without the space after and the word `HEAD` That is this: `development-main` do you know how I can do? – Paul Sep 08 '22 at 14:20
  • 1
    `git ls-remote -q --symref https://github.com/nanashili/AuroraEditor | awk 'NR==1 { print $2; }'` ; `NR==1` selects Record Number 1 (the 1st line); `$2` prints the second field of the 3 space/tab-separated fields. – phd Sep 08 '22 at 14:25
  • @phd: What do you think like this: `git ls-remote -q --symref https://github.com/nanashili/AuroraEditor | awk 'NR==1 { print $2; }' | sed "s,.*${TAB}refs/heads/,,"` – Paul Sep 08 '22 at 14:30
  • @TTT: Then give me an example repository so I can try? – Paul Sep 08 '22 at 20:02
  • I'm interested in having at least one. – Paul Sep 08 '22 at 20:32
  • @Paul actually I think it just points to one, so I deleted my comment. The rest are probably manual. – TTT Sep 08 '22 at 20:51

3 Answers3

2
git ls-remote -q --symref <remote> | head -1

prints where the HEAD points at in the remote repository. <remote> can be a name like origin or an URL. To cut the full ref name out of the result run

git ls-remote -q --symref <remote> | awk 'NR==1 { print $2; }'

NR==1 selects Record Number 1 (the first line); $2 prints the second field of the 3 space/tab-separated fields. If you want to cut the short branch name instead of the full:

git ls-remote -q --symref <remote> | awk 'NR==1 { print $2; }' | sed 's!^refs/heads/!!'

Example:

$ git ls-remote -q --symref https://github.com/nanashili/AuroraEditor | awk 'NR==1 { print $2; }' | sed 's!^refs/heads/!!'
development-main

Or avoiding awk completely:

git ls-remote -q --symref <remote> | head -1 | cut -f1 | sed 's!^ref: refs/heads/!!'
phd
  • 82,685
  • 13
  • 120
  • 165
  • Is there a way without using print ? – Paul Sep 08 '22 at 14:49
  • @Paul Why? You do want to get the output, don't you? – phd Sep 08 '22 at 14:51
  • The problem I have to use it on Swift, I have a package that checks the syntax. It recognizes print as swift's print, albeit a string, but it gives me trouble. https://i.stack.imgur.com/nXbDI.png – Paul Sep 08 '22 at 14:55
  • @Paul Brouhaha, broken syntax checker, take my sympathy! Replace `awk` with `cut` and `sed`. See the updated answer. – phd Sep 08 '22 at 15:00
  • Ok, it seems to work, I was trying a similar thing: `git ls-remote -q --symref https://github.com/nanashili/AuroraEditor | head -1 | sed "s,.*${TAB}refs/heads/,," | sed "s,HEAD,,"`, but yours works. – Paul Sep 08 '22 at 15:22
  • One thing being that I would like to keep the same syntax, in your opinion this is correct, to know all the remote branches? `git ls-remote https://github.com/nanashili/AuroraEditor --h --sort origin "refs/heads/*" | cut -f2 | sed 's!^refs/heads/!!'` – Paul Sep 08 '22 at 16:15
  • @Paul `git ls-remote --heads` See [`git help ls-remote`](https://git-scm.com/docs/git-ls-remote) – phd Sep 08 '22 at 16:41
  • 1
    FWIW : `git ls-remote -q --symref origin HEAD | grep -Po "(?<=ref: refs/heads/)[^\t]*"` – LeGEC Sep 09 '22 at 05:43
  • The additions are : 1. specify we only want `HEAD` from remote, 2. use only "grep" instead of "head -n" (head -1 adds a "remote end hung up unexpectedly" warning in my terminal), 3. write a one stop grep command, with `-o` to isolate the short name of the branch (I got the `-P` and `(?<=...)` shenanigans from [this other answer](https://stackoverflow.com/a/15136585/86072)) – LeGEC Sep 09 '22 at 05:44
0

Plain Git doesn't have a term as main branch. In GitHub, for example, you can choose main, but it will be used on GitHub servers only, your git repository will not change. So to solve the problem, you can make main branch configurable in your script using config file or arguments.

Vadim Beskrovnov
  • 941
  • 6
  • 18
  • I know, but consider the following example. You have a project, there are several branches in this project, but none of them are called `main` and `master`. For example the following link: https://github.com/nanashili/AuroraEditor When you `git clone` the following project, the project is automatically checked out automatically on the one defined as main eg on` github`. – Paul Sep 08 '22 at 13:56
  • Then you can use HEAD pointer: https://stackoverflow.com/questions/18726037/what-determines-default-branch-after-git-clone – Vadim Beskrovnov Sep 08 '22 at 14:17
0

Understanding that the main branch can be any branch in a repo, the only thing that can get you close to know what branch is the main branch would be (I think) by checking HEAD from the remote, then check the branches in the remote and see which branch is on that revision.... the problem is that there might be more than one branch pointing at that revision.

eftshift0
  • 26,375
  • 3
  • 36
  • 60