1

For scripting purposes I need to know the name of the "master" branch. I have some projects with the deprecated, old naming "master. I have other projects where the branch is named "main".

I would like to determine that name programatically. How do I find that name with a command?

One example I use a lot is to delete local branches that arez already merged to master (git branch --merged master | grep -v master |xargs git branch -d). In that case I need to provide either "main" or "master" as a value.

Akshat m
  • 92
  • 1
  • 8
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
  • 1
    Does this answer your question? [git - how to get default branch?](https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch) – IMSoP Jun 13 '21 at 11:20

2 Answers2

3

Git has no concept of the main branch. Therefore what you want to do is not really possible.

Git users have a concept of the main branch and by general agreement among all git users (and general advice originally given by Linus Torvalds) this branch is normally called master but it does not have to be.

The reason the main branch can be given a name other than master is because git has no concept of the main branch.

Software created by other people such as Github or Bitbucket or Gitlab do have the concept of the main branch. If this is what you are asking you will need to use the APIs exposed by those software to find out which branch is the main branch. Of course you will need to know what service the developer is using in order to use its API.

For example for Github you can do this to get the main branch:

curl -H "Accept: application/vnd.github.v3+json" \ 
    https://api.github.com/repos/slebetman/httpstress |
    grep default_branch

However, using git alone it is not possible to do what you want because the concept of a main branch is does not exist in git - it is only a concept in the human brains of programmers.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • The `git ls-remote` command can also be used to figure out which branch name is stored in the server's `HEAD`, provided both client and server are new enough. Of course, as you note, just because some name is the server's `HEAD` does not mean it is a, or the, "main branch". To get around the People Problem, probably the most workable method is to see if the remote repository has `master`, `main`, both, or neither; if it has exactly one, that's the one a human would use; otherwise an automated process is insufficient, and some human should inspect it. – torek Jun 13 '21 at 06:04
1

Try this command in a git repo

if git branch --all | grep -q master; then echo "'master' @ `pwd`"; else :; fi

If you do that, It prints out the directory path.

'master' @ /home/my_user/Documents/a-git-repo-with-master-name

You can use this do whatever you want. If it's main it doesn't print anything. If you need more functionality I can help you. Just let me know.

EDIT: For your specific case, if git branch --all | grep -q master; then git branch --merged master | grep -v master |xargs -I@ git branch -d @; else git branch --merged main | grep -v main |xargs -I@ git branch -d @; fi

link
  • 62
  • 1
  • 9
  • I would certainly recommend creating a bash script to maintain this logic. That would be easier to read and understand. – link Jun 12 '21 at 23:12
  • Out of curiosity, If your xargs definition correct? I get `fatal: branch name required` error. – link Jun 12 '21 at 23:25
  • the xargs command will throw that specific error if given no arguments. (happens in the case merged branches no longer exist). So for the script to run successfully in all conditions, there has to be another check before the xargs command. (which again means xargs is a bad choice for the command) – Jesper Rønn-Jensen Jun 13 '21 at 11:12
  • This command does not product any error, It simply ignores if not present `git branch --merged master | grep -v master |xargs -I@ git branch -d @`. See if that works. I can also edit the post to include it. – link Jun 13 '21 at 16:25