git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] revisions: add @{default} shorthand for default branch

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 29, 2026, 20:23 UTC
Message-ID
<xmqq1pj8b22h.fsf@gitster.g>
In-Reply-To
<pull.2183.git.git.1769700352081.gitgitgadget@gmail.com>
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 22 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Git already has shorthands like @{upstream} and @{push} to refer to
> tracking branches, but there is no convenient way to refer to the
> default branch of a repository (typically "main" or "master").
>
> Users often want to switch to the default branch regardless of its
> name, especially when working across repositories with different
> default branch names. Currently they must either hardcode the branch
> name or query it via configuration, which is cumbersome.
>
> Add a new @{default} shorthand that resolves to the default branch
> as determined by init.defaultBranch (or falls back to "main" or
> "master" depending on Git version). This allows users to write:
>
>   git checkout @{default}
>
> instead of having to know or look up the default branch name.
>
> The implementation follows the same pattern as @{upstream} and @{push},
> using a new branch_get_default() function that queries the default
> branch name and verifies it exists in the repository.

But @{upstream} and @{push} are inherently very different from what you are adding, aren't they? Asking for topic1@{upstream} and topic2@{upstream} makes quite a lot of sense, because the meaning of @{upstream} depends on "which branch's upstream are you talking about???". But I suspect that asking for topic1@{default} and expect it would be different from topic2@{default} is nonsense, as "the default" is not per branch but is an attribute of a repository. In other words, <branch>@{default} may by itself be a nonsense query. Are you rejecting a non-empty <branch> that may appear before @{default} as an error?

After cloning an upstream project, those who dislike the local branch name 'master' often rename it to something else, like

    $ git branch -m master main

and be happy, without configuring "init.defaultbranch". After all, that configuration variable affects newly created repositories, so after renaming 'master' to 'main', it is too late anyway. In such a repository, if you say @{default}, what should happen? As 'master' branch no longer exist, even though it is the @{default}, should it error out? Does your implementation error out?

Also I do not quite see how this would be useful in practice. Given that the names of local branches are under control of the local end user and not upstream projects, I would imagine that the primary branch used by a user is of per-user nature, not per repository. In other words, instead of having to do "git branch -m" after cloning, you may do "git config --global init.defaultBranch" just once and keep using the same default name. Under that condition, "can I ask what default branch name this repository uses, so that I can work on that branch" is rarely needed, if you are writing a script to use in many of your repositories, isn't it?

So, I am not sure. I wouldn't mind too terribly if <name>@{default} is rejected, but I do not imagine many people using it.

Previous: Harald Nordgren via GitGitGadgetNext: Harald Nordgren
Message 2 of 32 in “revisions: add @{default} shorthand for default branch”
  1. revisions: add @{default} shorthand for default branchHarald Nordgren via GitGitGadget, Jan 29, 2026
  2. Junio C HamanoJan 29, 2026
  3. Harald NordgrenJan 30, 2026
  4. Harald NordgrenJan 30, 2026
  5. Junio C HamanoJan 30, 2026
  6. Harald NordgrenJan 30, 2026
  7. Junio C HamanoJan 30, 2026
  8. Harald NordgrenJan 31, 2026
  9. Junio C HamanoJan 31, 2026
  10. Harald NordgrenJan 31, 2026
  11. Harald NordgrenJan 31, 2026
  12. Junio C HamanoFeb 2, 2026
  13. Harald NordgrenFeb 2, 2026
  14. Phillip WoodFeb 2, 2026
  15. Harald NordgrenFeb 2, 2026
  16. D. Ben KnobleFeb 2, 2026
  17. Harald NordgrenFeb 2, 2026
  18. Kristoffer HaugsbakkFeb 2, 2026
  19. Ben KnobleFeb 2, 2026
  20. Harald NordgrenFeb 2, 2026
  21. Junio C HamanoFeb 2, 2026
  22. Ben KnobleFeb 2, 2026
  23. Harald NordgrenFeb 2, 2026
  24. Junio C HamanoFeb 2, 2026
  25. Harald NordgrenFeb 2, 2026
  26. Harald NordgrenFeb 3, 2026
  27. Phillip WoodFeb 3, 2026
  28. Junio C HamanoFeb 2, 2026
  29. revisions: add @{default} shorthand for default branchHarald Nordgren via GitGitGadget, Jan 30, 2026
  30. Kristoffer HaugsbakkJan 30, 2026
  31. revisions: add @{primary} shorthand for primary branchHarald Nordgren via GitGitGadget, Jan 30, 2026
  32. revisions: add @{primary} shorthand for primary branchHarald Nordgren via GitGitGadget, Jan 31, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.