Bitbucket平台的HTTP Access Tokens操作手册
在Bitbucket平台添加HTTP Access Tokens(用于替代密码进行认证)。
1. 登录Bitbucket并访问个人设置
- 打开 Bitbucket 并登录账号。
- 点击右上角头像 → 选择 Manage account。
2. 生成Access Token
- 在左侧菜单中选择 Access tokens(位于 Security 分类下)。
- 点击 Create access token。
3. 配置Token权限
- Token name:输入描述性名称(如
MyApp Token
)。 - Permissions:根据需求选择权限,例如:
- Repository →
Read
(只读访问)或Write
(读写权限)。 - Pipeline、Build 等其他可选权限。
- Repository →
4. 生成并保存Token
- 点击 Create 生成Token。
- 立即复制Token值(生成后无法再次查看,若丢失需重新生成)。
5. 在Git中使用Token
方式一:临时使用(命令行)
git clone https://x-token-auth:<YOUR_TOKEN>@bitbucket.org/<USERNAME>/<REPO>.git
- 将
<YOUR_TOKEN>
替换为实际Token值。
方式二:持久保存(推荐)
使用Git凭证存储(避免每次输入Token):
# 存储凭证(会提示输入用户名和Token)
git config --global credential.helper store# 执行任意Git操作触发存储
git clone https://bitbucket.org/<USERNAME>/<REPO>.git
- 首次输入后,Git会将凭证保存在
~/.git-credentials
文件中。
6. 查看当前凭证存储配置
git config --global credential.helper
- 无输出:未配置全局凭证存储,每次操作需手动输入用户名和密码。
- 常见输出:
store # 使用明文文件存储(~/.git-credentials) cache # 内存缓存(默认15分钟) osxkeychain # macOS钥匙串(需安装Git凭证助手)
7. 修改凭证存储方式
方式一:使用系统原生密钥管理(推荐)
# macOS(使用钥匙串)
git config --global credential.helper osxkeychain# Windows(使用凭据管理器)
git config --global credential.helper wincred# Linux(需安装libsecret或gnome-keyring)
git config --global credential.helper /usr/share/git-core/git-credential-libsecret
8. 删除已保存的凭证
场景一:系统密钥管理(macOS/Windows)
- macOS:打开 钥匙串访问 → 搜索Bitbucket/GitHub相关条目 → 删除。
- Windows:打开 控制面板 → 凭据管理器 → 删除Git相关凭据。
场景二:明文文件存储
# 直接删除存储文件(需重新输入凭证)
rm ~/.git-credentials
9. 自定义凭证存储路径(高级)
若需将凭证存储在非默认位置:
git config --global credential.helper "store --file /path/to/credentials"
10. 验证配置是否生效
# 执行需认证的操作,验证是否不再提示输入密码
git pull
11. 多账户管理(不同域名使用不同凭证)
在.gitconfig
中添加条件配置:
[credential "https://bitbucket.org"]helper = store --file ~/.git-credentials-bitbucket
[credential "https://github.com"]helper = store --file ~/.git-credentials-github
12. 验证Token是否生效
git pull # 若无需再次输入密码,则配置成功
.git-credentials内容例子
https://username:password@domian(仓库域名)
Token管理
- 查看现有Tokens:返回 Access tokens 页面。
- 撤销Token:点击Token右侧的 Revoke 按钮。
- 修改权限:删除旧Token,重新创建新Token并分配不同权限。
注意事项
- 安全性:Token相当于密码,请勿共享或提交到代码库。
- 有效期:Token永久有效,需定期轮换(建议每6-12个月更新一次)。
- 权限最小化:仅授予必要的最低权限,避免过度授权。