在 Linux 下配置 GitLab Token,通常分为 使用 Personal Access Token(个人访问令牌) 或 使用 Deploy Token(部署令牌),最常见的是前者。下面按使用场景说明。
linux-gitread_repository(拉代码)write_repository(推代码)git clone https://<username>:<token>@gitlab.com/yourgroup/yourrepo.git
示例:
git clone https://zhangsan:glpat-xxxxxx@gitlab.com/team/project.git
git remote set-url origin https://<username>:<token>@gitlab.com/yourgroup/yourrepo.git
git config --global credential.helper store
然后执行一次:
git pull
输入:
之后 Token 会保存在:
~/.git-credentials
vim ~/.netrc
内容:
machine gitlab.com
login your_username
password glpat-xxxxxx
权限:
chmod 600 ~/.netrc
如果你用 SSH,就不需要 Token:
ssh-keygen -t ed25519 -C "your@email.com"
cat ~/.ssh/id_ed25519.pub
复制到 GitLab → SSH Keys
git clone git@gitlab.com:group/project.git
export GITLAB_TOKEN=glpat-xxxxxx
git clone https://oauth2:${GITLAB_TOKEN}@gitlab.com/group/project.git
✅ Token 失效
✅ 403 / 401
read_repository如果你告诉我:
我可以给你更精确的配置示例。