435 lines
12 KiB
Markdown
435 lines
12 KiB
Markdown
|
<h2><center>版本控制</center></h2>
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:Git 简介
|
|||
|
|
|||
|
### 1. 介绍
|
|||
|
|
|||
|
- `Git`是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目
|
|||
|
- `Git`是`Linus Torvalds`为了帮助管理`Linux`内核开发而开发的一个开放源码的版本控制软件
|
|||
|
- `Git`与常用的版本控制工具`CVS`、`Subversion`等不同,它采用了分布式版本库的方式,不必服务器端软件支持
|
|||
|
|
|||
|
### 2. Git 工作流程
|
|||
|
|
|||
|
1. 克隆 Git 资源作为工作目录
|
|||
|
2. 在克隆的资源上添加或修改文件
|
|||
|
3. 如果其他人修改了,你可以更新资源
|
|||
|
4. 在提交前查看修改
|
|||
|
5. 提交修改
|
|||
|
6. 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交
|
|||
|
|
|||
|

|
|||
|
|
|||
|
### 3. git 的工作区、暂存区和版本库
|
|||
|
|
|||
|
**工作区:**就是你在电脑里能看到的目录
|
|||
|
|
|||
|
**暂存区:**英文叫`stage`,或`index`。一般存放在`git`目录下的`index`文件(`.git/index`)中,所以我们把暂存区有时也叫作索引(index)
|
|||
|
|
|||
|
**版本库:**工作区有一个隐藏目录`.git`,这个不算工作区,而是`Git`的版本库
|
|||
|
|
|||
|

|
|||
|
|
|||
|
### 4. Git 安装
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git ~]# yum -y install git curl-devel expat-devel gettext-devel openssl-devel zlib-devel git-core
|
|||
|
```
|
|||
|
|
|||
|
## 二:Git 使用
|
|||
|
|
|||
|
`Git`提供了一个叫做`git config`的工具,专门用来配置或读取相应的工作环境变量
|
|||
|
|
|||
|
这些环境变量,决定了`Git`在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:
|
|||
|
|
|||
|
- `/etc/gitconfig`文件:系统中对所有用户都普遍适用的配置。若使用`git config`时用`--system`选项,读写的就是这个文件
|
|||
|
- `~/.gitconfig`文件:用户目录下的配置文件只适用于该用户。若使用`git config`时用`--global`选项,读写的就是这个文件
|
|||
|
- 当前项目的`Git`目录中的配置文件(也就是工作目录中的`.git/config`文件):这里的配置仅仅针对当前项目有效
|
|||
|
|
|||
|
### 1. Git 用户信息
|
|||
|
|
|||
|
配置个人的用户名称和电子邮件地址:
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git ~]# git config --global user.name "wxin"
|
|||
|
[root@git ~]# git config --global user.email "wxin@163.com"
|
|||
|
```
|
|||
|
|
|||
|
### 2. 查看配置信息
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git ~]# git config --list
|
|||
|
user.name=wxin
|
|||
|
user.email=wxin@163.com
|
|||
|
|
|||
|
用户名和邮箱和github上一致
|
|||
|
```
|
|||
|
|
|||
|
这些配置我们也可以在`~/.gitconfig`看到,如下所示:
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git ~]# cat ~/.gitconfig
|
|||
|
[user]
|
|||
|
name = wxin
|
|||
|
email = wxin@163.com
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
```bash
|
|||
|
git config --global 参数
|
|||
|
取消代理:
|
|||
|
git config --global --unset http.proxy
|
|||
|
git config --global --unset https.proxy
|
|||
|
设置代理:
|
|||
|
git config --global http.proxy http://domain.local\vsilva:Passw0rd@proxyServer:8080
|
|||
|
出现各种 SSL certificate problem 的解决办法:
|
|||
|
git config --global http.sslVerify false
|
|||
|
```
|
|||
|
|
|||
|
### 3. 常用的 git 命令
|
|||
|
|
|||
|
```bash
|
|||
|
# 初始化
|
|||
|
git init
|
|||
|
|
|||
|
# 将某一个文件添加到暂存区
|
|||
|
git add main.cpp
|
|||
|
|
|||
|
# 将文件夹下的所有的文件添加到暂存区
|
|||
|
git add .
|
|||
|
|
|||
|
# 将暂存区中的文件保存成为某一个版本
|
|||
|
git commit -m 'note'
|
|||
|
|
|||
|
# 查看所有的版本日志
|
|||
|
git log
|
|||
|
|
|||
|
# 查看现在暂存区的状况
|
|||
|
git status
|
|||
|
|
|||
|
# 查看现在文件与上一个提交-commit版本的区别
|
|||
|
git diff
|
|||
|
|
|||
|
# 回到上一个版本
|
|||
|
git reset --hard HEAD^
|
|||
|
|
|||
|
# XXX为版本编号,回到某一个版本
|
|||
|
git reset --hard XXXXX
|
|||
|
|
|||
|
# 从主分支pull到本地
|
|||
|
git pull origin master
|
|||
|
|
|||
|
# 从本地push到主分支
|
|||
|
git push -u origin master
|
|||
|
|
|||
|
# push默认主分支 ...
|
|||
|
git push
|
|||
|
```
|
|||
|
|
|||
|
### 4. Git 使用
|
|||
|
|
|||
|
`ssh`链接:
|
|||
|
|
|||
|
客户机上产生公钥上传到`gitlab`的`SSH-Keys`里,`git clone`下载和`git push`上传都没问题,这种方式很安全
|
|||
|
|
|||
|
`ssh`连接`github`:
|
|||
|
|
|||
|
登录`github`,这是`github`的主页(如果没有账户需要注册)
|
|||
|
|
|||
|
`Git`服务器生成秘钥:
|
|||
|
|
|||
|
```bash
|
|||
|
[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:HhgQDguY3A8fAlR+unlWK3liO+o/f8bsQ0+sFHQE5zw root@git
|
|||
|
The key's randomart image is:
|
|||
|
+---[RSA 2048]----+
|
|||
|
|=+=.o. .oo |
|
|||
|
|oo.B o .+. |
|
|||
|
| ..*.o . .E |
|
|||
|
| oo o . . |
|
|||
|
| . ..S o |
|
|||
|
| o o..+ o |
|
|||
|
| o B o* + |
|
|||
|
| +o= B . |
|
|||
|
| .ooo+.+.. |
|
|||
|
+----[SHA256]-----+
|
|||
|
[root@git ~]# cat .ssh/id_rsa.pub
|
|||
|
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTniLfB2SsuRFonPOD94/uCyc8x/wSgkmW0Jq8eRoFvW3+DNHon+iwqufSnkodlkGn2zUun9y6vRGJnU69+/hEFSupZ/eCyUNrnPwwE+BDy2CsRr73pckMmXJERw7SedT5VhDCHFYq6a4fxVZjLhhxbfhi55HisRNc99tJBopkXpOMutr9jvKC+p929Jva4COsqUSya3tHMyv9oXTNwaZCibuIlfUghntry7D3ONH6xV9UyFs8NDXR6fanxkDDBuDvQrxDYncf3Zyi5A7OIC5qJp9MGrftOvyKxFZN/ryzC/asac7StP7SqK7d17XBgwr8jr98JfQ8sVOruSUa6Qkj root@git
|
|||
|
```
|
|||
|
|
|||
|
`Github`添加`Git`服务器秘钥:
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
`git`服务器上创建项目并上传到`github`:
|
|||
|
|
|||
|
```bash
|
|||
|
# 创建本地仓库
|
|||
|
[root@git ~]# mkdir /opt/wxin_cloud
|
|||
|
[root@git ~]# cd /opt/wxin_cloud/
|
|||
|
|
|||
|
# 初始化本地仓库
|
|||
|
[root@git wxin_cloud]# git init
|
|||
|
初始化空的 Git 版本库于 /opt/wxin_cloud/.git/
|
|||
|
|
|||
|
# 创建文件
|
|||
|
[root@git wxin_cloud]# echo "hello wxin" > wxin
|
|||
|
|
|||
|
# 将文件添加到本地暂存区
|
|||
|
[root@git wxin_cloud]# git add .
|
|||
|
|
|||
|
# 将文件提交到本地版本仓库
|
|||
|
[root@git wxin_cloud]# git commit -m "hello"
|
|||
|
[master(根提交) 8eec16d] hello
|
|||
|
1 file changed, 1 insertion(+)
|
|||
|
create mode 100644 wxin
|
|||
|
|
|||
|
# 为本地仓库添加指定的远程仓库
|
|||
|
[root@git wxin_cloud]# git remote add origin git@github.com:wxin712/wxin_cloud.git
|
|||
|
|
|||
|
# 查看节点
|
|||
|
[root@git wxin_cloud]# git branch
|
|||
|
* master
|
|||
|
|
|||
|
# 将文件上传到Github的远程仓库
|
|||
|
[root@git wxin_cloud]# git push -u origin master
|
|||
|
Counting objects: 3, done.
|
|||
|
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
|
|||
|
Total 3 (delta 0), reused 0 (delta 0)
|
|||
|
remote:
|
|||
|
remote: Create a pull request for 'master' on GitHub by visiting:
|
|||
|
remote: https://github.com/wxin712/wxin_cloud/pull/new/master
|
|||
|
remote:
|
|||
|
To git@github.com:wxin712/wxin_cloud.git
|
|||
|
* [new branch] master -> master
|
|||
|
分支 master 设置为跟踪来自 origin 的远程分支 master。
|
|||
|
```
|
|||
|
|
|||
|
`github`查看:
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
注意:这样上传到`github`的分支不能跟`main`分支合并
|
|||
|
|
|||
|
### 5. 版本穿梭
|
|||
|
|
|||
|
版本回退:
|
|||
|
|
|||
|
```bash
|
|||
|
# 用 git log 命令查看:
|
|||
|
# 每一个提交的版本都唯一对应一个 commit 版本号,
|
|||
|
# 使用 git reset 命令退到上一个版本:
|
|||
|
[root@git wxin_cloud]# git log
|
|||
|
commit b0e63bbe3bf54ac7240c06c8fa31bd3f130702e9
|
|||
|
Author: wxin712 <1497427046@qq.com>
|
|||
|
Date: Tue Apr 22 17:57:37 2025 +0800
|
|||
|
|
|||
|
modify
|
|||
|
|
|||
|
commit 8eec16d9cad6dd8df503ab2d346dc5aeb48d6fcb
|
|||
|
Author: wxin712 <1497427046@qq.com>
|
|||
|
Date: Tue Apr 22 17:43:24 2025 +0800
|
|||
|
|
|||
|
hello
|
|||
|
|
|||
|
[root@git wxin_cloud]# git reset --hard 8eec16d9cad6dd8df503ab2d346dc5aeb48d6fcb
|
|||
|
HEAD 现在位于 8eec16d hello
|
|||
|
[root@git wxin_cloud]# git push -f -u origin master
|
|||
|
Total 0 (delta 0), reused 0 (delta 0)
|
|||
|
remote: To git@github.com:wxin712/wxin_cloud.git
|
|||
|
+ b0e63bb...8eec16d master -> master (forced update)
|
|||
|
分支 master 设置为跟踪来自 origin 的远程分支 master。
|
|||
|
```
|
|||
|
|
|||
|
查看结果:
|
|||
|
|
|||
|

|
|||
|
|
|||
|
```bash
|
|||
|
# 查看命令历史,以便确定要回到哪个版本
|
|||
|
[root@git wxin_cloud]# git reflog
|
|||
|
8eec16d HEAD@{0}: reset: moving to 8eec16d9cad6dd8df503ab2d346dc5aeb48d6fcb
|
|||
|
b0e63bb HEAD@{1}: reset: moving to b0e63bb
|
|||
|
8eec16d HEAD@{2}: reset: moving to 8eec16d9cad6dd8df503ab2d346dc5aeb48d6fcb
|
|||
|
b0e63bb HEAD@{3}: reset: moving to b0e63bb
|
|||
|
8eec16d HEAD@{4}: reset: moving to 8eec16d9cad6dd8df503ab2d346dc5aeb48d6fcb
|
|||
|
b0e63bb HEAD@{5}: commit: modify
|
|||
|
8eec16d HEAD@{6}: commit (initial): hello
|
|||
|
|
|||
|
# 回退
|
|||
|
[root@git wxin_cloud]# git reset --hard b0e63bb
|
|||
|
HEAD 现在位于 b0e63bb modify
|
|||
|
[root@git wxin_cloud]# git push -f -u origin master
|
|||
|
Counting objects: 5, done.
|
|||
|
Writing objects: 100% (3/3), 240 bytes | 0 bytes/s, done.
|
|||
|
Total 3 (delta 0), reused 0 (delta 0)
|
|||
|
remote: To git@github.com:wxin712/wxin_cloud.git
|
|||
|
8eec16d..b0e63bb master -> master
|
|||
|
分支 master 设置为跟踪来自 origin 的远程分支 master。
|
|||
|
```
|
|||
|
|
|||
|
查看结果:
|
|||
|
|
|||
|

|
|||
|
|
|||
|
### 6. 分支管理
|
|||
|
|
|||
|
创建分支:
|
|||
|
|
|||
|
```bash
|
|||
|
# 创建dev分支,然后切换到dev
|
|||
|
[root@git wxin_cloud]# git checkout -b dev
|
|||
|
切换到一个新分支 'dev'
|
|||
|
|
|||
|
# 查看当前分支
|
|||
|
[root@git wxin_cloud]# git branch
|
|||
|
* dev
|
|||
|
master
|
|||
|
|
|||
|
# 创建分支
|
|||
|
[root@git wxin_cloud]# git branch test
|
|||
|
```
|
|||
|
|
|||
|
分支切换:
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git wxin_cloud]# git checkout master
|
|||
|
切换到分支 'master'
|
|||
|
[root@git wxin_cloud]# git branch
|
|||
|
dev
|
|||
|
* master
|
|||
|
test
|
|||
|
```
|
|||
|
|
|||
|
合并分支:
|
|||
|
|
|||
|
```bash
|
|||
|
# 把dev分支的工作成果合并到master分支上
|
|||
|
[root@git wxin_cloud]# git merge dev
|
|||
|
更新 b0e63bb..4cc8b4f
|
|||
|
Fast-forward
|
|||
|
readme.txt | 1 +
|
|||
|
1 file changed, 1 insertion(+)
|
|||
|
create mode 100644 readme.txt
|
|||
|
```
|
|||
|
|
|||
|
删除分支:
|
|||
|
|
|||
|
```bash
|
|||
|
# 删除dev分支了
|
|||
|
[root@git wxin_cloud]# git branch -d dev
|
|||
|
已删除分支 dev(曾为 4cc8b4f)。
|
|||
|
|
|||
|
# 查看分支
|
|||
|
[root@git wxin_cloud]# git branch
|
|||
|
* master
|
|||
|
```
|
|||
|
|
|||
|
### 7. GitHub 分支管理
|
|||
|
|
|||
|
注意:`github`默认的分支是`main`而不是`master`
|
|||
|
|
|||
|
创建项目:
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
创建分支:
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
新分支创建文件:
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
合并分支:
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
`main`分支验证是否合并成功:
|
|||
|
|
|||
|

|
|||
|
|
|||
|
### 8. 使用 Github 上的项目
|
|||
|
|
|||
|
下载仓库到本地:
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git ~]# cd /opt/
|
|||
|
[root@git opt]# git clone git@github.com:wxin712/wxin_cloud_test.git
|
|||
|
正克隆到 'wxin_cloud_test'...
|
|||
|
remote: Enumerating objects: 7, done.
|
|||
|
remote: Counting objects: 100% (7/7), done.
|
|||
|
remote: Compressing objects: 100% (4/4), done.
|
|||
|
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
|
|||
|
接收对象中: 100% (7/7), done.
|
|||
|
```
|
|||
|
|
|||
|
进入工作目录,进行文件更新:
|
|||
|
|
|||
|
```bash
|
|||
|
[root@git opt]# ls
|
|||
|
wxin_cloud wxin_cloud_test
|
|||
|
[root@git opt]# cd wxin_cloud_test/
|
|||
|
[root@git wxin_cloud_test]# ls
|
|||
|
README.md wxin
|
|||
|
[root@git wxin_cloud_test]# echo "github's cloud is update" > gitupdate
|
|||
|
[root@git wxin_cloud_test]# git add .
|
|||
|
[root@git wxin_cloud_test]# git commit -m "test update"
|
|||
|
[main f67d4ae] test update
|
|||
|
1 file changed, 1 insertion(+)
|
|||
|
create mode 100644 gitupdate
|
|||
|
[root@git wxin_cloud_test]# git push -u origin main
|
|||
|
Counting objects: 4, done.
|
|||
|
Delta compression using up to 4 threads.
|
|||
|
Compressing objects: 100% (2/2), done.
|
|||
|
Writing objects: 100% (3/3), 324 bytes | 0 bytes/s, done.
|
|||
|
Total 3 (delta 0), reused 0 (delta 0)
|
|||
|
remote: To git@github.com:wxin712/wxin_cloud_test.git
|
|||
|
085d909..f67d4ae main -> main
|
|||
|
分支 main 设置为跟踪来自 origin 的远程分支 main。
|
|||
|
```
|
|||
|
|
|||
|
`github`验证:
|
|||
|
|
|||
|

|