当前位置 : 首页 » 文章分类 :  开发  »  Git-安装配置

Git-安装配置

Git安装笔记

Git官方网站
https://git-scm.com/

Git官方下载(即msysgit)
https://git-scm.com/downloads

msysgit(Git for Windows)
https://git-for-windows.github.io/

《Pro Git》中文版,Git官方资料
https://git-scm.com/book/zh/v2

Git教程-廖雪峰
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000


安装Git

4 起步 - 安装 Git
https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git

Windows安装msysgit

从Git官网 https://git-scm.com/downloads 或 msysgit(Git for Windows) http://gitforwindows.org/ 下载并安装Git for Windows。安装后就有了Git Bash命令行工具。,注意在“Configuring the line ending conversions”选项中,
第一个选项:如果是跨平台项目,在windows系统安装,选择;
第二个选项:如果是跨平台项目,在Unix系统安装,选择;
第三个选项:非跨平台项目,选择。
注意:msysgit会自动安装cygwin

Linux安装git

在 CentOS 等Linux发行版上
sudo yum install git
git 需要依赖 curl,zlib,openssl,expat,libiconv 等库,会自动安装这些依赖。

Mac安装git

安装Brew后,执行
brew install git
安装目录为:/usr/local/Cellar/git/2.17.0

安装 Git
https://git-scm.com/book/zh/v2/起步-安装-Git

安装Git-廖雪峰

git version查看git版本

git --versiongit version 查看git版本


SSH密钥连接Git远程仓库

生成SSH Key

ssh-keygen -P '' 生成密钥对, 使用默认加密算法rsa,默认文件名,得到私钥文件 id_rsa,公钥文件 id_rsa.pub
最后看到这样的界面,就成功设置ssh key了:


Git生成ssh key

添加公钥(pub keys)到Git仓库

登陆 github/coding/gitlab 等git托管商web页面,一般在账户设置中有ssh keys配置页面,新建一个pub key,打开刚才生成的公钥文件id_rsa.pub,复制其内容粘贴到文本框中。

测试ssh连接是否成功

Git Bash中执行:
ssh -T git@github.com
返回:

$ ssh -T git@github.com
Hi masikkk! You've successfully authenticated, but GitHub does not provide shell access.

说明连接成功。

添加ssh密钥之前使用此命令测试连接会提示如下错误信息:

$ ssh -T git@github.com
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5: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
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
Permission denied (publickey).

增加-v参数可将整个传输过程中的操作都显示出来,哪一步出错很明显就可以看出来:
ssh -vT git@github.com

$ ssh -vT git@github.com
OpenSSH_7.1p2, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /c/Users/MaSi/.ssh/config
debug1: /c/Users/MaSi/.ssh/config line 1: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.252.123] port 22.
debug1: Connection established.
debug1: identity file /c/Users/MaSi/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/MaSi/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.1
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /c/Users/MaSi/.ssh/known_hosts:1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/MaSi/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.252.123]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi masikkk! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3080, received 1776 bytes, in 0.5 seconds
Bytes per second: sent 6159.6, received 3551.8
debug1: Exit status 1

不同git仓库使用不同ssh密钥

在ssh的~/.ssh/config配置中指定域名与密钥文件的对应关系
例如我同时使用了GitHub和Coding,可以如下配置:

Host github.com *.github.com
    IdentityFile ~/.ssh/id_rsa

Host git.coding.net
    IdentityFile ~/.ssh/coding_rsa

Permission denied (publickey) 错误

原因
Permission denied (publickey)错误的原因就是找不到私钥文件,为什么这里会出现这种情况呢?
首先要知道ssh会根据~/.ssh/config配置文件决定哪个host用哪个密钥文件,安装GitHub桌面版后,会自动修改SSH的配置文件~/.ssh/config中的”IdentityFile”为github_rsa,从而导致使用Git Bash命令行push代码时出现Permission denied (publickey)错误**,但通过GitHub桌面提交代码没有问题。

解决

  1. 把GitHub for Windows桌面版自动生成的~/.ssh/github_rsa~/.ssh/github_rsa.删除
  2. 使用ssh-keygen命令生成自己的私钥id_rsa和公钥id_rsa.pub
  3. 把公钥id_rsa.pub内容添加到GitHub
  4. 修改~/.ssh/config配置文件,指定github.com使用id_rsa密钥
    Host github.com *.github.com
     IdentityFile ~/.ssh/id_rsa
    

代理环境下使用ssh连接git远程仓库

代理环境下需要给ssh配置http代理
安装msysgit后,打开Git Bash,新建~/.ssh/config用户ssh配置文件(如果没有的话),配置如下:

Host github.com *.github.com
    ProxyCommand connect -H 172.17.18.80:8080 %h %p   #设置代理
    IdentityFile ~/.ssh/github_rsa

注意:必须设置IdentityFile项,否则提示Permission denied (publickey).

ProxyCommand命令格式
ProxyCommand connect 代理服务器地址 端口 %h %p
其中connect是个程序,Windows用户下载安装了msysgit,里面有这个程序。
或者
ProxyCommand corkscrew 代理服务器地址 端口 %h %p
需要corkscrew这个软件。如果使用corkscrew,那么下载后解压缩,把corkscrew.exe和cygwin1.dll拷贝到msysgit的bin目录中。
其中%h表示目标地址,%p是目标端口。这句可以用在命令行里。

测试ssh,输入命令:
$ ssh -T git@github.com
如果返回结果:Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.说明成功


在HTTPS端口上使用SSH协议

问题
安装配置好Cntlm,参看前面的文章进行配置,我们就可以在Github上检入检出https://描述的repository了,如果你尝试使用git://或git@github.com格式的地址访问,则在某些代理环境下可能会报出如下错误:
HTTP/1.1 502 Proxy Error ( The specified Secure Sockets Layer (SSL) port is not allowed. Forefront TMG is not configured to allow SSL requests from this port. Most Web browsers use port 443 for SSL requests. )
引起这一问题的原因是git://使用的9418端口以及基于SSH的git@github.com格式的地址使用的22端口在你的网络环境里(特别是代理服务端)被防火墙屏蔽了。这是极为常见的,大多数的代理环境基于安全考虑,会屏蔽除80(http)和443(https)之外的端口。那这是否意味着我们就不能在这种网络环境里使用git://或git@github.com格式的地址呢?请继续看下文。

在HTTPS端口上使用SSH(Using SSH over the HTTPS port)
虽然可以在Github上使用HTTPS协议push代码(使用的是用户名在Github上的用户名和密码),但通常来说,使用SSH检出和提交代码是更为安全,更适合团队开发,那么怎么解决前文提到的问题呢?
对此,Github通过将ssh的连接端口指定为https使用的443,使用了另外一个主机名:ssh.github.com来解决这一问题,这样我们可以通过大多数代理服务器允许的443端口,使用SSH访问Github了,这一点在前面参考文章中提到的config文件里也配置过了,就是Host为ssh.github.com的部分,关于这部分的说明和配置,Github的官方帮助文档也有说明:https://help.github.com/articles/using-ssh-over-the-https-port

测试本地网络环境是否支持在HTTPS端口上使用SSH
打开GitHub Shell 输入以下命令:
$ ssh -T -p 443 git@ssh.github.com
如果返回结果:Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.说明本地网络环境支持在HTTPS端口上使用SSH

修改SSH配置文件指定默认使用443端口
如果经过上一步测试可通过443端口连接GitHub,可以修改ssh配置文件做映射,以后不用每次都在命令中输入端口了
ssh配置文件位于~/.ssh/config
增加如下配置:

Host github.com
  Hostname ssh.github.com
  Port 443

保存后用$ ssh -T git@github.com测试是否成功

透过代理连接SSH
http://blog.csdn.net/asx20042005/article/details/7041294

SSH Over Proxy
https://daniel.haxx.se/docs/sshproxy.html

Corkscrew
http://agroman.net/corkscrew/

在公司的局域网使用git或github 设置代理
http://www.chenyudong.com/archives/use-git-or-github-in-company-local-net.html
此方法比在HTTPS端口上使用SSH高到不知哪里去了,直接配置代理。

Git代理设置与访问Github
http://blog.csdn.net/bluishglc/article/details/37807501

Using SSH over the HTTPS port
https://help.github.com/articles/using-ssh-over-the-https-port/


上一篇 XML

下一篇 常用工具软件

阅读
评论
2.2k
阅读预计10分钟
创建日期 2016-01-13
修改日期 2019-02-15
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论