gitgitlab

Gitlab Personal Access Token - where to keep the token for seamless clone / pull / push


Can anyone tell where to put the Gitlab PAT - Personal Access Token?

Downloading git::https://gitlab.com/mycompany/myproject.git?ref=v0.0.1 - unable to download - I can't change the URL as its fixed in project repo.

I tried to set it in my git config --global gitlab.accesstoken=abcdef1233TVHEPkNxyz

also as environment variable TOKEN=abcdef1233TVHEPkNxyz PRIVATE_TOKEN=abcdef1233TVHEPkNxyz

Any advice please?


Solution

  • Basically we need to:

    1. Generate personal access tokens (with permissions according to your requirement).
    2. git clone https://oauth2:abcdef1233TVHEPkNxyz@gitlab.com/mycompany/myproject.git?ref=v0.0.1

    However, if we can not change the URL from git::https://gitlab.com/mycompany/myproject.git?ref=v0.0.1 to the above format or we want to avoid entering the token each time we clone a repo or use GitLab APIs, we need to do the following:

    1. Setup a credential store. Fore example, on Ubuntu:

      sudo apt-get install libsecret-1-0 libsecret-1-dev
      sudo make --directory=/usr/share/doc/git/contrib/credential/libsecret
      git config --global credential.helper '/usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret'
      
    2. Clone a HTTPS URL:

      git clone 'https://gitlab.com/mycompany/myproject.git?ref=v0.0.1'
      

    when prompted for password use PAT instead. The PAT will be stored in the libsecret store for default 15 minutes. Use git config --global credential.helper 'cache --timeout=1800' to change the timeout duration.

    (Reference)