Git遠程推送時記住用戶名和密碼


當使用HTTPS協議推送代碼到Git倉庫時,發現每次都需要輸入密碼,操作起來非常麻煩。下面介紹幾種免去輸入密碼的方法。

HTTPS協議推送

使用HTTPS協議,有一種簡單粗暴的方式是在遠程地址中帶上密碼。

> git remote set-url origin http://yourname:password@bitbucket.org/yourname/project.git 

還有一種方法,是創建文件存儲Git用戶名和密碼。

以Windows環境為例,在%USERPROFILE%目錄中(一般為C:\Users\yourname),打開Git Bash命令行,創建文件

> touch .git-credentials 

在文件中輸入倉庫域名,這里使用了bitbucket.org

https://yourname:password@bitbucket.org 

在CMD終端中設置在全局Git環境中,長期存儲密碼

> git config --global credential.helper store 

其他設置密碼方式
記住密碼(默認15分鍾):git config --global credential.helper cache
自定義存儲時間:git config credential.helper 'cache --timeout=3600'

SSH協議推送

如果原來的推送地址協議是HTTPS,可以通過換成SSH協議,在遠程倉庫添加SSH Key來實現推送時免賬戶密碼輸入。

> git remote -v             // 查看遠程地址 > git remote rm origin // 刪除原有的推送地址 > git remote add origin git@github.com:<用戶名>/版本庫名 

或者

> git remote -v
> git remote set-url origin git@github.com:<用戶名>/版本庫名 

執行推送。

> git push -u origin master

發現提示權限不夠。

The authenticity of host 'bitbucket.org (104.192.143.1)' can't be established. RSA key fingerprint is SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'bitbucket.org,104.192.143.1' (RSA) to the list of kn own hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 

需要在本地創建該帳號的RSA Key。可以參考以下兩篇文章:
Windows下配置SSH連接Github
Git如何在本地生成多個SSH key

然后再執行推送。

> git push -u origin master


作者:RebeccaYan
鏈接:https://www.jianshu.com/p/7182b2faab84
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM