详细学习能够到:Git 官方教程html
[root@centos7 ~] yum install git [root@centos7 ~] git --version
Git 版本:git version 1.8.3.1git
Git Hub 选择 release版本github
Git 的依赖 curl-devel expat-devel gettext-devel openssl-devel zlib-devel
进行下载、解压、配置、编译、安装web
[root@centos7 ~] yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel [root@centos7 ~] wget https://codeload.github.com/git/git/tar.gz/v2.20.0-rc0 [root@centos7 ~] tar -xvf v2.20.0-rc0 [root@centos7 ~] make configure [root@centos7 ~] ./configure [root@centos7 ~] make && make install
此处使用 ./configure 直接使用默认配置,实际上和 yum 没什么区别了shell
[root@centos7 ~] git --version
Git 版本:git version 2.20.0-rc0vim
为了访问的便捷,咱们使用 git 用户的身份来建立代码仓库,实际上使用任何用户都是能够的,区别在于在 git clone 的时候,需将 git@server 改为别的用户名
[root@centos7 ~] adduser git [root@centos7 ~] passwd git
The authorized_keys file in SSH specifies the SSH keys that can be used for logging into the user account for which the file is configured.
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2 # but this is overridden so installations will only check .ssh/authorized_keys AuthorizedKeysFile .ssh/authorized_keys
因此咱们明白了 authorized_keys 文件是用以保存已受权的客户端公钥。具体参见: SSH原理
若是在建立「ssh-key」的时候使用了「passphrase」 ,那么使用「SSH」的「method: publickey」方式进行链接时会报出一个提示:Enter passphrase for key '/home/git/.ssh/id_rsa',与免密登陆违背了segmentfault
[root@centos7 ~] su git [root@centos7 ~] ssh-keygen -t rsa [root@centos7 ~] cat /home/git/.ssh/id_rsa.pub >> /home/git/.ssh/authorized_keys [root@centos7 ~] chmod 700 /home/git/.ssh +---[RSA 2048]----+ |o. +XE=o. | # -t:指定生成密钥类型(rsa、dsa、ecdsa等) |o.=+=*= | # -P:指定passphrase,用于确保私钥的安全 | . . o B o | # -f:指定存放密钥的文件 | . = . | |o+.oo* o o | |o o ..+ + . | # authorized_keys #保存已受权的客户端公钥 |. o +.oS. . | # known_hosts #保存已认证的远程主机ID |. o B.+ . | # id_rsa.pub #保存公钥 | . | # id_rsa #保存私钥 +----[SHA256]-----+
在 authorized_keys添加须要受权 shell 登陆用户的公钥,首先「server」向「client」发送一个随机数值,用户在「client」返回使用密钥加密随机数后的密文在「server」进过公钥解密与原随机数进行比对从而完成一次认证过程windows
[root@centos7 ~] ssh -vv git@104.199.134.0 #追溯两层错误
若是出现错误,根据 debug 的关键字搜索。其中大部分缘由能够分为两种:centos
1. SSH 配置问题 2. 文件权限问题
StrictModes no #关闭严格校验 RSAAuthentication yes #容许RSA认证 PubkeyAuthentication yes #容许公钥认证 AuthorizedKeysFile .ssh/authorized_keys #ssh文件位置 PasswordAuthentication yes #容许密码认证
Protocol 2 SyslogFacility AUTH LogLevel debug PermitRootLogin yes RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials yes UsePAM yes AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS X11Forwarding yes Subsystem sftp /usr/libexec/openssh/sftp-server
更改文件权限安全
[root@centos7 ~] chmod 700 /home/git/.ssh [root@centos7 ~] chmod 600 /home/git/.ssh/authorized_keys
[root@centos7 ~]$ ssh git@104.199.134.0 The authenticity of host '104.199.134.0 (104.199.134.0)' can't be established. ECDSA key fingerprint is SHA256:49g0X6kyudjfjCa/QBoZwf0mbPZnFphYjMRV/LrQPpQ. ECDSA key fingerprint is MD5:cc:ef:83:ab:e4:33:00:9e:0f:62:87:df:62:01:73:62. Are you sure you want to continue connecting (yes/no)?
登陆后 .ssh 下就生成了 known_hosts
[root@centos7 ~]$ vim /home/git/.ssh/known_hosts 104.199.134.0 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABEBPA/O+a8s92uinv3NKnVzGqaohbX6vqDVGMoE5vs+PuT7NXivy5aSkRutROcN/H8AhnBLaK5HWGsqwBRw8FXgSY=
由于git用户是专门用来上传代码的,因此禁用git用户的登陆权限
将 /sbin/nologin 做为 git 用户的登陆 shell,即禁止 git用户 shell 登陆
[root@centos7 ~] usermod -s /sbin/nologin git #禁止shell登陆 [root@centos7 ~] usermod -s /bin/bash git #恢复默认shell
[root@centos7 ~] su - git [root@centos7 ~] git init sea.git
git init 用来搭建本地仓库,在工程中使用的 commit 保存到本地仓库
[root@centos7 ~] su - git [root@centos7 ~] git init --bare sea.git
git init --bare 用来搭建远程仓库,在工程中使用 push 推送到远程仓库
前面咱们建立了 git 用户,那么 git 用户的 home 目录变能够用来当仓库路径
这里的 git 仓库即是远程仓库了,用户们使用 push 命令将更新推送到远程仓库,使用 --bare 选项运行 git init 来创建一个裸仓库
仓库后缀都是 .git
建立远程仓库目录并初始化了空的仓库
[root@centos7 ~] cd /home/git/ [root@centos7 ~] mkdir sea.git [root@centos7 ~] git init --bare sea.git
git 不只能够作本地开发的版本控制,更多还用与团队写做的迭代开发。普通仓库保存着工程代码、版本历史,远程的裸仓库即版本库仅包含记录着版本历史的文件
使用 git init –bare 方法建立一个所谓的裸仓库,之因此叫裸仓库是由于这个仓库只保存git历史提交的版本信息,而不容许用户在上面进行各类git操做,若是你硬要操做的话,只会获得下面的错误: This operation must be run in a work tree
所谓的远程仓库在该协议(Local protocol)中的表示,就是硬盘上的另外一个目录。这常见于团队每个成员都对一个共享的文件系统(例如 NFS)拥有访问权,或者比较少见的多人共用同一台电脑的状况。
用 Git 协议做为访问项目的惟一方法一般是不可取的。通常的作法是,同时提供 SSH 接口,让几个开发者拥有推送(写)权限,其余人经过 git:// 拥有只读权限。
这是一个包含在 Git 软件包中的特殊守护进程; 它会监听一个提供相似于 SSH 服务的特定端口(9418),而无需任何受权。打算支持 Git 协议的仓库,须要先建立 git-daemon-export-ok 文件 — 它是协议进程提供仓库服务的必要条件 — 但除此以外该服务没有什么安全措施。要么全部人都能克隆 Git 仓库,要么谁也不能。这也意味着该协议一般不能用来进行推送。你能够容许推送操做;然而因为没有受权机制,一旦容许该操做,网络上任何一个知道项目 URL 的人将都有推送权限。
SSH 也是惟一一个同时支持读写操做的网络协议。也是默认协议
[root@centos7 ~] git clone ssh://user@server/project.git [root@centos7 ~] git clone user@server:project.git
HTTP 或 HTTPS 协议的优美之处在于架设的简便性。基本上,只须要把 Git 的裸仓库文件放在 HTTP 的根目录下,配置一个特定的 post-update 挂钩(hook)就能够搞定(Git 挂钩的细节见第 7 章)。此后,每一个能访问 Git 仓库所在服务器上 web 服务的人均可以进行克隆操做。
操做环境:windows clone 虚拟机的远程仓库
[root@centos7 ~] git clone git@192.168.108.128:/home/git/sea.git # Cloning into 'sea'... # git@192.168.108.128's password: # warning: You appear to have cloned an empty repository.
[root@centos7 ~] git clone git@164.109.134.117:/home/git/sea.git # Cloning into 'test1.git'... # done. # warning: You appear to have cloned an empty repository.
[root@centos7 ~] git clone ssh://git@193.29.97.24:14726/home/git/star.git
[root@centos7 ~] git config --global user.email "sea.star.com" [root@centos7 ~] git config --global user.name "sea" [root@centos7 ~] git config --global -- list # user.email=sea@star.com # user.name=sea
查看远程仓库
[root@centos7 ~] git remote -v # origin git@193.29.97.24:/home/git/sea.git (fetch) # origin git@193.29.97.24:/home/git/sea.git (push)
[root@centos7 ~] git init --bare star.git
[root@centos7 ~] git remote add star ssh://git@193.29.97.24:14726/home/git/star.git
再次查看远程仓库
[root@centos7 ~] git remote -v # origin git@193.29.97.24:/home/git/sea.git (fetch) # origin git@193.29.97.24:/home/git/sea.git (push) # upstream ssh://git@193.29.97.24:14726/home/git/star.git (fetch) # upstream ssh://git@193.29.97.24:14726/home/git/star.git (push)
[root@centos7 ~] git remote rm upstream [root@centos7 ~] git remote -v # origin git@193.29.97.24:/home/git/sea.git (fetch) # origin git@193.29.97.24:/home/git/sea.git (push)
这种方式的多个远程仓库须要分别推送
[root@centos7 ~] git remote set-url --add origin ssh://193.29.97.25:/home/git/taskv2.git [root@centos7 ~] git remote -v # origin git@193.29.97.24:/home/git/sea.git (fetch) # origin git@193.29.97.24:/home/git/sea.git (push) # origin git@193.29.97.25:/home/git/sea.git (push)
提交修改,推送到了两个远程仓库去了
[root@centos7 ~]$ git push origin master # Enumerating objects: 4, done. # Counting objects: 100% (4/4), done. # Compressing objects: 100% (2/2), done. # Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done. # Total 3 (delta 1), reused 0 (delta 0) # To ssh://104.199.134.0:/home/git/star.git # b0785b2..5db23a4 master -> master # Enumerating objects: 4, done. # Counting objects: 100% (4/4), done. # Compressing objects: 100% (2/2), done. # Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done. # Total 3 (delta 1), reused 0 (delta 0) # To ssh://93.179.97.24:27038/home/git/star.git # b0785b2..5db23a4 master -> master
git branch -a bug doc * master remotes/origin/bug remotes/origin/doc remotes/origin/master
git push branch :branchname To ssh://127.0.0.1/home/git/repository.git - [deleted] branchname
Git的升级策略大可能是安全更新,少有重大新特性更新,升级可能会引入系统失效陷阱,由此浪费的时间精力彻底没必要要。
Linux 学习笔记(一):内网穿透
Linux 学习笔记(二):搭建我的Git服务器
Linux 学习笔记(三):Ubuntu 操做系统
Linux 学习笔记(四):Docker
Linux 学习笔记(五):Redis
Linux 学习笔记(六):Linux