Question: For example (not necessarily factual), Gentoo has an ebuild `git`, which has the default branch name `main` hardcoded in the source code.

Alice, as a user of Gentoo, would like to change it to `master` before building, so that the original default branch name may be restored.

While Gentoo may not accept such a patch, Alice might still be able to apply a user-space patch before Portage makes the ebuild.

Is there such a way to achieve it?

Answer: 
Sounds like you are looking for the `/etc/portage/patches` directory. <https://wiki.gentoo.org/wiki//etc/portage/patches>

Patches placed here (specifically at `/etc/portage/patches/${CATEGORY}/${P}` tec.) will be applied during build, without (usually) requiring any change to the ebuild. The Wiki page talks about EAPI 5 and 6, but most of the tree is now using EAPI 6 or newer, so you are unlikely to have issues. If you place the patch under the package name (no version), then portage will attempt to apply it to every version of the package which you build.

I've found it really useful for testing upstream fixes before submitting for inclusion, without having to set up a separate development / build environment for major pieces of software like KDE.
A bad example, as `git` is already using `master` as the default branch, and selecting a different name as the default will be [supported with 2.28.0](https://www.kunal-chowdhury.com/2020/07/git-default-branch.html).

However, to my knowledge there's no easy and straightforward way to achieve what you ask for. My approach would be to copy the current ebuild to a [local custom repository](https://wiki.gentoo.org/wiki/Handbook:AMD64/Portage/CustomTree#Defining_a_custom_ebuild_repository) (I use `/usr/local/portage` for this purpose), perhaps increase the revision number (you could start with `*-r100 + x` to be ahead of the Gentoo releases) and apply your patch there. It's up to you to keep this ebuild updated.