生成ssh keys
1.打開終端,輸入:ssh-keygen -t rsa -C shw***@126.com,生成:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wgj/.ssh/id_rsa): 此處可以自定義路徑,不定義而點(diǎn)擊回車就是默認(rèn)的.ssh路徑。下面自定義就輸入:/Users/wgj/.wgjGithubSSh/wgjgithub_rsa,其中.wgjGithubSSh是建好的文件,wgjgithub_rsa,則是沒(méi)有創(chuàng)建猪杭,生成后是文件的名字。
2.繼續(xù)操作妥衣,輸入兩次密碼皂吮,就在路徑中有相應(yīng)的東西了。
3.在github添加ssh key的時(shí)候,有兩個(gè)地方需要操作税手,1.key的名字蜂筹,這個(gè)可以以郵箱+mac名字,命名芦倒,便于以后的區(qū)分艺挪。2. 需要拷貝key,這個(gè)地方注意,不是拷貝文件兵扬,而是還有在終端中操作:
進(jìn)入攜帶‘wgjgithub_rsa.pub’的文件夾(cd /Users/wgj/.wgjGithubSSh)麻裳,然后:cat wgjgithub_rsa.pub口蝠,打開顯示出來(lái),發(fā)現(xiàn)是一大串津坑,這才是需要拷貝的東西(公鑰)(大串前面的ssh-rsa不要漏掉)妙蔗。
打開終端,先測(cè)試一下你的帳號(hào)跟github連上沒(méi)有:ssh -T git@github.com 如果出現(xiàn)如下提示疆瑰,表示你連已經(jīng)連上了.(因?yàn)橛辛说谝徊矫鸨兀圆挥米约鹤鲞^(guò)多的連接github的操作了,另外乃摹,下一次要連接github的時(shí)候記得打開第一步的工具:sourcetree).
Hi wgj0909! You've successfully authenticated, but GitHub does not provide shell access.
如果輸入$ ssh -T git@github.com
出現(xiàn)錯(cuò)誤提示:Permission denied (publickey).因?yàn)樾律傻膋ey不能加入ssh就會(huì)導(dǎo)致連接不上github。
解決辦法如下:
1.先輸入$ ssh-agent跟衅,再輸入$ ssh-add ~/.ssh/id_key孵睬,這樣就可以了。--( ssh-add ~/.ssh/id_key:把專用密鑰添加到 ssh-agent 的高速緩存中)
2.如果還是不行的話伶跷,輸入ssh-add ~/.ssh/id_key 命令后出現(xiàn)報(bào)錯(cuò)Could not open a connection to your authentication agent.
解決方法是key用Git Gui的ssh工具生成掰读,這樣生成的時(shí)候key就直接保存在ssh中了,不需要再ssh-add命令加入了叭莫,其它的user蹈集,token等配置都用命令行來(lái)做。
3.最好檢查一下在你復(fù)制id_rsa.pub文件的內(nèi)容時(shí)有沒(méi)有產(chǎn)生多余的空格或空行雇初,有些編輯器會(huì)幫你添加這些的拢肆。
Mac OS SSH密鑰在重啟后丟失的問(wèn)題
1. 從macOS Sierra 10.12.2以后,SSH的配置選項(xiàng)中多了“UseKeychain”選項(xiàng)]
2. 要解決這個(gè)問(wèn)題靖诗,就要借助于UseKeychain選項(xiàng)郭怪。在~/.ssh/config文件中添加下面這段話,問(wèn)題就可以解決了:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
在github下建自己的Repository刊橘。Create a New Repository,建好之后鄙才,可以具體操作了,下面是終端操作git上面的一些操作指令
touch README.md //新建一個(gè)記錄提交操作的文檔
git init //初始化本地倉(cāng)庫(kù)
git add README.md //添加
git commit -m "first commit"http://提交到要地倉(cāng)庫(kù)促绵,并寫一些注釋
git remote add origin git@github.com:youname/Test.git //連接遠(yuǎn)程倉(cāng)庫(kù)并建了一個(gè)名叫:origin的別名
git push -u origin master //將本地倉(cāng)庫(kù)的東西提交到地址是origin的地址攒庵,master分支下
git add * :添加所有的文件
還有一些錯(cuò)誤:
如果輸入$ Git remote add origin git@github.com:djqiang(github帳號(hào)名)/gitdemo(項(xiàng)目名).git
提示出錯(cuò)信息:fatal: remote origin already exists.
解決辦法如下:
1、先輸入$ git remote rm origin
2败晴、再輸入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不會(huì)報(bào)錯(cuò)了浓冒!
3、如果輸入$ git remote rm origin 還是報(bào)錯(cuò)的話尖坤,error: Could not remove config section 'remote.origin'. 我們需要修改gitconfig文件的內(nèi)容
4裆蒸、找到你的github的安裝路徑,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc
5糖驴、找到一個(gè)名為gitconfig的文件僚祷,打開它把里面的[remote "origin"]那一行刪掉就好了佛致!
如果輸入$ git push origin master
提示出錯(cuò)信息:error:failed to push som refs to .......
解決辦法如下:
1、先輸入$ git pull origin master //先把遠(yuǎn)程服務(wù)器github上面的文件拉下來(lái)
2辙谜、再輸入$ git push origin master
3俺榆、如果出現(xiàn)報(bào)錯(cuò) fatal: Couldn't find remote ref master或者fatal: 'origin' does not appear to be a git repository以及fatal: Could not read from remote repository.
4、則需要重新輸入$ git remote add origingit@github.com:djqiang/gitdemo.git
//刪除文件夾下的所有 .svn 文件
find . -name ".svn" | xargs rm -Rf
//刪除文件夾下的所有 .git 文件
find . -name ".git" | xargs rm -Rf
其實(shí)装哆,控制操作github罐脊,1.可以操作github里面具體頁(yè)面,頁(yè)面操作蜕琴。2.可以用終端命令行萍桌。3.可以用sourceTree。
參考:http://blog.csdn.net/leikezhu1981/article/details/47664215
SSH keys
An SSH key allows you to establish a secure connection between your computer and GitLab.
Before generating an SSH key, check if your system already has one by running cat ~/.ssh/id_rsa.pub. If you see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.
To generate a new SSH key, just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename, you can press enter to use the default.
It is a best practice to use a password for an SSH key, but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can't be altered or retrieved.
ssh-keygen -t rsa -C "wangguanjun@rongzi.com"
Use the code below to show your public key.
cat ~/.ssh/id_rsa.pub
Copy-paste the key to the 'My SSH Keys' section under the 'SSH' tab in your user profile. Please copy the complete key starting with ssh- and ending with your username and host.
Use code below to copy your public key to the clipboard. Depending on your OS you'll need to use a different command:
Windows:
clip < ~/.ssh/id_rsa.pub
Mac:
pbcopy < ~/.ssh/id_rsa.pub
Linux (requires xclip):
xclip -sel clip < ~/.ssh/id_rsa.pub
Deploy keys
Deploy keys allow read-only access to multiple projects with a single SSH key.
This is really useful for cloning repositories to your Continuous Integration (CI) server. By using deploy keys, you don't have to setup a dummy user account.
If you are a project master or owner, you can add a deploy key in the project settings under the section 'Deploy Keys'. Press the 'New Deploy Key' button and upload a public SSH key. After this, the machine that uses the corresponding private key has read-only access to the project.
You can't add the same deploy key twice with the 'New Deploy Key' option. If you want to add the same key to another project, please enable it in the list that says 'Deploy keys from projects available to you'. All the deploy keys of all the projects you have access to are available. This project access can happen through being a direct member of the projecti, or through a group. See def accessible_deploy_keys in app/models/user.rb for more informati