配置github公钥实现无密提交代码

来源:互联网 发布:东方时尚网络授课手机 编辑:程序博客网 时间:2024/06/05 00:48

         git常用命令如下:

         git clone git@github.com:yourname/repo.git  //从github上克隆项目到本地

         git add filename                                         //将新增或者修改的文件添加到要提交的列表

         git commit -m "add or modify file"           //提交并注释

         git push origin master                               //提交本地修改到主分支

         最后两个命令其实就是我们在eclipse里面通过项目->右键->Team->commit选择文件之后进行的commit&push操作。

         我们本地安装了git命令之后,可以从github上clone下来项目,如果对项目做了更改,还可以提交到项目中。但是如果默认无配置提交,需要我们输入github用户名密码。为了实现免密提交,我们需要配置github公钥(公钥没有限制可以针对不同的机器配置多个)。配置公钥可以分为以下三步。

第一步、生成密钥

第二步、将生成的密钥配置到github的https://github.com/settings/keys

密钥的内容就是根目录下.ssh/id_rsa.pub的内容,我们可以将其拷贝到key的位置,title可以随便填写。

通过如下命令来验证是否配置成功:

$ ssh -T git@github.com

以上操作表明我们本地到github上可以实现无需输入用户名密码就可以提交文件。但是实际我们再来提交代码时仍然提示需要输入用户名密码,这是怎么回事?

[root@client css]# git push origin masterUsername for 'https://github.com': yangfei2013Password for 'https://yangfei2013@github.com': remote: Invalid username or password.fatal: Authentication failed for 'https://github.com/yangfei2013/ghblog.git/'

原来git提交有两个地址一个是https://github.com/yourname/repo.git,还有一个地址是git@github.com:yourname/repo.git,如果我们配置了公钥,那么再提交代码时需要使用git@github.com:yourname/repo.git的url来提交。这样就需要我们配置这个提交的URL地址,没有修改配置之前我们通过 git config --list可以看到相关配置。

[root@client css]# git config --listuser.name=yangfei2013user.email=yangfei5459@126.comcore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=trueremote.origin.url=https://github.com/yangfei2013/ghblog.gitremote.origin.fetch=+refs/heads/*:refs/remotes/origin/*branch.master.remote=originbranch.master.merge=refs/heads/master

第三步、配置提交的URL,更改项目路径下.git/config文件,修改remote.origin.url

更改之后,我们再通过git config --list查看remote.origin.url确实发生了变化

更改之后再次提交代码,就不出现提示输入用户名和密码了。

[root@client css]# vi ../.git/config [root@client css]# git push origin masterWarning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.Counting objects: 7, done.Compressing objects: 100% (3/3), done.Writing objects: 100% (4/4), 358 bytes | 0 bytes/s, done.Total 4 (delta 2), reused 0 (delta 0)remote: Resolving deltas: 100% (2/2), completed with 2 local objects.To git@github.com:yangfei2013/ghblog.git   8dd24e8..3fb9ab9  master -> master


0 0
原创粉丝点击