Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to set init.defaultBranch during FETCHSOURCE

How can I set git config --global init.defaultBranch main during the FETCHSOURCE step in google cloud build?

My mission is silencing this redundant message:

hint: Using 'master' as the name for the initial branch. This
default branch name hint: is subject to change. To configure the
initial branch name to use in all hint: of your new repositories,
which will suppress this warning, call: hint: hint:     git config
--global init.defaultBranch <name>

I've tried this in step 0 of cloudbuild.yaml but it runs after FETCHSOURCE

steps:
- name: 'gcr.io/cloud-builders/git'   
args: [ 'config', '--global', 'init.defaultBranch', 'master']
0 6 116
6 REPLIES 6

Hi @tonymet,

Welcome to Google Cloud Community.

You might want to check this repository from github or this document for your reference for your cloud build. 

For further assistance, please reach out to the Google Cloud Support team.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

no, it's not a solution.   There's no documentation on FETCHSOURCE and setting git config during the FETCHSOURCE phase.  

cloudbuild.yaml steps execute AFTER fetchsource. What i need help with is how to configure FETCHSOURCE

Hello @tonymet ??? I totally get what you're trying to do. That annoying hint: Using 'master' as the initial branch name. message — it shows up when you're performing FETCHSOURCE, and yep, git config --global init.defaultBranch main in your subsequent build steps doesn't cut it. Been there ????Why it's happeningFETCHSOURCE is run prior to your own actions of construction beginning. So even when you have set in step 0, it's too late already — Git has already run with defaults.What you can do instead???? Choice 1: Custom builder imageYou can create your own Docker image with that configuration already set. Something like:FROM gcr.io/cloud-builders/gitRUN git config --global init.defaultBranch mainThen, in your cloudbuild.yaml, substitute the default git one with this custom image.???? Option 2: Just ignore it (if it is not breaking thingsActually, it's a warning. Does not impact your build unless you're authoring something that depends upon the default branch name.

???? Option 3: Use repo which already has main set

If you're adding repos manually, just run this once locally:

git config --global init.defaultBranch main

Then your new repos will default to main, and warnings will end.

thanks that sounds like a step in the right direction.  where in cloudbuild.yaml to I set the build image for FETCHSOURCE? i only see build images for - steps

Hey! Yeah, totally get the confusion — FETCHSOURCE isn’t something you can customize in cloudbuild.yaml directly.

That part (where Cloud Build fetches your source code) happens before any of the steps, and Google handles it under the hood. So there’s no way to set a custom image just for that.

If you need full control over how the source is pulled (like with a specific Git image or custom logic), you can actually skip FETCHSOURCE entirely and add a manual git clone step using your own image.

Let me know if you want a quick example of how to do that — happy to share!