生成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): 此处可以自定义路径,不定义而点击回车就是默认的.ssh路径。下面自定义就输入:/Users/wgj/.wgjGithubSSh/wgjgithub_rsa,其中.wgjGithubSSh是建好的文件,wgjgithub_rsa,则是没有创建,生成后是文件的名字。
2.继续操作,输入两次密码,就在路径中有相应的东西了。
3.在github添加ssh key的时候,有两个地方需要操作,1.key的名字,这个可以以邮箱+mac名字,命名,便于以后的区分。2. 需要拷贝key,这个地方注意,不是拷贝文件,而是还有在终端中操作:
进入携带‘wgjgithub_rsa.pub’的文件夹(cd /Users/wgj/.wgjGithubSSh),然后:cat wgjgithub_rsa.pub,打开显示出来,发现是一大串,这才是需要拷贝的东西(公钥)(大串前面的ssh-rsa不要漏掉)。
打开终端,先测试一下你的帐号跟github连上没有:ssh -T git@github.com 如果出现如下提示,表示你连已经连上了.(因为有了第一步,所以不用自己做过多的连接github的操作了,另外,下一次要连接github的时候记得打开第一步的工具:sourcetree).
Hi wgj0909! You've successfully authenticated, but GitHub does not provide shell access.
如果输入$ ssh -T git@github.com
出现错误提示:Permission denied (publickey).因为新生成的key不能加入ssh就会导致连接不上github。
解决办法如下:
1.先输入$ ssh-agent,再输入$ ssh-add ~/.ssh/id_key,这样就可以了。--( ssh-add ~/.ssh/id_key:把专用密钥添加到 ssh-agent 的高速缓存中)
2.如果还是不行的话,输入ssh-add ~/.ssh/id_key 命令后出现报错Could not open a connection to your authentication agent.
解决方法是key用Git Gui的ssh工具生成,这样生成的时候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行来做。
3.最好检查一下在你复制id_rsa.pub文件的内容时有没有产生多余的空格或空行,有些编辑器会帮你添加这些的。
Mac OS SSH密钥在重启后丢失的问题
1. 从macOS Sierra 10.12.2以后,SSH的配置选项中多了“UseKeychain”选项]
2. 要解决这个问题,就要借助于UseKeychain选项。在~/.ssh/config文件中添加下面这段话,问题就可以解决了:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
在github下建自己的Repository。Create a New Repository,建好之后,可以具体操作了,下面是终端操作git上面的一些操作指令
touch README.md //新建一个记录提交操作的文档
git init //初始化本地仓库
git add README.md //添加
git commit -m "first commit"//提交到要地仓库,并写一些注释
git remote add origin git@github.com:youname/Test.git //连接远程仓库并建了一个名叫:origin的别名
git push -u origin master //将本地仓库的东西提交到地址是origin的地址,master分支下
git add * :添加所有的文件
还有一些错误:
如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git
提示出错信息:fatal: remote origin already exists.
解决办法如下:
1、先输入$ git remote rm origin
2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!
3、如果输入$ git remote rm origin 还是报错的话,error: Could not remove config section 'remote.origin'. 我们需要修改gitconfig文件的内容
4、找到你的github的安装路径,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc
5、找到一个名为gitconfig的文件,打开它把里面的[remote "origin"]那一行删掉就好了!
如果输入$ git push origin master
提示出错信息:error:failed to push som refs to .......
解决办法如下:
1、先输入$ git pull origin master //先把远程服务器github上面的文件拉下来
2、再输入$ git push origin master
3、如果出现报错 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
其实,控制操作github,1.可以操作github里面具体页面,页面操作。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