每次都需要輸入用戶名和密碼是因?yàn)槟悴捎玫氖?https 方式提交代碼, 如果采用的是 ssh 方式只需要在版本庫中添加用戶的 sha 的key就可以實(shí)現(xiàn)提交時(shí)無需輸入用戶名和密碼。
如果你的版本庫已經(jīng)用https 方式創(chuàng)建好了,那么就需要先刪除原來的提交方式。在終端執(zhí)行以下指令:
git remote rm origin git remote add origin git@github.com:(用戶名)/版本庫名
這里我提供一下我的具體例子:
https: https://github.com/用戶名/GitTest.Git
ssh: git@github.com:用戶名/GitTest.git
我是怎么知道的呢?如果你在創(chuàng)建版本庫時(shí)選擇不創(chuàng)建README.md,系統(tǒng)會(huì)提示你創(chuàng)建:
https:…or create a new repository on the command lineecho # GitTest >> README.mdgit initgit add README.mdgit commit -m "first commit"git remote add origin https://github.com/Sugerming/GitTest.gitgit push -u origin master…or push an existing repository from the command linegit remote add origin https://github.com/Sugerming/GitTest.gitgit push -u origin masterssh:…or create a new repository on the command lineecho # GitTest >> README.mdgit initgit add README.mdgit commit -m "first commit"git remote add origin git@github.com:Sugerming/GitTest.gitgit push -u origin master…or push an existing repository from the command linegit remote add origin git@github.com:Sugerming/GitTest.gitgit push -u origin master
然后這個(gè)時(shí)候你使用下面指令提交代碼:
git push -u origin master
系統(tǒng)會(huì)提示:
The authenticity of host 'github.com (192.30.252.131)' can't be established.RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known h osts.Permission denied (publickey).fatal: Could not read from remote repository.Please make sure you have the correct access rights
說明你權(quán)限不夠。所以這時(shí)你需要在本地創(chuàng)建自己的RSA的key。如下:
ssh-keygen -t rsa -C "用戶名"
然后系統(tǒng)會(huì)問你保存路徑等東西,我直接enter跳過了。
Generating public/private rsa key pair.Enter file in which to save the key (/c/Users/AlexYi/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:
然后系統(tǒng)會(huì)生成一些東西:
Your identification has been saved in /c/Users/AlexYi/.ssh/id_rsa.Your public key has been saved in /c/Users/AlexYi/.ssh/id_rsa.pub.The key fingerprint is:SHA256:rxfK05d7oZWpDvQ5dRQM0Na7...The key's randomart image is:+---[RSA 2048]----+| .o.+. || o o.|| . o|| o |...
最主要的是告訴你,你的可以在:
Your public key has been saved in /c/Users/AlexYi/.ssh/id_rsa.pub
找到這個(gè)文件,然后用記事本打開,就可以看到自己的key:
ssh-rsa AAAAB3NzaC1yc2EAAAADA...
然后將生成的rsa 的key添加到版本庫中即可,方法:
打開自己的版本庫,點(diǎn)擊右邊的 Settings 進(jìn)入配置頁。
然后點(diǎn)擊左邊導(dǎo)航欄的: Deploy keys 進(jìn)入添加key頁面
然后點(diǎn)擊: Add deploy keys ,將自己的內(nèi)容輸入進(jìn)去就可以了。
這樣就完成了。
最后繼續(xù)提交更改的代碼,使用:
git push -u origin master
可以提交成功。
如果要使用 git push
簡(jiǎn)短提交代碼:
git push
需要配置 :
git config --global push.default simple
或者:
git config --global push.default matching
區(qū)別在于,前者只提交你當(dāng)前所在的分支,而后者會(huì)提交本地所有的分支。具體的自己去查一下就明白了
聯(lián)系客服