什么是git
参考下这里:https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-Git-%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F
站长写的很详细。
如何安装
安装
[root@OpenKM ~]# yum -y install git
CentOS Stream 9 - BaseOS 718 B/s | 4.0 kB 00:05
CentOS Stream 9 - AppStream 790 B/s | 4.1 kB 00:05
CentOS Stream 9 - Extras packages 823 B/s | 4.3 kB 00:05
Dependencies resolved.
=======================================================================================================================================
Package Architecture Version Repository Size
=======================================================================================================================================
Installing:
git x86_64 2.31.1-2.el9.2 appstream 124 k
Installing dependencies:
git-core x86_64 2.31.1-2.el9.2 appstream 3.6 M
git-core-doc noarch 2.31.1-2.el9.2 appstream 2.5 M
perl-Error noarch 1:0.17029-7.el9 appstream 42 k
perl-Git noarch 2.31.1-2.el9.2 appstream 43 k
perl-TermReadKey x86_64 2.38-11.el9 appstream 37 k
perl-lib x86_64 0.65-479.el9 appstream 24 k
Transaction Summary
=======================================================================================================================================
Install 7 Packages
Total download size: 6.4 M
Installed size: 32 M
Downloading Packages:
(1/7): git-2.31.1-2.el9.2.x86_64.rpm 24 kB/s | 124 kB 00:05
(2/7): perl-Error-0.17029-7.el9.noarch.rpm 387 kB/s | 42 kB 00:00
(3/7): git-core-doc-2.31.1-2.el9.2.noarch.rpm 455 kB/s | 2.5 MB 00:05
(4/7): perl-TermReadKey-2.38-11.el9.x86_64.rpm 755 kB/s | 37 kB 00:00
(5/7): perl-lib-0.65-479.el9.x86_64.rpm 527 kB/s | 24 kB 00:00
(6/7): perl-Git-2.31.1-2.el9.2.noarch.rpm 85 kB/s | 43 kB 00:00
(7/7): git-core-2.31.1-2.el9.2.x86_64.rpm 608 kB/s | 3.6 MB 00:06
---------------------------------------------------------------------------------------------------------------------------------------
Total 553 kB/s | 6.4 MB 00:11
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : git-core-2.31.1-2.el9.2.x86_64 1/7
Installing : git-core-doc-2.31.1-2.el9.2.noarch 2/7
Installing : perl-lib-0.65-479.el9.x86_64 3/7
Installing : perl-TermReadKey-2.38-11.el9.x86_64 4/7
Installing : perl-Error-1:0.17029-7.el9.noarch 5/7
Installing : perl-Git-2.31.1-2.el9.2.noarch 6/7
Installing : git-2.31.1-2.el9.2.x86_64 7/7
Running scriptlet: git-2.31.1-2.el9.2.x86_64 7/7
Verifying : git-2.31.1-2.el9.2.x86_64 1/7
Verifying : git-core-2.31.1-2.el9.2.x86_64 2/7
Verifying : git-core-doc-2.31.1-2.el9.2.noarch 3/7
Verifying : perl-Error-1:0.17029-7.el9.noarch 4/7
Verifying : perl-Git-2.31.1-2.el9.2.noarch 5/7
Verifying : perl-TermReadKey-2.38-11.el9.x86_64 6/7
Verifying : perl-lib-0.65-479.el9.x86_64 7/7
Installed:
git-2.31.1-2.el9.2.x86_64 git-core-2.31.1-2.el9.2.x86_64 git-core-doc-2.31.1-2.el9.2.noarch
perl-Error-1:0.17029-7.el9.noarch perl-Git-2.31.1-2.el9.2.noarch perl-TermReadKey-2.38-11.el9.x86_64
perl-lib-0.65-479.el9.x86_64
Complete!
[root@OpenKM ~]#
基本语法与逻辑:
Git 常用的是以下 6 个命令:git clone、git push、git add 、git commit、git checkout、git pull
说明:
- workspace:工作区
- staging area:暂存区/缓存区
- local repository:版本库或本地仓库
- remote repository:远程仓库
Git 的工作流程:
大家也可以参考这里写的很详细了:
https://www.runoob.com/git/git-commit-history.html
如何解决访问github等网站仓库连接失败的问题
问题现象:
默认情况下尝试下载github资源(我们以一个开源项目phpMyFAQ为例)
[root@OpenKM phpMyFAQ]# git clone https://github.com/phpMyFAQ/docker-hub.git
Cloning into 'docker-hub'...
fatal: unable to access 'https://github.com/phpMyFAQ/docker-hub.git/': OpenSSL SSL_read: error:0A000126:SSL routines::unexpected eof while reading, errno 0
[root@OpenKM phpMyFAQ]#
你将得到unable to access
的问题。
这是由于github被gw屏蔽的原因导致的,因此,要解决问题需要首选找到可访问的github服务器的地址。
解决方法(更新于2022-10-18)
如何获取Github可访问的ip地址
我们访问链接:https://ping.chinaz.com/github.com(站长之家)。利用它的ping
检测目标网站的IP地址。
往下看测试结果:
不出意外,国内因该都是失败的
不要着急,继续往下翻:
可以看到国外的节点所访问的某些地址是正常的。
在这些正常的地址中,我们随便选择一个;比如:140.82.121.3
然后,在需要使用git工具的计算机中尝试ping 140.82.121.3
[root@xml-srv phpMyFAQ]# ping 140.82.121.3
PING 140.82.121.3 (140.82.121.3) 56(84) bytes of data.
64 bytes from 140.82.121.3: icmp_seq=1 ttl=47 time=246 ms
64 bytes from 140.82.121.3: icmp_seq=2 ttl=47 time=245 ms
64 bytes from 140.82.121.3: icmp_seq=3 ttl=47 time=245 ms
64 bytes from 140.82.121.3: icmp_seq=4 ttl=47 time=246 ms
64 bytes from 140.82.121.3: icmp_seq=5 ttl=47 time=246 ms
64 bytes from 140.82.121.3: icmp_seq=6 ttl=47 time=246 ms
^C
--- 140.82.121.3 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5007ms
rtt min/avg/max/mdev = 245.269/245.661/245.944/0.259 ms
[root@xml-srv phpMyFAQ]#
可以很直观看到地址是可以访问的,那么我们就可以用该ip
来设置github hosts
地址。
注意:若上面这个地址无法ping通或者质量不好,可以多测试几个,然后选择最优的地址作为hosts对应的github网站地址。
//我的环境是Linux设置hosts的方法如下;如果你是其他OS,参照本文设定原理即可
[root@xml-srv phpMyFAQ]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@xml-srv phpMyFAQ]# vi /etc/hosts
[root@xml-srv phpMyFAQ]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
140.82.121.3 github.com www.github.com
[root@xml-srv phpMyFAQ]#
//设置完成后进行域名的ping测试
[root@xml-srv phpMyFAQ]# ping github.com
PING github.com (140.82.121.3) 56(84) bytes of data.
64 bytes from github.com (140.82.121.3): icmp_seq=1 ttl=47 time=246 ms
64 bytes from github.com (140.82.121.3): icmp_seq=2 ttl=47 time=246 ms
^C
--- github.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 245.715/246.004/246.293/0.289 ms
[root@xml-srv phpMyFAQ]# ping www.github.com
PING github.com (140.82.121.3) 56(84) bytes of data.
64 bytes from github.com (140.82.121.3): icmp_seq=1 ttl=47 time=246 ms
^C
--- github.com ping statistics ---
2 packets transmitted, 1 received, 50% packet loss, time 1002ms
rtt min/avg/max/mdev = 245.850/245.850/245.850/0.000 ms
[root@xml-srv phpMyFAQ]#
再次尝试git访问GitHub
[root@xml-srv phpMyFAQ]# git clone https://github.com/phpMyFAQ/docker-hub.git
Cloning into 'docker-hub'...
remote: Enumerating objects: 90, done.
remote: Total 90 (delta 0), reused 0 (delta 0), pack-reused 90
Receiving objects: 100% (90/90), 13.18 KiB | 116.00 KiB/s, done.
Resolving deltas: 100% (47/47), done.
[root@xml-srv phpMyFAQ]# ll
total 0
drwxr-xr-x. 3 root root 146 Oct 18 12:14 docker-hub
[root@xml-srv phpMyFAQ]#
到此,就成功事先了国内对github的访问。
若出现fatal: unable to access 'https://github.com/.../.git': Could not resolve host: github.com错误则:
问题现象:
[root@xml-srv docker-hub]# docker build -t phpmyfaq:xml3.0 .
Sending build context to Docker daemon 89.09kB
Step 1/25 : FROM composer:latest as composer
---> 93cce8060354
Step 2/25 : WORKDIR /app
---> Using cache
---> 342315ade676
Step 3/25 : ARG PMF_BRANCH="3.0"
---> Using cache
---> f2f6fbf883dc
Step 4/25 : RUN set -x && git clone --depth 1 -b $PMF_BRANCH https://github.com/thorsten/phpMyFAQ.git /app
---> Running in d9dd1d71a18b
+ git clone --depth 1 -b 3.0 https://github.com/thorsten/phpMyFAQ.git /app
Cloning into '/app'...
fatal: unable to access 'https://github.com/thorsten/phpMyFAQ.git/': Could not resolve host: github.com
The command '/bin/sh -c set -x && git clone --depth 1 -b $PMF_BRANCH https://github.com/thorsten/phpMyFAQ.git /app' returned a non-zero code: 128
[root@xml-srv docker-hub]#
[root@xml-srv docker-hub]#
解决办法:
[root@xml-srv phpMyFAQ]git config --global --unset http.proxy
[root@xml-srv phpMyFAQ]git config --global --unset https.proxy
然后,必须要清空dns缓存护着重启服务器后再试。
清空缓存的方法大家自行百度一下,方法有很多。