報錯內容
fatal: unable to access 'https://git.dev.tencent.com/chendongnan/sfedu_wx.git/': The requested URL returned error: 403
問題起源
https方式每次都要輸入密碼,按照如下設置即可輸入一次就不用再手輸入密碼的困擾而且又享受https帶來的極速。
設置記住密碼(默認15分鐘):git config --global credential.helper cache
如果想自己設置時間,可以這樣做:git config credential.helper 'cache --timeout=3600'
這樣就設置一個小時之後失效
長期存儲密碼 (不推薦):git config --global credential.helper store
不推薦這種,因爲這裏保存的賬號和密碼保存到了~/.git-credentials
文件中,它會自動應用到每一個git clone/pull指令,如果在服務器裏面有多個項目,那麼克隆的不是該賬號下的項目,就只會得到403錯誤。
報錯原因
原因是git config --global credential.helper store
這個命令可以將用戶名和密碼長期全局地長期地存儲在客戶端(實際是客戶端所在電腦,並非git的任何目錄下,也就是說,即使重裝git,該用戶名和密碼也存在)。因爲,這裏保存的賬號和密碼會自動應用到每一個git clone
或者git pull
指令,如果想要克隆的不是該賬號下的項目,就只會得到403錯誤。
解決辦法
方法一
在最開始增加遠程地址的時候帶上用戶名和密碼:https://yourname:[email protected]/name/project.git
比如我的是這樣:git remote add origin https://chendongnan:[email protected]/chendongnan/sfedu_wx.git
方法二
如果已經使用命令git remote add origin https://git.dev.tencent.com/chendongnan/sfedu_wx.git
添加了遠程倉庫地址。
在~
目錄下的.git-credentials
文件中添加如下內容:https://chendongnan:[email protected]
步驟:
- 進入根目錄:
cd ~
- 顯示根目錄下隱藏文件看有沒有
.git-credentials
文件:ls -a
- 有則添加內容,無則先創建這個文件,進入編輯命令:
vim .git-credentials
,添加如下內容保存退出:https://chendongnan:[email protected]
方法三
運行命令:rm ~/.git-credentials
,刪掉git config --global credential.helper store
保存的賬號和密碼。回到每次輸入用戶名和密碼。