一、什么是Git
Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。
二、Git与SVN对比
- 共同点
都是用来存放代码和版本控制 - 区别
a. 工作模式:git是分布式;svn是中心化(集中化)。
b. 使用上:git入门周期长,但目前使用率高;svn入门简单。
c. 分支:git创建和维护分支方便;svn创建和维护分支繁琐。
三、部署Git
1.1 部署在window上
下载地址[Git - Downloading Package (git-scm.com)](https://git-scm.com/download/win)
根据系统选择32位或64位
1.2 部署在macOS上
#需要先安装brew
$ brew install git
详细安装流程传送门
1.3 部署在linux上
#centos/redhat
yum -y install git
#debian/ubuntu
apt install -y git
这里只介绍常用的linux版本,其它linux系统参考git官网
Git (git-scm.com)
1.4 安装新版本教程
首先在Github 下载最新版本
这里可以选择版本和对应的压缩包进行下载(这里可以将链接复制到linux中通过wget下载)
在2022年2月14日时(这是一个有趣的日期!!!哈哈哈),最新版为2.35.1
#测试环境
[root@git~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core) #系统版本
[root@git~]# uname -r
3.10.0-1160.el7.x86_64 #内核版本
#下载最新版并解压
[root@git~]# wget https://github.com/git/git/archive/refs/tags/v2.35.1.tar.gz
[root@git~]# tar xf v2.35.1.tar.gz
#安装软件依赖包
[root@git~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
#编译安装
[root@git~]# cd git-2.35.1/
[root@git~/git-2.35.1]# make prefix=/usr/local/git all
[root@git~/git-2.35.1]# make prefix=/usr/local/git install
#卸载低版本的git
[root@git~]# rpm -qa git
git-1.8.3.1-23.el7_8.x86_64
[root@git~]# rpm -e perl-Git-1.8.3.1-23.el7_8.noarch git-1.8.3.1-23.el7_8.x86_64 gettext-devel-0.19.8.1-3.el7.x86_64 intltool-0.50.2-7.el7.noarch
#配置环境变量
[root@git~/git-2.35.1]# vim /etc/profile.d/git.sh
[root@git~/git-2.35.1]# cat /etc/profile.d/git.sh
export PATH=$PATH:/usr/local/git/bin
[root@git~/git-2.35.1]# . /etc/profile.d/git.sh
#测试
[root@git~/git-2.35.1]# git version
git version 2.35.1
四、Git详解
4.1 Git初使用-开发阶段
#配置用户
[root@git~]# git config --global user.name 'yunweixiaoyu'
[root@git~]# git config --global user.email '[email protected]'
[root@git~]# git config --global color.ui true
[root@git~]# git config --global --list
user.name=yunweixiaoyu
[email protected]
color.ui=true
[root@git~]# cat .gitconfig
[user]
name = yunweixiaoyu
email = [email protected]
[color]
ui = true
#创建项目代码目录
[root@git~]# mkdir -p /project/test/app
[root@git~]# cd /project/test/app
#对目录进行初始化
[root@git/project/test/app]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m
Initialized empty Git repository in /project/test/app/.git/
[root@git/project/test/app]# ll .git/
total 16
drwxr-xr-x 2 root root 6 Feb 14 22:41 branches
-rw-r--r-- 1 root root 92 Feb 14 22:41 config
-rw-r--r-- 1 root root 73 Feb 14 22:41 description
-rw-r--r-- 1 root root 23 Feb 14 22:41 HEAD
drwxr-xr-x 2 root root 4096 Feb 14 22:41 hooks
drwxr-xr-x 2 root root 21 Feb 14 22:41 info
drwxr-xr-x 4 root root 30 Feb 14 22:41 objects
drwxr-xr-x 4 root root 31 Feb 14 22:41 refs
#书写代码文件
[root@git/project/test/app]# touch live.html
[root@git/project/test/app]# echo "直播功能完成50%" >live.html
[root@git/project/test/app]# cat live.html
直播功能完成50%
#查看状态
[root@git/project/test/app]# git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
live.html #此时这里为红色,因为代码只存在工作区,没有添加到暂存区
nothing added to commit but untracked files present (use "git add" to track)
#添加到暂存区
[root@git/project/test/app]# git add . #这里也可以书写为git add live.html
[root@git/project/test/app]# git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: live.html #绿色表示添加到暂存区
#提交到本地仓库
[root@git/project/test/app]# git commit -m "直播50%"
[master (root-commit) ae37f5c] 直播50%
1 file changed, 1 insertion(+)
create mode 100644 live.html
[root@git/project/test/app]# git status
On branch master
nothing to commit, working tree clean
#代码进行升级
[root@git/project/test/app]# vim live.html
[root@git/project/test/app]# cat live.html
直播功能完成70%
[root@git/project/test/app]# git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: live.html
no changes added to commit (use "git add" and/or "git commit -a")
[root@git/project/test/app]# git add .
[root@git/project/test/app]# git commit -m "直播70%"
[master 7787f00] 直播70%
1 file changed, 1 insertion(+), 1 deletion(-)
#查看commit日志(后面回滚用到commit id)
[root@git/project/test/app]# git log
commit 7787f00275ce9353da9af442cf1a9430d9cb9b7d (HEAD -> master)
Author: yunweixiaoyu
Date: Mon Feb 14 23:00:49 2022 +0800
直播70%
commit ae37f5c6d5b3251f4af4e407292fd7197cf77324
Author: yunweixiaoyu
Date: Mon Feb 14 22:54:38 2022 +0800
直播50%
[root@git/project/test/app]# git re
rebase relink repack request-pull revert
reflog remote replace reset
[root@git/project/test/app]# git reflog
7787f00 (HEAD -> master) HEAD@{0}: commit: 直播70%
ae37f5c HEAD@{1}: commit (initial): 直播50%
#回滚(删除代码测试回滚)
[root@git/project/test/app]# rm -rf live.html
[root@git/project/test/app]# ll
total 0
[root@git/project/test/app]# git reset --hard ae37f5c
HEAD is now at ae37f5c 直播50%
[root@git/project/test/app]# ll
total 4
-rw-r--r-- 1 root root 22 Feb 14 23:06 live.html
[root@git/project/test/app]# cat live.html
直播功能完成50%
[root@git/project/test/app]# git log
commit ae37f5c6d5b3251f4af4e407292fd7197cf77324 (HEAD -> master)
Author: yunweixiaoyu
Date: Mon Feb 14 22:54:38 2022 +0800
直播50%
[root@git/project/test/app]# git reflog
ae37f5c (HEAD -> master) HEAD@{0}: reset: moving to ae37f5c
7787f00 HEAD@{1}: commit: 直播70%
ae37f5c (HEAD -> master) HEAD@{2}: commit (initial): 直播50%
#注意:当回滚到最开始的版本的时候,后面的版本ID号通过git log无法查看,此时可以使用git reflog进行查看,然后进行回滚
4.2 Git区域与状态
4.3 Git命令
命令 | 含义 |
---|---|
git init | 初始化本地仓库目录 |
git config --global | 邮箱,用户名,颜色 |
git add | 提交数据到缓冲区(暂存区) git add . (所有文件) 或 git add 文件 |
git commit | 把暂存区的数据提交到本地仓库 git commit -m "标记/说明" |
git status | 显示工作空间的状态 |
git reset | 回滚 |
git reset --soft cid(版本号) | 把指定的版本数据内容下载到暂存区 |
git reset HEAD | 暂存区--->工作空间(被修改的状态) |
git checkout | 文件下载到工作空间并可以使用 git checkout . 或 git checkout 文件 |
git reset --hard | 版本号把本地仓库指定版本信息数据下载到工作目录中 |
4.4 Git分支
1)什么是分支?
分支是为了将修改记录的整体流程分叉保存。分叉后的分支不受其他分支的影响,所以在同一个远程仓库里可以同时进行多个修改。分叉后的分支还可以进行合并。
默认情况下,项目都有一个主分支master
2)分支案例
#查看项目分支
[root@git/project/test/app]# git branch
* master
#创建分支
[root@git/project/test/app]# git branch shopping
#切换到shopping分支
[root@git/project/test/app]# git checkout shopping
M live.html
Switched to branch 'shopping'
#查看当前分支
[root@git/project/test/app]# git branch
master
* shopping
#修改代码
[root@git/project/test/app]# cat live.html
直播功能完成50%
购物功能完成70%
#添加到暂存区,上传本地仓库
[root@git/project/test/app]# git add .
[root@git/project/test/app]# git commit -m "购物70%"
[shopping 244fb7e] 购物70%
1 file changed, 2 insertions(+), 1 deletion(-)
#此时在shopping分支上查看代码内容
[root@git/project/test/app]# cat live.html
直播功能完成50%
购物功能完成70%
#切换到master上查看代码内容
[root@git/project/test/app]# git checkout master
Switched to branch 'master'
[root@git/project/test/app]# git branch
* master
shopping
[root@git/project/test/app]# cat live.html
直播功能完成70%
#合并分支
[root@git/project/test/app]# git merge shopping -m "直播70%+购物70%"
Updating 7787f00..244fb7e
Fast-forward (no commit created; -m option ignored)
live.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[root@git/project/test/app]# cat live.html
直播功能完成50%
购物功能完成70%
3)临时分支(fixbug/hotfix分支)
如果代码中出现bug,这时便可以创建分支进行修复,修复完成后进行分支的删除即可
[root@git/project/test/app]# git branch fixbug
[root@git/project/test/app]# git branch
fixbug
* master
shopping
[root@git/project/test/app]# git checkout fixbug
Switched to branch 'fixbug'
[root@git/project/test/app]# vim live.html
[root@git/project/test/app]# cat live.html
直播功能完成50%
购物功能完成70%
bug已成功修复
[root@git/project/test/app]# git add .
[root@git/project/test/app]# git commit -m "bug成功修复"
[fixbug 0405bc0] bug成功修复
1 file changed, 1 insertion(+)
[root@git/project/test/app]# git checkout master
Switched to branch 'master'
[root@git/project/test/app]# git merge fixbug -m "bug修复完成后与主分支合并"
Updating 244fb7e..0405bc0
Fast-forward (no commit created; -m option ignored)
live.html | 1 +
1 file changed, 1 insertion(+)
[root@git/project/test/app]# cat live.html
直播功能完成50%
购物功能完成70%
bug已成功修复
[root@git/project/test/app]# git branch
fixbug
* master
shopping
#修复并合并后通过git branch -d 删除指定分支
[root@git/project/test/app]# git branch -d fixbug
Deleted branch fixbug (was 0405bc0).
[root@git/project/test/app]# git branch
* master
shopping
4)分支命令汇总
git分支命令 | 含义 |
---|---|
git branch | 查看分支 |
git branch name | 创建分支 |
git branch -d name | 删除分支 |
git checkout name | 切换分支 |
git merge name | 将指定分支合并到当前分支 |
git checkout -b name | 创建分支并切换到这个分支 |
注意:表中name表示分支名
4.5 tag标签
给commit id设置别名,也就是标签,相对方便记忆。
#添加标签(默认是对当前最新的commit id进行添加)
[root@git/project/test/app]# git tag -a "v1.0" -m "直播和购物"
#查看当前分支
[root@git/project/test/app]# git tag
v1.0
#对指定版本的commit id进行添加标签(格式如下)
#git tag -a "标签名" -m "描述" commitID
[root@git/project/test/app]# git reflog
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{0}: merge fixbug: Fast-forward (no commit created; -m option ignored)
244fb7e (shopping) HEAD@{1}: checkout: moving from fixbug to master
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{2}: commit: bug成功修复
244fb7e (shopping) HEAD@{3}: checkout: moving from master to fixbug
244fb7e (shopping) HEAD@{4}: merge shopping: Fast-forward (no commit created; -m option ignored)
7787f00 HEAD@{5}: checkout: moving from shopping to master
244fb7e (shopping) HEAD@{6}: commit: 购物70%
7787f00 HEAD@{7}: checkout: moving from master to shopping
7787f00 HEAD@{8}: reset: moving to 7787f00
7787f00 HEAD@{9}: reset: moving to 7787f00
ae37f5c HEAD@{10}: reset: moving to ae37f5c
7787f00 HEAD@{11}: commit: 直播70%
ae37f5c HEAD@{12}: commit (initial): 直播50%
[root@git/project/test/app]# git tag -a "v0.1" -m "直播" ae37f5c
[root@git/project/test/app]# git reflog
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{0}: merge fixbug: Fast-forward (no commit created; -m option ignored)
244fb7e (shopping) HEAD@{1}: checkout: moving from fixbug to master
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{2}: commit: bug成功修复
244fb7e (shopping) HEAD@{3}: checkout: moving from master to fixbug
244fb7e (shopping) HEAD@{4}: merge shopping: Fast-forward (no commit created; -m option ignored)
7787f00 HEAD@{5}: checkout: moving from shopping to master
244fb7e (shopping) HEAD@{6}: commit: 购物70%
7787f00 HEAD@{7}: checkout: moving from master to shopping
7787f00 HEAD@{8}: reset: moving to 7787f00
7787f00 HEAD@{9}: reset: moving to 7787f00
ae37f5c (tag: v0.1) HEAD@{10}: reset: moving to ae37f5c
7787f00 HEAD@{11}: commit: 直播70%
ae37f5c (tag: v0.1) HEAD@{12}: commit (initial): 直播50%
#将标签上传远程仓库
#这里需要先配置好远程仓库(配置见4.7小节)
git push origin --tags #上传所有标签
git push origin "标签名" #上传指定标签
生产环境常用的标签名和含义
1. Alpha 预览测试版
2. Beta 公开测试版
3. RC 最终测试版
4. Release 正式发布版本
5. Final 最终版,正式发布版的一直表示方法
6. Stable 稳定版
4.6 gitignore(了解)
上传代码的过程中,代码目录中的隐藏文件,里面的内容默认不会上传到远程仓库,代码生成的临时文件(需要排除上传)。
需要排除上传时,可以在代码的根目录创建文件.gitignore书写需要排除的内容即可。
这个一般是开发人员进行书写,这里不做过多介绍,如果需要可以参考github上的各类语言的ignore文件
五、远程仓库gitee
这里我们先简单的介绍一下gitee的使用,将本地仓库上传至gitee远程仓库中,后续我们会搭建开源的gitlab作为远程仓库。
使用gitee前我们需要在gitee官网上注册gitee账号,然后进行后续操作。
5.1 gitee上创建仓库
创建后会显示如下内容
5.2 远程连接gitee
#全局配置
git config --global user.name "悠然"
git config --global user.email "[email protected]"
#因为本地git仓库已经创建过了这里就不重新创建了
#添加远程仓库
[root@git/project/test/app]# git remote add origin https://gitee.com/yunweixiaoyu/app-live.git
#查看远程仓库
[root@git/project/test/app]# git remote -v
origin https://gitee.com/yunweixiaoyu/app-live.git (fetch)
origin https://gitee.com/yunweixiaoyu/app-live.git (push)
#上传本地仓库内容到远程仓库
[root@git/project/test/app]# git push -u origin "master"
Username for 'https://gitee.com': yunweixiaoyu #gitee账户名
Password for 'https://[email protected]': #gitee密码
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (12/12), 1022 bytes | 1022.00 KiB/s, done.
Total 12 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/yunweixiaoyu/app-live.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
#上传tag标签到gitee上
[root@git/project/test/app]# git push origin --tags
Username for 'https://gitee.com': yunweixiaoyu #gitee账户名
Password for 'https://[email protected]': #gitee密码
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 314 bytes | 314.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/yunweixiaoyu/app-live.git
* [new tag] v0.1 -> v0.1
* [new tag] v1.0 -> v1.0
#下拉代码到本地仓库
[root@git/project/test/app]# git pull origin master #或者git fetch origin master
#下载代码到目录
[root@git/project/test/app]# git clone https://gitee.com/yunweixiaoyu/app-live.git
Cloning into 'app-live'...
Username for 'https://gitee.com': yunweixiaoyu
Password for 'https://[email protected]':
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (14/14), done.
5.3 配置远程仓库秘钥认证
使用远程仓库gitee的过程中,我们在上传和下拉代码都需要输入用户名和密码,因此我们可以配置秘钥认证进行简化操作。
1)创建密钥
#生成密钥对
[root@git~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #默认回车即可
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #默认回车不创建密码
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:x6DNxWtF/ObsnwO/GFZT3XNInkbrgj1um56iuPKXwQ4 root@git
The key's randomart image is:
+---[RSA 2048]----+
| .. o |
| . ..+ +o|
| . o ..*.=|
| + + = oo +|
| ..S * ++.o |
| E oo . ++ .|
| o o o+o |
| . .+ ...++o.|
| o+o.. o=. ++|
+----[SHA256]-----+
#查看公钥,将内容复制
[root@git~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9xaoi5RVExW1dEQR22XO+sAsEmvuYO/P+NAubeiwDs4ki4xNRdpsXGLn1k+ZAM3z4NP+qzhITWh8HTh5Gje0C+aI/DqnAIFB9LKCRJVeuLtvUazyjKBA3tW1yTXUa3duAWYc4uqdPPB5WvuvHxtwPDpANZY0GMnyZ3Gh1AND2m6RWEMatowkDRxsacGRYi0l4Fg99T/vKg6rvrEoIYDUm2X/L95Qlv5fLVIW0cR5N6oQ5TR8Zizo5s5ZvPgNCQe2zEr3GvBmHRxUwgMMEZywQfHwrLgjSv07/m2HoZywwygZUE5/mOfvlD4RlZ5a+DLbfK6rAVfho1c3iNkXFCW+R root@git
2)公钥配置到远程仓库
进入设置
按步骤将公钥复制即可
3)添加ssh协议的远程仓库
远程仓库有两种访问方法
- https:每次连接都需要输入账户和密码
- ssh:密钥认证,最常用
复制内容
#删除旧的远程仓库,重新配置ssh协议远程仓库
root@git/project/test/app]# git remote remove origin
[root@git/project/test/app]# git remote add origin [email protected]:yunweixiaoyu/app-live.git
[root@git/project/test/app]# git remote -v
origin [email protected]:yunweixiaoyu/app-live.git (fetch)
origin [email protected]:yunweixiaoyu/app-live.git (push)
#测试
[root@git~]# git clone [email protected]:yunweixiaoyu/app-live.git
Cloning into 'app-live'...
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes #第一次使用需要使用yes确认
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (14/14), done.