1. 学习目标
- 掌握Git企业应用开发的基本操作以及背后原理,掌握工作区、暂存区、版本库的区别
- 掌握Git的版本管理,例如版本回退、撤销、修改等操作
- 掌握Git的分支管理,例如创建分支、合并分支、删除分支
- 掌握本地仓库与远程仓库之间的区别,达到个人企业级开发水准
- 了解团队协作开发,以及常见的模型
2. Git的安装
环境:宝塔Linux环境(腾讯云服务器)
使用命令如下:
# centos安装
sudo yum install -y git
# centos移除
sudo yum remove -y git
# ubantu安装
sudo apt-get install -y git
# ubantu移除
sudo apt-get remove -y git
# 查看git是否安装成功(版本)
git --version
3. Git学习
3.1 Day01
前置工作:
- 如何初始化本地仓库:
git init
,执行该命令后就会在当前目录生成一个**.git**文件,这个.git文件就是版本库 - 如何配置用户名以及邮箱:
- 配置用户名称:
git [--global] config user.name "xxx"
- 配置用户邮箱:
git [--global] config user.email "xxx"
- 配置用户名称:
可以使用git config -l
查看配置信息
# 配置name
git config user.name "ricejson"
# 配置email
git config user.email "wjjbangbangbang@163.com"
# 查看配置
git config -l
# 移除name
git config --unset user.name
# 移除email
git config --unset user.email
# 配置全局name
git config --global user.name "ricejson"
# 配置全局email
git config --global user.email "wjjbangbangbang@163.com"
- 将工作区文件添加到暂存区中:
git add ./具体文件
- 将暂存区文件添加到master分支下:
git commit -m "具体描述信息"
- 可以使用
git log --pretty=oneline
命令查看历史提交信息
[root@VM-12-4-opencloudos gitcode]# touch readme
[root@VM-12-4-opencloudos gitcode]# ls
readme
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add readme
[root@VM-12-4-opencloudos gitcode]# git commit -m "add readme file"
[master (root-commit) c85412b] add readme file1 file changed, 1 insertion(+)create mode 100644 readme
[root@VM-12-4-opencloudos gitcode]# git log
commit c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 (HEAD -> master)
Author: ricejson <wjjbangbangbang@163.com>
Date: Mon May 13 00:55:20 2024 +0800add readme file
.git文件内容(版本库):
- index:标识暂存区/索引区中的内容
- head:指针,指向master分支下最新提交的记录
- objects:对象区,每一次对于工作区文件的修改都会对应到对象区中的一个.git对象
3.2 Day02
3.2.1 git status 命令
git追踪的是文件的修改而非文件本身!
我们需要清楚的了解到工作区与暂存区的文件是不一样的
输入git status
命令可以查看仓库当前的状态,用来查看该文件内容是否相较于之前提交的版本有所修改
[root@VM-12-4-opencloudos gitcode]# ls
readme
[root@VM-12-4-opencloudos gitcode]# 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: readmeno changes added to commit (use "git add" and/or "git commit -a")
我们可以看出readme文件已经被修改了,但是还没有进行添加和提交
当我们使用git add readme
可以进行添加
[root@VM-12-4-opencloudos gitcode]# git add readme
[root@VM-12-4-opencloudos gitcode]# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)modified: readme
当我们对readme文件添加到暂存区后,git status会显示待commited
[root@VM-12-4-opencloudos gitcode]# git commit -m "add readme"
[master 689991b] add readme1 file changed, 1 insertion(+)
[root@VM-12-4-opencloudos gitcode]# git status
On branch master
nothing to commit, working tree clean
当我们进行commit之后会显示当前tree 是clean的
3.2.2 git diff 命令
git status命令可以让我们知道哪些文件被修改了,但是我们无法看到具体修改的内容,但是git diff命令就可以查看文件的差异:
[root@VM-12-4-opencloudos gitcode]# git add readme
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git diff readme
diff --git a/readme b/readme
index 8c7ca04..045403d 100644
--- a/readme
+++ b/readme
@@ -2,3 +2,5 @@ hello githello world!ricejson
+
+one line
[root@VM-12-4-opencloudos gitcode]# git diff HEAD -- readme
diff --git a/readme b/readme
index 15ca5b9..045403d 100644
--- a/readme
+++ b/readme
@@ -1,2 +1,6 @@hello git
-hello world!t
+hello world!
+
+ricejson
+
+one line
git diff filename
:查看工作区与暂存区中文件的差异git diff HEAD -- filename
:查看工作区和版本库中文件的差异
3.2.3 git reset 版本回退命令
命令:git reset [--soft | --mixed | --hard] [HEAD]
- –mixed为默认参数:表示将版本库以及暂存区内容回退到指定版本,工作区内容不变
- –soft:表示将版本库内容回退到指定版本,工作区以及暂存区内容不变
- –hard:表示将版本库、工作区、暂存区都回退到指定版本
- HEAD:表示回退到当前版本,HEAD^、HEAD~1表示上一个版本,以此类推
现在我们想回退到v2版本,因此需要使用–hard参数
[root@VM-12-4-opencloudos gitcode]# git log --pretty=oneline
f4b771d0709db09cc5b5eb4e62335a755154209e (HEAD -> master) update readme v3
3662918784e02c58608b814ecba90e53292e581d update readme v2
689991ba08bd08804de26123c388b9b0d666e4f0 add readme
c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 add readme file
[root@VM-12-4-opencloudos gitcode]# git reset --hard 3662918784e02c58608b814ecba90e53292e581d
HEAD is now at 3662918 update readme v2
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git log --pretty=oneline
3662918784e02c58608b814ecba90e53292e581d (HEAD -> master) update readme v2
689991ba08bd08804de26123c388b9b0d666e4f0 add readme
c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 add readme file
但是现在我又后悔了,想回到v3版本应该怎么办?此时git log无法查看v3的提交id,还好Git还提供了一个命令git reflog
进行补救:
[root@VM-12-4-opencloudos gitcode]# git reflog
3662918 (HEAD -> master) HEAD@{0}: reset: moving to 3662918784e02c58608b814ecba90e53292e581d
f4b771d HEAD@{1}: commit: update readme v3
3662918 (HEAD -> master) HEAD@{2}: commit: update readme v2
689991b HEAD@{3}: commit: add readme
c85412b HEAD@{4}: commit (initial): add readme file
[root@VM-12-4-opencloudos gitcode]# git reset --hard f4b771d
HEAD is now at f4b771d update readme v3
[root@VM-12-4-opencloudos gitcode]# git log --pretty=oneline
f4b771d0709db09cc5b5eb4e62335a755154209e (HEAD -> master) update readme v3
3662918784e02c58608b814ecba90e53292e581d update readme v2
689991ba08bd08804de26123c388b9b0d666e4f0 add readme
c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 add readme file
3.2.4 撤销修改命令
Case1:对于工作区中的代码还没有进行add操作
命令:git checkout -- [file]
,可以将工作区的代码回到最近一次add或者commit的状态
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# 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: readmeno changes added to commit (use "git add" and/or "git commit -a")
[root@VM-12-4-opencloudos gitcode]# git checkout -- readme
[root@VM-12-4-opencloudos gitcode]# git status
On branch master
nothing to commit, working tree clean
Case2:对于工作区中的代码已经进行add操作,但是还没有commit
方式一:使用回退命令:git reset --hard HEAD
直接回退到当前版本
方式二:使用命令:git reset --mixed HEAD
此时就回到了Case1的情况,然后继续执行git checkout – [file]命令
Case3:已经commit提交
使用回退命令:git reset --hard HEAD^
直接回退到上一版本
3.2.5 删除命令
命令:git rm [file]
- git rm [file]相当于rm [file] + git add file1的结合体
[root@VM-12-4-opencloudos gitcode]# git commit -m "add file1"
[master e907760] add file11 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 file1
[root@VM-12-4-opencloudos gitcode]# git status
On branch master
nothing to commit, working tree clean
[root@VM-12-4-opencloudos gitcode]# git rm file1
rm 'file1'
[root@VM-12-4-opencloudos gitcode]# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: file1[root@VM-12-4-opencloudos gitcode]# git commit -m "remove file1"
[master 6e35bc4] remove file11 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file1
3.3 Day03
3.3.1 git branch 命令
查看当前所有的分支:**git branch**
之前我们观察到的.git目录下HEAD指针其实是指向master分支
而master分支则对应指向该分支下的提交记录
[root@VM-12-4-opencloudos gitcode]# cat .git/HEAD
ref: refs/heads/master
[root@VM-12-4-opencloudos gitcode]# cat .git/refs/heads/master
6e35bc4cab6906b0cb074e2af434b6dca77bb71f
[root@VM-12-4-opencloudos gitcode]# git cat-file -p 6e35bc4cab6906b0cb074e2af434b6dca77bb71f
tree 6ef62311f0d21fa88145f6e5c8e70bd80cd861b8
parent e907760d8466f84b3d655b287df15bebc50f83a5
author ricejson <wjjbangbangbang@163.com> 1715595900 +0800
committer ricejson <wjjbangbangbang@163.com> 1715595900 +0800remove file1
新建分支:**git branch dev**
[root@VM-12-4-opencloudos gitcode]# git branch dev
[root@VM-12-4-opencloudos gitcode]# git branchdev
* master
[root@VM-12-4-opencloudos gitcode]# cat .git/HEAD
ref: refs/heads/master
[root@VM-12-4-opencloudos gitcode]# tree .git
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── push-to-checkout.sample
│ ├── sendemail-validate.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ ├── dev
│ └── master
├── objects
│ ├── 04
│ │ └── 5403d976be5fbc25a531b5c924e664b3221fc6
│ ├── 15
│ │ └── ca5b906102a278626835d25f3d740c71ec3374
│ ├── 22
│ │ └── d19a942e422df71b16f6169eb65c27894d5a97
│ ├── 36
│ │ └── 62918784e02c58608b814ecba90e53292e581d
│ ├── 68
│ │ └── 9991ba08bd08804de26123c388b9b0d666e4f0
│ ├── 6e
│ │ ├── 35bc4cab6906b0cb074e2af434b6dca77bb71f
│ │ ├── 84d6a5ed71a327ba3376cac9801558d9ea2e80
│ │ └── f62311f0d21fa88145f6e5c8e70bd80cd861b8
│ ├── 8c
│ │ └── 7ca049e4ed76db4c19a2391ef45b749f8ae8c4
│ ├── 8d
│ │ └── 0e41234f24b6da002d962a26c2495ea16a425f
│ ├── 8e
│ │ └── 3bde47132a2d53b6c01daf870702fba582aeff
│ ├── c8
│ │ └── 5412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826
│ ├── cd
│ │ └── 979bb355c3c301ed85b46c2b712e7a259625e0
│ ├── de
│ │ └── 55f88aa577ebe8b1294c26e0315470bd7bb8d4
│ ├── e5
│ │ └── c092dc5d9fcb8fdcd53fe30142957a503cc681
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│ ├── e9
│ │ └── 07760d8466f84b3d655b287df15bebc50f83a5
│ ├── eb
│ │ └── b76d2d63485465b349f945977bbed134ed0f04
│ ├── f4
│ │ └── b771d0709db09cc5b5eb4e62335a755154209e
│ ├── info
│ └── pack
├── ORIG_HEAD
└── refs├── heads│ ├── dev│ └── master└── tags29 directories, 45 files
3.3.2 git checkout 切换所在分支
切换当前分支:git checkout 分支名
创建分支同时切换所在分支:git checkout -b 分支名
3.3.3 git merge 合并分支
合并分支命令:git merge 分支名
[root@VM-12-4-opencloudos gitcode]# git branchdev
* master
[root@VM-12-4-opencloudos gitcode]# git checkout dev
Switched to branch 'dev'
[root@VM-12-4-opencloudos gitcode]# git branch
* devmaster
[root@VM-12-4-opencloudos gitcode]# ls
readme
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branch
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add merge branch content"
[dev febf6ee] add merge branch content1 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# git status
On branch dev
nothing to commit, working tree clean
[root@VM-12-4-opencloudos gitcode]# git checkout master
Switched to branch 'master'
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone line
[root@VM-12-4-opencloudos gitcode]# git merge dev
Updating 6e35bc4..febf6ee
Fast-forwardreadme | 2 ++1 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# git status
On branch master
nothing to commit, working tree clean
[root@VM-12-4-opencloudos gitcode]# git log --pretty=oneline
febf6ee072d3f48c6bc1e46a60cb2d650f3a575f (HEAD -> master, dev) add merge branch content
6e35bc4cab6906b0cb074e2af434b6dca77bb71f remove file1
e907760d8466f84b3d655b287df15bebc50f83a5 add file1
f4b771d0709db09cc5b5eb4e62335a755154209e update readme v3
3662918784e02c58608b814ecba90e53292e581d update readme v2
689991ba08bd08804de26123c388b9b0d666e4f0 add readme
c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 add readme file
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branch
3.3.4 git branch -d 删除分支
删除某个分支:git branch -d 分支名
**需要注意:**只有在不同的分支下才能删除别的分支,例如想要删除dev分支,则不能在dev分支下操作,而应该在master分支下进行
3.3.5 演示合并冲突
步骤一:新建一个dev1分支,将readme新增add dev1内容,并进行提交
步骤二:在master分支下将readme新增add master内容,并进行提交
步骤三:进行合并操作
[root@VM-12-4-opencloudos gitcode]# git branch dev1
[root@VM-12-4-opencloudos gitcode]# git branchdev1
* master
[root@VM-12-4-opencloudos gitcode]# git checkout dev1
Switched to branch 'dev1'
[root@VM-12-4-opencloudos gitcode]# git branch
* dev1master
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd dev1
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add dev1"
[dev1 f1c4920] add dev11 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# git checkout master
Switched to branch 'master'
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branch
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add master"
[master 6eb52f9] add master1 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# git log --pretty=oneline
6eb52f96f549ad74d35813dc8567797019660735 (HEAD -> master) add master
febf6ee072d3f48c6bc1e46a60cb2d650f3a575f add merge branch content
6e35bc4cab6906b0cb074e2af434b6dca77bb71f remove file1
e907760d8466f84b3d655b287df15bebc50f83a5 add file1
f4b771d0709db09cc5b5eb4e62335a755154209e update readme v3
3662918784e02c58608b814ecba90e53292e581d update readme v2
689991ba08bd08804de26123c388b9b0d666e4f0 add readme
c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 add readme file
[root@VM-12-4-opencloudos gitcode]# git merge dev1
Auto-merging readme
CONFLICT (content): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branch<<<<<<< HEAD
add master
=======
add dev1
>>>>>>> dev1
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add master"
[master 4717bf6] add master
[root@VM-12-4-opencloudos gitcode]# git merge dev1
Already up to date.
**解决方法:**当出现冲突之后,打开readme文件已经为我们展示了冲突区域,只需要选择最后修改保留的结果,删除冲突部分即可,最后记得要进行提交!!!然后再合并就OK了
3.4 Day04
分支策略:
- 线上环境运行的都是master分支下的代码,因此master要保证稳定性
- 而其余分支例如dev就是开发人员开发的分支,是没有经过测试的因此会出现bug等不稳定因素
3.4.1 bug分支
现在有这样一种情况:master分支下又新建了一个dev分支开发别的业务,但是此时突然发现master分支代码存在bug,因此需要修复
注意:不能在master分支下直接修复bug!因为线上环境一定要保证稳定
最佳实践:
- 步骤一:先在reslove_bug分支下修复正确代码,然后进行提交并合并到master分支下
- 步骤二:在dev分支下,将master分支合并到dev分支下,然后解决冲突再进行提交
- 步骤三:最后将dev分支合并到master分支下
[root@VM-12-4-opencloudos gitcode]# git checkout -b dev
Switched to a new branch 'dev'
[root@VM-12-4-opencloudos gitcode]# git branch
* devmaster
[root@VM-12-4-opencloudos gitcode]# ls
readme
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add coding"
[dev e6d2c36] add coding1 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# git checkout master
Switched to branch 'master'
[root@VM-12-4-opencloudos gitcode]# ls
readme
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd master
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git checkout -b reslove_bug
Switched to a new branch 'reslove_bug'
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add reslove bug"
[reslove_bug 267809e] add reslove bug1 file changed, 1 insertion(+), 1 deletion(-)
[root@VM-12-4-opencloudos gitcode]# git checkout master
Switched to branch 'master'
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd master
[root@VM-12-4-opencloudos gitcode]# git merge reslove_bug
Updating 4717bf6..267809e
Fast-forwardreadme | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd master reslove bug
[root@VM-12-4-opencloudos gitcode]# git checkout dev
Switched to branch 'dev'
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd masteri am coding...
[root@VM-12-4-opencloudos gitcode]# git merge master
Auto-merging readme
CONFLICT (content): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "merge reslove bug to dev"
[dev 95fff14] merge reslove bug to dev
[root@VM-12-4-opencloudos gitcode]# git merge -m "merge master to dev" master
Already up to date.
[root@VM-12-4-opencloudos gitcode]# git checkout master
Switched to branch 'master'
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd master reslove bug
[root@VM-12-4-opencloudos gitcode]# git merge dev -m "merge dev to master"
Updating 267809e..95fff14
Fast-forward (no commit created; -m option ignored)readme | 2 ++1 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd master reslove bugi am coding...
[root@VM-12-4-opencloudos gitcode]# git log --graph --pretty=oneline
* 95fff140599ed8d3ef010f5e8ad280570f46b1a7 (HEAD -> master, dev) merge reslove bug to dev
|\
| * 267809ed8ec8759bea2ac269ed4b1217081beba7 (reslove_bug) add reslove bug
* | e6d2c366dfae0c939715c5ae52abe70d894923bd add coding
|/
* 4717bf64651c6f05c64a5a136a72814305365b75 add master
|\
| * f1c4920a23ab59a6eedd692defa071e66ed623d6 add dev1
* | 6eb52f96f549ad74d35813dc8567797019660735 add master
|/
* febf6ee072d3f48c6bc1e46a60cb2d650f3a575f add merge branch content
* 6e35bc4cab6906b0cb074e2af434b6dca77bb71f remove file1
* e907760d8466f84b3d655b287df15bebc50f83a5 add file1
* f4b771d0709db09cc5b5eb4e62335a755154209e update readme v3
* 3662918784e02c58608b814ecba90e53292e581d update readme v2
* 689991ba08bd08804de26123c388b9b0d666e4f0 add readme
* c85412bc2d6e2e7b1c94b1c9f3b36c44fd6a4826 add readme file
[root@VM-12-4-opencloudos gitcode]#
3.4.2 强制删除分支
前面我们已经使用过git branch -d 分支名
进行删除分支,
但是这种情况下我们只能够删除已经合并过后的分支,但是如果该分支我们没有进行合并操作,那就必须使用强制删除:git branch -D 分支名
[root@VM-12-4-opencloudos gitcode]# git branch dev1
[root@VM-12-4-opencloudos gitcode]# git checkout dev1
Switched to branch 'dev1'
[root@VM-12-4-opencloudos gitcode]# vim readme
[root@VM-12-4-opencloudos gitcode]# git add .
[root@VM-12-4-opencloudos gitcode]# git commit -m "add coding again"
[dev1 2428a7d] add coding again1 file changed, 2 insertions(+)
[root@VM-12-4-opencloudos gitcode]# cat readme
hello git
hello world!ricejsonone linedev branchadd master reslove bugi am coding...i am coding again
[root@VM-12-4-opencloudos gitcode]# git checkout master
Switched to branch 'master'
[root@VM-12-4-opencloudos gitcode]# git branch -d dev1
error: The branch 'dev1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev1'.
[root@VM-12-4-opencloudos gitcode]# git branch -D dev1
Deleted branch dev1 (was 2428a7d).
[root@VM-12-4-opencloudos gitcode]# git branch
* master
3.5 Day05
3.5.1 克隆远程仓库
- 首先需要查看本地主目录下有无/root/.ssh/id_rsa以及.ssh/id_rsa.pub这两个秘钥
- 【上述步骤没有则需要执行】ssh-keygen -t rsa -C "wjjbangbangbang@163.com"生成密钥
- 将id_rsa.pub中的公钥内容复制到gitee平台配置中
- 然后使用SSH协议执行git clone命令克隆仓库
[root@VM-12-4-opencloudos home]# git clone git@gitee.com:eat-rice-well/remote-gitcode.git
Cloning into 'remote-gitcode'...
The authenticity of host 'gitee.com (180.76.198.77)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (7/7), done.
[root@VM-12-4-opencloudos home]# ls
gitcode lighthouse remote-gitcode springboot www
[root@VM-12-4-opencloudos home]# cd remote-gitcode
[root@VM-12-4-opencloudos remote-gitcode]# ls
README.en.md README.md
[root@VM-12-4-opencloudos remote-gitcode]# git config -l
user.name=ricejson
user.email=wjjbangbangbang@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:eat-rice-well/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
[root@VM-12-4-opencloudos remote-gitcode]# git config --unset user.name
[root@VM-12-4-opencloudos remote-gitcode]# git config -l
user.name=ricejson
user.email=wjjbangbangbang@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:eat-rice-well/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
[root@VM-12-4-opencloudos remote-gitcode]# git config --global user.name "米饭好好吃"
[root@VM-12-4-opencloudos remote-gitcode]# git config -l
user.name=米饭好好吃
user.email=wjjbangbangbang@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:eat-rice-well/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
3.5.2 推送到远程仓库
命令:git push origin(远程仓库名) master(本地分支):master(远程分支)
如果本地分支和远程分支一致,可以简写为git push origin master
[root@VM-12-4-opencloudos remote-gitcode]# ls
file.txt README.en.md README.md
[root@VM-12-4-opencloudos remote-gitcode]# vim file.txt
[root@VM-12-4-opencloudos remote-gitcode]# git add .
[root@VM-12-4-opencloudos remote-gitcode]# git commit -m "add file.txt"
[master 07ad17b] add file.txt1 file changed, 1 insertion(+)create mode 100644 file.txt
[root@VM-12-4-opencloudos remote-gitcode]# git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits)nothing to commit, working tree clean
[root@VM-12-4-opencloudos remote-gitcode]# git push origin master:master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 293.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:eat-rice-well/remote-gitcode.gite84d96b..07ad17b master -> master
3.5.3 从远程仓库进行拉取
命令:git pull origin(远程仓库名) master(远程分支名):master(本地分支名)
注意:
- 如果本地分支和远程分支一致,可以简写为
git pull origin master
- pull命令执行了两件事,一件是clone代码,另一件就是进行merge合并
[root@VM-12-4-opencloudos remote-gitcode]# git pull origin master:master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 975 bytes | 975.00 KiB/s, done.
From gitee.com:eat-rice-well/remote-gitcode07ad17b..ae0b3ba master -> master07ad17b..ae0b3ba master -> origin/master
warning: fetch updated the current branch head.
fast-forwarding your working tree from
commit 07ad17b0133c2b604511760c131bc166415d1496.
Already up to date.
[root@VM-12-4-opencloudos remote-gitcode]# vim file.txt
[root@VM-12-4-opencloudos remote-gitcode]# cat file.txt
hello git
hello world
[root@VM-12-4-opencloudos remote-gitcode]#
3.5.4 .gitignore忽略文件
使用方法:
- 可以使用*.yml忽略所有的.yml格式结尾的文件
- 可以使用文件名忽略文件
- 可以使用!表示不忽略该文件
- 可以使用git add -f 强制添加(可以无视忽略规则)
[root@VM-12-4-opencloudos remote-gitcode]# touch .gitignore
[root@VM-12-4-opencloudos remote-gitcode]# vim .gitignore
[root@VM-12-4-opencloudos remote-gitcode]# cat .gitignore
# 配置忽略文件*.yml!test.yml
[root@VM-12-4-opencloudos remote-gitcode]# touch application.yml
[root@VM-12-4-opencloudos remote-gitcode]# git status
On branch master
Your branch is up to date with 'origin/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: file.txtUntracked files:(use "git add <file>..." to include in what will be committed).gitignoreno changes added to commit (use "git add" and/or "git commit -a")
[root@VM-12-4-opencloudos remote-gitcode]# touch test.yml
[root@VM-12-4-opencloudos remote-gitcode]# git status
On branch master
Your branch is up to date with 'origin/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: file.txtUntracked files:(use "git add <file>..." to include in what will be committed).gitignoretest.ymlno changes added to commit (use "git add" and/or "git commit -a")
如果要查看为什么某个文件被忽略,也可以使用git check-ignore application.yml
[root@VM-12-4-opencloudos remote-gitcode]# git check-ignore -v application.yml
.gitignore:3:*.yml application.yml
3.5.5 给命令配置别名
命令:git config --global alias.st status
则我们的git status就相当于git st
对于一些长命令,例如:git log --pretty=onelone --abbrev-commit
也可以配置成git lpa
3.3.6 标签管理
命令:
- 查看所有标签:
git tag
- 新增标签:
git tag 标签名 commit的id(默认为最新提交id)
- 删除标签:
git tag -d 标签名
- 给标签生成描述信息:
git tag -a 标签名 -m "描述信息" commit_id
- 展示标签描述信息:
git show 标签名
[root@VM-12-4-opencloudos remote-gitcode]# git tag
[root@VM-12-4-opencloudos remote-gitcode]# git tag v1.0
[root@VM-12-4-opencloudos remote-gitcode]# git tag
v1.0
[root@VM-12-4-opencloudos remote-gitcode]# git log --pretty=oneline --abbrev-commit
13d93bf (HEAD -> master, tag: v1.0) add test .gitignore file
ae0b3ba (origin/master, origin/HEAD) update file.txt.
07ad17b add file.txt
e84d96b Initial commit
[root@VM-12-4-opencloudos remote-gitcode]# git tag -a v0.9 -m "important tag" 07ad17b
[root@VM-12-4-opencloudos remote-gitcode]# git tag
v0.9
v1.0
[root@VM-12-4-opencloudos remote-gitcode]# git show 07ad17b
commit 07ad17b0133c2b604511760c131bc166415d1496 (tag: v0.9)
Author: 米饭好好吃 <wjjbangbangbang@163.com>
Date: Thu May 16 14:58:10 2024 +0800add file.txtdiff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..8d0e412
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+hello git
[root@VM-12-4-opencloudos remote-gitcode]#
- 将标签推送到远程仓库:
git push 指定标签名(--tags表示全部标签)
[root@VM-12-4-opencloudos remote-gitcode]# git push --tags origin master
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 649 bytes | 649.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:eat-rice-well/remote-gitcode.gitae0b3ba..13d93bf master -> master* [new tag] v0.9 -> v0.9* [new tag] v1.0 -> v1.0
- 推送移除标签:
git push origin :v1.0
[root@VM-12-4-opencloudos remote-gitcode]# git push origin :v1.0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:eat-rice-well/remote-gitcode.git- [deleted] v1.0
[root@VM-12-4-opencloudos remote-gitcode]#
3.6 Day06—多人协作开发实战
查看远程分支和本地分支:git branch -a
查看远程分支:git branch -r
新建并切换分支并与远程分支建立链接:git checkout -b dev origin/dev
目标:在本地仓库中file.txt文件新增aaa以及bbb内容
环境:存在开发者1以及开发者2,要求开发者1完成aaa,开发者2完成bbb
条件:两个开发者基于同一个分支进行开发
- step1:在gitee平台新建分支dev用于开发
- step2:在开发者1平台下进行git pull拉取最新代码、在开发者2平台下进行git pull拉取最新代码
- step3:在开发者1平台下使用
git checkout -b dev origin/dev
新建并切换分支,并与远程dev分支建立链接 - step4:在dev分支下修改aaa代码并add、commit并push到远程dev分支下
- step5:在开发者2平台下进行
git checkout -b dev origin/dev
新建并切换分支,并与远程dev分支建立链接 - step6:在dev分支下修改bbb代码并add、commit并push到远程dev分支下,此时报错,因为出现合并冲突
- step7:在开发者2中将远程dev分支下内容进行pull到dev下进行解决冲突,然后重新add、commit、push操作
- step8:dev分支合并到master下
- 方式一:使用提交PR单,交由审核人员进行审核
- 方式二:在本地操作,首先进行git pull拉取最新代码,然后将master分支合并到dev下解决冲突,在提交到本地master分支,然后再由master进行push
- step9:最后dev分支开发完毕后删除即可