Metadata: Reading GIT Branch Name - Not working because Head is detached

Hello. I need to read the name of the Git branch Netlify is pulling when it builds my code. Yet because Netlify pulls a commit and not a branch, when I read process.env.BRANCH, it comes up HEAD. This happens because the Head is detached. I tried to read the branch name directly, with a GIT command, yet, of course, result. Build environment variables | Netlify Docs

Please help

Thanks for getting in touch! I think you’ll need to resolve the detached HEAD for the BRANCH env var to log a specific branch name.

Since the HEAD is detached, you’re not on a specific branch, and so there is no branch name for that variable to read from.

You would ideally want to create a new branch (if you want to keep your current changes) where you can then access the branch name using that variable or merge it into a previously existing branch, which would also allow you to access that environment variable value. Hope this helps!

1 Like

Ok. Thanks. Yet, would what I experienced make the documentation here inaccurate: Build environment variables | Netlify Docs

Hi there,

Thanks for following up! If you can provide any additional clarity on what necessary changes need to be made, we can definitely take a look!

The documentation does look to be correct as-is; since the HEAD was detached and you’re not on an actual branch, there is no way for that variable to return anything other than HEAD. Let me know!

Ok. Thank you. That was great help.