Correct global git config for personal access tokens in GitLab

Viewed 8196

The question: How do I set up my global user (I am assuming) .gitconfig to use the Personal Access Token in gitlab? Due to the other constraints of my coworkers and our repository, I believe this is the only way for it to work for everyone.

E.g., this is how to use the personal access token in a cloned repository's remote config, and works:

[remote "origin"]
     url = https://gitlab-ci-token:[PERSONAL ACCESS TOKEN]@gitlab.com/path/to/repostiory.git

But how do we set up our global git config so that the token is automatic? I am assuming there should be some key like

git config --global gitlab.[????somethingtoken????] [PERSONAL ACCESS TOKEN]

But for the life of me I can't find it anywhere or in any documentation. It seems like gitlab.accesstoken worked at one point in history, but they've changed their approach. neither gitlab.ci-token nor gitlab.gitlab-ci-token appears to work.

Caveats (that prevent obvious alternatives):

  1. This isn't just an inconvenience - we use git submodules in which the origin url needs to be included in the parent repository, and so we literally cannot use the tokenized url because it will be shared with others in .gitmodules
  2. We have users working from home on Windows without SSH, and so we are confined to the https repository protocol and can't switch to ssh

Thanks for your help/observations

1 Answers

You want to use a credential helper for this purpose. Each user should set up the credential helper by setting credential.helper to an appropriate value (wincred or manager on Windows, osxkeychain on macOS, and libsecret on Linux), and then enter their username and token when prompted. That will save their credentials, and they'll be used for subsequent accesses to the same server.

Git itself doesn't know anything about any keys starting with gitlab because Git is agnostic to your hosting platform. Similarly, it has no knowledge of GitHub, either. Even if it did, storing secrets in the Git configuration is insecure (and therefore so is placing them in the URL), and using a credential helper keeps them stored in the system's encrypted credential store.

Related