Git概述
1.1 Git是什么?
Git是免费的开源分布式版本控制系统,也就是一个可以记录文件修改历史和切换文件修改状态的工具。所谓的版本控制,想我们写论文或者给客户方案文件一样,有不同的修改建议和对应的方案,这样会延伸出来不同方案的文件,我们需要保存不同版本的文件,避免搞混不同版本的文件。
![](https://i-blog.csdnimg.cn/blog_migrate/266df03ca5fa75284394b03e3e3ca79d.png)
1.2 为什么需要版本控制
团队开发需要多个人合力完成一个项目,类比一群人建一个房子,A在建左右边的墙,B在建前后的墙,A先盖好了,没有版本控制,相当于B不认A的墙,直接将A的墙推倒了盖B自己的墙,但实际上需要A和B的墙合并起来才可以。这就需要版本控制。
![](https://i-blog.csdnimg.cn/blog_migrate/9509994645aef1f278d1c658e81914db.png)
1.3 版本控制工具
集中式版本控制工具
CVS、SVN、VSS等,单一集中的服务器来管理所有文件的修订版本。
优点:可以看到其他人在做什么,管理员可以进行统一管理和维护;缺点:中央服务器故障就无法更新。
![](https://i-blog.csdnimg.cn/blog_migrate/f4d699615330d361ae641be59a8273ea.png)
分布式版本控制工具
Git、Mercurial、Bazaar等,各个客户端使用本地进行控制,即使在断网情况下都可以进行开发,而且每个客户端保存的都是完整的项目,每个人都有历史记录,更加安全。
![](https://i-blog.csdnimg.cn/blog_migrate/4b2f6f8066585806e6802724a90bff9d.png)
1.4 Git发展历史
![](https://i-blog.csdnimg.cn/blog_migrate/0c7a18063a9cc51f0c46f74269be829b.png)
1.5 Git工作机制
![](https://i-blog.csdnimg.cn/blog_migrate/b7f7460050508109e4de2f7fd6e24589.png)
1.6 Git和代码托管中心
代码托管中心是基于网络服务器的远程代码仓库,也就是远程库。
互联网:GitLab
互联网:GitHub,Gitee码云
Git安装
2.1 下载
官网下载地址:https://git-scm.com/downloads,下载对应操作系统的文件
![](https://i-blog.csdnimg.cn/blog_migrate/14a307df095f69bd1446ab131facc92e.png)
2.2 安装过程
双击下载好的文件,进行安装(可以修改一下默认的安装路径,这里没有修改,直接安装C盘)
![](https://i-blog.csdnimg.cn/blog_migrate/2af204626e6f54bfb3d746b6320fb824.png)
2.3 安装完成
win11第一次安装Git 2.39.2的版本没有让你选择,直接install就完成了
![](https://i-blog.csdnimg.cn/blog_migrate/76e793867b3764f5c5a4a56a91e10aa3.png)
2.4 使用
一开始不太习惯纯命令的话,可以使用Git Gui,分别对应创建仓库、克隆现有仓库和打开现有仓库。
![](https://i-blog.csdnimg.cn/blog_migrate/e86201d31a571cf7601a75d2fd3c4b8c.png)
一般使用Git Bash,桌面上出现的也是Git Bash
![](https://i-blog.csdnimg.cn/blog_migrate/e5e5a7823282a87f7843206658234159.png)
Git常用命令
命令名词 | 作用 |
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化库 |
git status | 查看本地状态 |
git add 文件名 | 添加到缓存库 |
git commit -m "日志信息" 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
3.1 设置用户签名
签名用于区别不用操作者身份,首次安转Git都要设置用户签名,否则无法提交代码。
git config --global user.name xxx
git config --global user.email xxxx123@163.com
3.2 初始化本地库
进去到存储Git的文件夹下,右键打开Git Bash,输入git init进行初始化库,其中win11的菜单隐藏在更多选项
![](https://i-blog.csdnimg.cn/blog_migrate/3b4a7c1dbbfb581d23177f66630aadd0.png)
3.3 查看本地库状态
首次查看:git status
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
新增文件: 新建txt文件
![](https://i-blog.csdnimg.cn/blog_migrate/147ef899da0392f196d86dcb9e0a00e9.png)
再次查看:git status,增加未提交文件
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
3.4 添加暂存区
将工作区文件添加到暂存区:git add hello.txt
查看状态:git status
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
删除暂存区的文件: git rm --cached hello.txt
$ git rm --cached hello.txt
rm 'hello.txt'
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
这个时候再查看状态,就又会变成未提交状态,工作区的文件还在,暂存区的文件删除。
![](https://i-blog.csdnimg.cn/blog_migrate/229d5c6861b0f85d868da2dc9a8ccae9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/674862587b705fcb7fb792452d8e2a0f.png)
3.5 提交本地库
提交语法:git commit -m "日志信息" 文件名
$ git commit -m "my first commit" hello.txt
[master (root-commit) 67731b1] my first commit
1 file changed, 6 insertions(+)
create mode 100644 hello.txt
查看状态:git status
$ git status
On branch master
nothing to commit, working tree clean
3.6 修改文件
修改文件,并查看状态:发现已经修改的文件。
![](https://i-blog.csdnimg.cn/blog_migrate/fa59860519d3302759c3382b7895c259.png)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
将修改的文件并再次添加本地库
git add hello.txt
git commit -m "my second commit" hello.txt
[master c187e5d] my second commit
1 file changed, 1 insertion(+), 1 deletion(-)
再次查看状态
$ git status
On branch master
nothing to commit, working tree clean
3.7 历史版本
查看历史版本
git reflog
c187e5d (HEAD -> master) HEAD@{0}: commit: my second commit
67731b1 HEAD@{1}: commit (initial): my first commit
$ git log
commit c187e5db0607991f18575a9d0cbb12bfcb7fab3b (HEAD -> master)
Author: jinc <2442727880@qq.com>
Date: Tue Mar 14 09:36:10 2023 +0800
my second commit
commit 67731b1393080c12a4c9cc18f33eea6af0b76ad7
Author: jinc <2442727880@qq.com>
Date: Tue Mar 14 09:21:56 2023 +0800
my first commit
版本穿梭(切换历史版本)
本质上是底层Head指针移动
git reset --hard 版本号
git分支操作
![](https://i-blog.csdnimg.cn/blog_migrate/78b5749ecbbc7f48137ee0acb6fe42f7.png)
4.1 什么是分支
在版本控制器中,会推进多个任务,为了让每个任务都能同步进行,通过为每个任务创建单独的分支,每个分支就是将开发主线上分离下来,每个任务不会相互影响,同时也方便后期合并任务。
![](https://i-blog.csdnimg.cn/blog_migrate/8112faa70562fbbea19d27ccbef74804.png)
4.2 分支的优点
同时推进多个任务,提高开发效率。
每个分支相互独立,不会直接产生影响
4.3 分支的操作
查看分支
git branch -v
创建分支
git branch 分支名
切换分支
git checkout 分支名
修改分支
重新添加暂存区,并添加本地库
合并分支
正常合并,即hot-fix合并到master里
git merge hot-fix
冲突合并
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
当两个版本都有修改时2,则需要手动合并
![](https://i-blog.csdnimg.cn/blog_migrate/d468bf8cdb941b70a13f8fd8b76c56b8.png)
后面还要进行添加暂存区和提交本地库,但本地库的提交中不要再加文件名
$ git add hello.txt
$ git commit -m "merger"
[master 95b90f9] merger
4.4 分支切换和分支合并的原理
![](https://i-blog.csdnimg.cn/blog_migrate/61b23cfdbe3692ea58421373b16e06d8.png)
其中master和hot-fix是指向某个版本的指针,head是决定哪一个分支,如果再创建分支,则再加一个指针。
Git团队协作机制
5.1 团队内协作
![](https://i-blog.csdnimg.cn/blog_migrate/31526473fcbbb17b16f8afb76ff01543.png)
5.2 跨团队协作
![](https://i-blog.csdnimg.cn/blog_migrate/11b1de90606dfc3977ae2bee786b1f3a.png)