git-ssh 配置和使用GitHub仓库做代码管理
1、设置Git的user name和email:(如果是第一次的话)
$ git config --global user.name "walt008"
$ git config --global user.email "walt008@cnvirtue.com"
2、生成密钥
# ssh-keygen -t rsa -C "walt008@cnvirtue.com"
连续3个回车。
如果不需要密码的话。
最后得到了两个文件:id_rsa和id_rsa.pub。在家目录的.ssh下
如果不是第一次,就选择overwrite.
3、添加密钥到ssh-agent
确保 ssh-agent 是可用的。ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序,其实ssh-agent就是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent来完成整个认证过程。
# eval "$(ssh-agent -s)"
添加生成的 SSH key 到 ssh-agent。
$ ssh-add ~/.ssh/id_rsa
4、登陆Github, 添加 ssh 。
把id_rsa.pub文件里的内容复制到这里
5、测试:
$ ssh -T git@github.com
你将会看到:
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?
填yes 回车,看到如下提示说明连接成功。
Hi walt008! You've successfully authenticated, but GitHub does not provide shell access.
6、开始上传代码做版本管理
可以先用以下命令克隆远程仓库到本地,也可以自建同名仓库然后同步。
#git clone git@github.com:walt008/AutoPXC.git
#仓库ssh链接
[root@walt008 AutoPXC]# echo “# AutoPXC” >> README.md
[root@walt008 AutoPXC]# git init #初始化仓库
Reinitialized existing Git repository in /root/AutoPXC/.git/
[root@walt008 AutoPXC]# git add README.md #添加到上传列队
[root@walt008 AutoPXC]# git commit -m “first commit” #提交
[master (root-commit) 49a76ca] first commit
2 files changed, 180 insertions(+)
create mode 100644 AutoPXC.sh
create mode 100644 README.md
[root@walt008 AutoPXC]# git remote add origin git@github.com:walt008/AutoPXC.git #设置仓库使用
ssh链接
fatal: remote origin already exists. #我在本地gitconfig文件里配置过,所以提示已存在。
[root@walt008 AutoPXC]# git push -u origin master #推送新版本
Counting objects: 4, done.