01-git

0. Summary

  • 版本控制系统(Version Control System, VCS)
  • Subversion(SVN)
  • Git从本质上而言是按内容寻址的文件系统

1. 版本控制

是一套系统,按时间顺序记录某一个或一系列文件的变更,让你可以查看其以前的特定版本

  1. 文件或整个项目恢复到先前的状态
  2. 比对文件随时间的变更
  3. 查看什么人最后做出的更改可能会造成麻烦,谁在何时引入了一个问题

2. 集中式版本控制系统

CVCS(Centralized Version Control System)

  • CVS
  • Subversion
  • Perforce

  • 优点:
    1. 所有人都可以在一定程度上掌握其他人在项目中都做了什么
    2. 管理员可以精细地控制每个人的权限
    3. 维护一个集中式版本控制系统要比在每台客户机上都维护一个数据库简单得多
  • 缺点:
    1. 单点故障

3. 分布式版本控制系统

DVCS(Distributed Version Control System)

  • git
  • Mercurial
  • Bazaar
  • Darcs

优点:

  1. 速度快
  2. 设计简洁
  3. 对于非线性开发强有力的支持(数以千计的并行分支)
  4. 完全的分布式设计
  5. 能够有效地处理像Linux内核这种大型项目(速度以及数据量)

1. Git的概念

  1. Git技术:公司必备,一定要会
  2. Git概念:
    • Git是一个免费的、开源的分布式版本控制系统,可以快速高效地处理小型到大型的项目
  3. 什么是版本控制?
    • 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统
  4. 为什么要使用版本控制?
    1. 可以将某个文件回溯到之前的状态,甚至将整个项目都回退到过去某个时间点的状态
    2. 整个项目中的文件改的改、删的删,也照样可以轻松恢复到原先的样子。额外增加的工作量却微乎其微
    3. 可以比较文件的变化细节,查出最后是谁修改了哪个地方,从而找出导致怪异问题出现的原因,又是谁在何时报告了某个功能缺陷等等
  5. 版本控制系统的分类:
    1. 集中化的版本控制系统
      • 集中化的版本控制系统诸如CVS、SVN、Perforce等,都有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人都通过客户端连到这台服务器,取出最新的文件或者提交更新。这已成为版本控制系统的标准做法
      • 每个人都可以在一定程度上看到项目中的其他人正在做些什么。管理员也可以轻松掌控每个开发者的权限
      • 中央服务器的单点故障。如果服务器宕机一小时,那么在这一小时内, 谁都无法提交更新,无法协同工作 27911701758311_.pic
    2. 分布式的版本控制系统
      • Git、BitKeeper等,客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来 27921701758540_.pic
      • 更进一步,许多这类系统都可以指定和若干不同的远端代码仓库进行交互。这样,就可以在同一个项目中分别和不同工作小组的人相互协作 27931701758575_.pic
      • 在管理项目时存放的不是项目版本与版本之间的差异,它存的是索引(所需磁盘空间很少)所以每个客户端都可以放下整个项目的历史记录 27941701758603_.pic

2. Git简史

27951701758643_.pic

Linux => 人越来越多 => 代码优化的越来越好 => 项目管理工具 => Bitkeeper => 终止合作 => 一周Git => 一个月内将Linux管理在Git上 => 开源,免费 => 用户群大

3. Git的安装过程

  1. Git官网open in new window
  2. 安装过程:一直下一步
  3. 点击《Git Bash Here》打开Git终端

4. Git结构

工作区 => git add => 暂存区 => git commit => 本地库

5. 代码托管中心

  • 代码托管中心是干嘛的呢?
    • 已经有了本地库,本地库可以帮我们进行版本控制
    • 维护远程库
  • 托管中心种类:
    • 局域网环境下:可以搭建GitLab服务器作为代码托管中心
    • 外网环境下:可以由GitHub或Gitee作为代码托管中心

6. 本地库、远程库交互方式

1. 团队内部协作

1381701759722_.pic

2. 跨团队协作

1391701759725_.pic

7. 初始化本地仓库

  1. 创建一个文件夹
  2. 打开Git终端,Git Bash Here
    • 进入以后先对字体和编码进行设置:
1401701759734_.pic
  • 在Git中命令跟Linux是一样的:
# 1. 查看git安装版本
git --version

# 2. 清屏
clear

# 3. 设置签名
git config --global user.name "listao"

# 4. 设置用户名和邮箱
git config --global user.email "ooxx@qq.com"

# 5. 本地仓库的初始化操作。`.git`目录是隐藏的
git init
Initialized empty Git repository in /Users/listao/sry/tmp/.git/

# 6. `.git`目录下内容
➜  .git git:(master) pwd
/Users/listao/sry/tmp/.git
➜  .git git:(master) ll
total 24
-rw-r--r--   1 listao  staff    23B Dec  6 10:30 HEAD
-rw-r--r--   1 listao  staff   137B Dec  6 10:30 config
-rw-r--r--   1 listao  staff    73B Dec  6 10:30 description
drwxr-xr-x  15 listao  staff   480B Dec  6 10:30 hooks
drwxr-xr-x   3 listao  staff    96B Dec  6 10:30 info
drwxr-xr-x   4 listao  staff   128B Dec  6 10:30 objects
drwxr-xr-x   4 listao  staff   128B Dec  6 10:30 refs
# 注意事项: .git目录下的本地库相关的子目录和子文件不要删除,不要胡乱修改

8. Git常用命令

1. add、commit

# 1. 先创建一个文件:
# 2. 将文件提交到暂存区
git add ooxx.txt

# 3. 将暂存区的内容提交到本地库
git commit -m 'first'
[master (root-commit) 0442bca] first
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ooxx.txt

2. status

查看工作区、暂存区状态

# 1. 创建一个文件
➜  tmp git:(master)# vim ooxx
➜  tmp git:(master)# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	ooxx

nothing added to commit but untracked files present (use "git add" to track)

# 2. 提交至:暂存区
➜  tmp git:(master)# git add ooxx
➜  tmp git:(master)# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   ooxx


# 3. 修改
➜  tmp git:(master)# git commit -m 'ooxx'
[master 24b15ab] ooxx
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ooxx
➜  tmp git:(master)# vim ooxx
➜  tmp git:(master)# 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:   ooxx

no changes added to commit (use "git add" and/or "git commit -a")

# 4. 修改
➜  tmp git:(master)# git add ooxx
➜  tmp git:(master)# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   ooxx

# 5. 提交
➜  tmp git:(master)# git commit -m 'status'
[master 4790c94] status
 1 file changed, 1 insertion(+)
➜  tmp git:(master) git status
On branch master
nothing to commit, working tree clean

3. log

  • 查看提交的,显示从最近到最远的日志
  • 分页效果,分屏效果
    • 下一页:Space
    • 上一页:b
    • 到尾页了,显示END
    • 退出:q
➜  tmp git:(master)# git log
commit 4790c94bbedc71f48f39c1198fae305c9bf5e155 (HEAD -> master)
Author: listao_pro <1471444606@qq.com>
Date:   Wed Dec 6 11:14:18 2023 +0800

    status

commit 24b15ab39cd3d8be9d6bc7067fed28775013f5c7
Author: listao_pro <1471444606@qq.com>
Date:   Wed Dec 6 11:12:27 2023 +0800

    ooxx

commit 0442bca6b44c9a9987a3716dc5f9e9c21109572d
Author: listao_pro <1471444606@qq.com>
Date:   Wed Dec 6 11:06:40 2023 +0800

    first

➜  tmp git:(master)# git log --pretty=oneline
4790c94bbedc71f48f39c1198fae305c9bf5e155 (HEAD -> master) status
24b15ab39cd3d8be9d6bc7067fed28775013f5c7 ooxx
0442bca6b44c9a9987a3716dc5f9e9c21109572d first
(END)

➜  tmp git:(master)# git log --oneline
4790c94 (HEAD -> master) status
24b15ab ooxx
0442bca first


# HEAD@{数字}。数字含义:指针回到当前这个历史版本需要走多少步
➜  tmp git:(master)# git reflog
4790c94 (HEAD -> master) HEAD@{0}: commit: status
24b15ab HEAD@{1}: commit: ooxx
0442bca HEAD@{2}: commit (initial): first

4. reset

前进、后退历史版本

  • 复制:在终端中选中就是复制了
  • 粘贴:右键 + paste
image-20240609161418547

1. hard

  • 最常用(放弃当前所有修改)
  • 本地库指针移动。清空暂存区,清空工作区
➜  tmp git:(master)# git reset --hard 24b15ab39
HEAD is now at 24b15ab ooxx
image-20240609161543521
image-20240609161517248

2. mixed

  • 本地库指针移动。清空暂存区,改变工作区(当前状态 - 回退版本)
image-20240609161932075

3. soft

  • 本地库指针移动。改变暂存区(当前状态 - 回退版本),清空工作区
image-20240609162004708

5. diff

  • git是按照行为单位管理数据
  • 工作区、暂存区中文件进行比较
➜  tmp git:(master)# git diff ooxx
diff --git a/ooxx b/ooxx
index 1cb6fd9..62253da 100644
--- a/ooxx
+++ b/ooxx
@@ -1 +1,3 @@
-111111111111111111111111111
+m11111111
+wwww11111111
+qqqqq

# 工作区、暂存区中所有文件差异
# git diff

# 比较暂存区、本地库(具体某次提交)
# git diff 24b15ab39c ooxx
# git diff HEAD ooxx

9. 分支

1. 什么是分支

  • 在版本控制过程中,使用多条线同时推进多个任务。这里面说的多条线,就是多个分支
  • 优点
    • 同时多个分支可以并行开发,互相不耽误,互相不影响,提高开发效率
    • 如果有一个分支功能开发失败,直接删除这个分支就可以了,不会对其他分支产生任何影响。
1871701759914_.pic

2. 查看、创建、切换

➜  tmp git:(master)# git branch -v
* master c54c3f6 2

➜  tmp git:(master)# git branch b1
➜  tmp git:(master)# git branch -v
  b1     c54c3f6 2
* master c54c3f6 2

➜  tmp git:(master)# git checkout b1
➜  tmp git:(b1)git branch -v
* b1     c54c3f6 2
  master c54c3f6 2

3. 冲突问题

  1. 两个分支merge时,出现conflict
  2. push到远程仓库时,出现conflict
➜  tmp git:(b1)# vim ooxx
➜  tmp git:(b1)# git add .
➜  tmp git:(b1)# git commit -m 'b1'
[b1 94887ab] b1
 1 file changed, 1 insertion(+), 3 deletions(-)

➜  tmp git:(b1)# git checkout master
Switched to branch 'master'
➜  tmp git:(master)# vim ooxx
➜  tmp git:(master)# git add .
➜  tmp git:(master)# git commit -m 'master'
[master af0507a] master
 1 file changed, 1 insertion(+), 3 deletions(-)

➜  tmp git:(master)# git merge b1
Auto-merging ooxx
CONFLICT (content): Merge conflict in ooxx
Automatic merge failed; fix conflicts and then commit the result.
➜  tmp git:(master)# git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   ooxx

no changes added to commit (use "git add" and/or "git commit -a")

➜  tmp git:(master)# vim ooxx

<<<<<<< HEAD
master
=======
b1
>>>>>>> b1

➜  tmp git:(master)# git add .
➜  tmp git:(master)# git commit -m 'merge'

10. GitHub账号注册

11. 初始化本地库

mkdir proj

cd proj

git init

# 关联远程库
git remote add origin http://192.168.*.*:8929/al_diff/al_doc.git

➜  al_doc git:(master)# git remote
origin
➜  al_doc git:(master)# git remote -v
origin	http://192.168.*.*:8929/al_diff/al_doc.git (fetch)
origin	http://192.168.*.*:8929/al_diff/al_doc.git (push)

# 推送远程库
git push origin master

# 克隆
git clone http://192.168.*.*:8929/al_diff/al_doc.git

# 加入团队push
git push origin master
remote: Permission to zhaoshanshan3366/GitResp2.git denied to zhaoshanfatal: unable to access https://github.com/zhaoshanshan3366/GitResp2. requested URL returned error: 403

# 远程库拉取
git fetch origin master # 工作区中的文件并没有更新
git checkout master
git merge origin/master

git pull origin master

12. 跨团队合作

  1. 得到远程库的地址
  2. 进行fork操作
  3. 克隆到本地,进行修改。push到远程库
  4. 进行《pull request》操作
  5. 进行《审核》操作
  6. 可以互相留言。查看具体提交的内容
  7. 确定通过以后,可以进行合并

13. SSH免密登录

  • ssh方式好处:不用每次都进行身份验证
  • 缺点:只能针对一个账号。仓库地址必须为ssh://git@192.168.*.*:2224/al_proj/al_doc.git
  1. 进入用户的主目录
  2. 执行命令,生成一个.ssh目录
  3. id_rad.pub内容copy到github的《SSH and GPG keys》中
# 三次默认回车
# 1. keygen => key_generation
# 2. 注意:C要大写
# 3. github注册账号时的邮箱
ssh-keygen -t rsa -C ***@qq.com

# .ssh目录出现两个文件:id_rsa、id_rsa.pub

14. 本地库、远程库交互

git pull origin master --allow-unrelated-histories

# -u => –set-upstream
# -f => -force
git push -u origin master -f