Home > Software engineering >  git:branch shows HEAD instead of the current branch when deployed with AWS Lambda
git:branch shows HEAD instead of the current branch when deployed with AWS Lambda

Time:04-14

I'm using serverless-plugin-git-variables. In my serverless.yml, I have configured environment variables as the following: GIT_BRANCH: ${git:branch} This shows the correct branch when invoking my function locally but when deployed and in higher environments, the git_branch is displayed as HEAD. Why does it point to the HEAD in git and how can I get it to point to the correct branch.

CodePudding user response:

HEAD, written in all uppercase like this, is a very special name in Git. (It's so special, in fact, that if it's missing in a Git repository, Git refuses to believe that the repository is a repository.)

The name HEAD is normally "attached to" some branch name. The act of attaching HEAD to a branch name makes that branch the current branch by definition. Each branch name in some repository selects some particular commit, and when that branch is the current branch, the selected commit is the current commit.

This is the normal everyday mode in which you, yourself, will use Git: with an "attached HEAD", so that there is a current branch to select the current commit. But Git has an alternative mode called detached HEAD mode. This mode is not for normal everyday use: it's mainly for things like an in-progress git rebase, or for looking at historic commits (rather than most-recent ones). Unless you're in the middle of a rebase operation that has stopped for some reason—because you told it to, or because you need to resolve a merge conflict, for instance—you won't normally see this mode yourself.

Build servers, however, are often instructed to put Git into this detached-HEAD mode. The reasons for this are up to the author of the software that does this building, but one of them is the fact that branch names in Git are specific to each clone. That is, your clone of some Git repository holds your branch names. If I clone the same Git repository, I get my own branch names. Each name in my repository is entirely under my control and might not refer to the same commits as the names in your Git repository.

Each commit in a Git repository, however, has a unique hash ID. This hash ID, which is a hexadecimal representation of a very large number, is specific to this particular commit. If you make a new commit, it gets a new, unique, never-been-used-before, never-will-be-used-again hash ID. If you then send that commit somewhere—e.g., with git pushevery Git repository that acquires this commit will use the same number.

Using that raw number, a server can tell Git software to check out that particular commit. This puts Git into detached HEAD mode, using that particular commit as the current commit. So that makes sure that the server is using the commit you said to use, without requiring a branch name for it. The server's Git repository might not have the right names, but it definitely has the same numbers.

Hence the answer to the first part of your question:

Why does [git branch say HEAD]

is "because the server software is designed to work that way", and the answer to the second part:

and how can I get it to point to the correct branch

is probably "you can't". Branch names in Git are generally unimportant and temporary and your branch names are not someone else's branch names, so there's no good reason to use those, when raw hash IDs are exact. Computers need to be exact here.

  • Related