在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個月更新一次)。
- 權限最小化:僅授予必要的最低權限,避免過度授權。