当前位置: 首页 > news >正文

陕西省城乡建设学校网站新余建站公司

陕西省城乡建设学校网站,新余建站公司,网站保姆-源码下载,连云港市网站建设上一篇#xff1a;01【Git的基本命令、底层命令、命令原理】 下一篇#xff1a;03【Git的协同开发、TortoiseGit、IDEA的操作Git】 文章目录 02【Git分支的使用、Git回退、还原】一、分支1.1 分支概述1.1.1 Git分支简介1.1.2 Git分支原理 1.2 创建分支1.2.1 创建普通分支1.…上一篇01【Git的基本命令、底层命令、命令原理】 下一篇03【Git的协同开发、TortoiseGit、IDEA的操作Git】 文章目录 02【Git分支的使用、Git回退、还原】一、分支1.1 分支概述1.1.1 Git分支简介1.1.2 Git分支原理 1.2 创建分支1.2.1 创建普通分支1.2.2 指定提交对象创建分支 1.3 切换分支1.3.1 checkout分支切换1.3.2 checkout原理1 影响工作空间2 影响暂存区 1.3.3 switch切换分支 1.4 分支合并1.4.1 快进合并1.4.2 典型合并 1.5 Git的代码冲突1.5.1 Git的代码冲突的分类与特点1.5.2 快进合并代码冲突1.5.3 典型合并代码冲突 1.6 Git的分支状态存储1.6.1 Git存储引入1.6.2 Git存储的使用1 使用存储状态2 读取存储状态3 存储状态的删除 1.6.3 Git存储的原理1 使用存储状态的原理2 读取存储状态的原理3 删除存储状态的原理 1.7 Git打标签1.7.1 标签的语法与介绍1.7.2 标签的使用 二、Git数据恢复与还原2.1 Git的还原2.1.1 还原工作空间2.1.2 还原暂存区2.1.3 还原提交对象1 提交日志修正2 提交内容修正3 提交文件修正 2.2 Git的数据回退2.2.1 回退HEA指针2.2.2 回退暂存区2.2.3 回退工作空间 02【Git分支的使用、Git回退、还原】 一、分支 1.1 分支概述 1.1.1 Git分支简介 Git的分支概念与SVN的分支概念完全不同在SVN中分支更倾向于是一个文件夹建立分支也只是建立一个新的文件夹利用分支管理项目其实本质上是为了使得项目的结构更加清晰。当然SVN的分支也提供合并、回退等功能但相对于Git过于笨重。 在Git中使用分支的主要目的是为了合并分支基于分支来开发项目并不会影响主线开发当其他分支的代码确认无误需要集成到主线分支Master分支时我们需要进行分支的合并即可即将主线分支合并到其他分支中这样一个完整的功能就集成到主线代码中了 Git分支如图所示 1.1.2 Git分支原理 在很多版本控制系统中创建分支是一个略微低效的过程——常常需要完全创建一个源代码目录的副本。对于大项目来说这样的过程会耗费很多时间例如SVN正是这样的操作。显然Git并没有采用这种笨重的方式来管理分支。 在Git中分支其实本质上仅仅是指向Commit对象的可变指针默认情况下所有仓库都会有一个默认的分支名为master。在.git\refs\heads目录存储着当前仓库的所有分支。每一个分支都有一个以分支名命名的文件该文件存储着当前分支指向的提交对象的hash值 【初始化仓库】 rm -rf .git ./* git init echo 111 aaa.txt git add ./ git commit -m 111 ./【查看分支】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline ceca35b (HEAD - master) 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat .git/refs/heads/master ceca35b10c0495e852cbf26205fb5a5af409b70eAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git cat-file -p ceca35b tree 8f96f2f60c766a6a6b78591e06e6c1529c0ad9af author xiaohui xiaohuialiyun.com 1697097246 0800 committer xiaohui xiaohuialiyun.com 1697097246 0800111【继续开发查看master分支变化】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ echo 222 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m 222 ./ warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directory warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directory [master 69372ca] 2221 file changed, 1 insertion(), 1 deletion(-)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 69372ca (HEAD - master) 222 ceca35b 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat .git/refs/heads/master 69372ca265e6a98c8a7f839b8760f98b80bbf4fe我们可以看到在Git中分支仅仅是一个指向Commit对象的一个可变指针当执行commit提交后当前分支会指向最新的commit对象的hash值这样通过提交对象的hash值来找到对应版本的内容而不需要像其他版本控制工具那样将整个文件夹进行拷贝。 因此Git的分支相对于其他版本控制工具显得极其轻量级操作速度也是其他版本控制工具不可比拟的。 在Git中每一个分支都是保存一个提交对象的hash值 1.2 创建分支 1.2.1 创建普通分支 语法 git branch {branchName} [commitHash]创建一个分支默认情况下指向最新的commit对象 【初始化项目】 rm -rf .git ./* git init echo 111 aaa.txt git add ./ git commit -m 111 ./ echo 222 aaa.txt git commit -m 222 ./ echo 333 aaa.txt git commit -m 333 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 6f02325 (HEAD - master) 333 7ded3d1 222 a9e7314 111HEAD是一个活动指针指向的是正在活跃的分支。 【在当前HEAD指针指向的位置创建分支】 git branch b1查看.git\refs\heads目录 【继续使用master分支开发】 echo 444 aaa.txt git commit -m 444 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 2920850 (HEAD - master) 444 57a8f94 (b1) 333 3ac4fa8 222 afb705d 1111.2.2 指定提交对象创建分支 【指定提交对象来创建分支】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git branch b2 3ac4fa8 # 根据指定的提交对象来创建分支AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 2920850 (HEAD - master) 444 57a8f94 (b1) 333 3ac4fa8 (b2) 222 afb705d 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat .git/refs/heads/b2 3ac4fa83b8c224518758b2b4ef7d2f214ac0224a1.3 切换分支 1.3.1 checkout分支切换 创建好了分支之后我们可以使用git checkout切换到其他分支进行开发 语法 git checkout {branchName}【准备环境】 rm -rf .git ./* git init echo 111 aaa.txt git add ./ git commit -m 111 ./ echo 222 aaa.txt git commit -m 222 ./ echo 333 aaa.txt git commit -m 333 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 2130f83 (HEAD - master) 333 86cbe2b 222 dad9d1a 111【创建分支】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 在当前位置创建b1分支 $ git branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 指定提交对象来创建分支 $ git branch b2 86cbe2bAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 2130f83 (HEAD - master, b1) 333 # b1分支的位置 86cbe2b (b2) 222 # b2分支的位置 dad9d1a 111【切换到b1分支进行开发】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 切换到b1分支 $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # HEAD指针指向了b1分支 $ git log --oneline 2130f83 (HEAD - b1, master) 333 86cbe2b (b2) 222 dad9d1a 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 编辑文件 $ echo 444 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 提交 $ git commit -m b1 444 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看文件内容 $ cat aaa.txt 111 222 333 444AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看日志 $ git log --oneline 51b41c0 (HEAD - b1) b1 444 # b1分支正在开发 2130f83 (master) 333 # master和b2分支留在原地 86cbe2b (b2) 222 dad9d1a 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 切换到master分支 $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看日志 $ git log --oneline 2130f83 (HEAD - master) 333 # HEAD指针指向了master分支 86cbe2b (b2) 222 dad9d1a 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看master分支工作状态的文件内容(文件内容是旧版本的) $ cat aaa.txt 111 222 333【查看所有版本情况】 默认情况下使用git log查询日志只能查询到之前的版本的日志无法查询到比自身分支版本还要新的版本通过--all可以查询所有版本的提交日志 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline # 只能查询master分支之前版本的日志信息 2130f83 (HEAD - master) 333 86cbe2b (b2) 222 dad9d1a 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查询所有日志信息 $ git log --oneline --all 51b41c0 (b1) b1 444 2130f83 (HEAD - master) 333 86cbe2b (b2) 222 dad9d1a 1111.3.2 checkout原理 通常情况下当前分支如果存在未提交的操作时则无法切换到其他分支所以我们切换分支时最好保证当前工作空间的状态为nothing to commit即所有操作均已提交 在切换分支时Git会对以下地方进行修改 1HEAD指针将HEAD指针指向最新的分支2工作空间的内容将当前工作空间的内容更新为之前的分支的内容 【代码示例】 rm -rf .git ./* git init echo 111 aaa.txt git add ./ git commit -m 111 ./ git branch b1 echo 222 aaa.txt git add ./ git commit -m 222 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git log --oneline --all 464d580 (HEAD - master) 222 cc3429a (b1) 111【操作master分支的文件但不提交切换到b1分支发现切换失败】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 编辑文件 $ echo 333 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (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: aaa.txtno changes added to commit (use git add and/or git commit -a)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 error: Your local changes to the following files would be overwritten by checkout:aaa.txt Please commit your changes or stash them before you switch branches. Aborting可以看出如果当前分支存在未提交的操作时Git不允许切换分支。但是有2种情况是例外的 1新分支的情况当该分支是一个新创建的分支时当前分支存在未提交的操作依旧可以切换到新分支并且会将未提交的操作影响到新分支2新文件的情况当操作的文件是一个新的文件时当前分支未提交依旧是可以切换到其他分支并将操作影响到其他分支 如果存在以上两种行为进行切换分支则切换分支时不仅会对HEAD指针、工作空间等地方修改还会对如下地方进行修改 1工作空间的状态将当前分支的工作空间状态更新为之前分支2暂存区的内容将当前暂存区内容更新为之前的分支 Tips我们在切换分支时要保证当前分支是提交的状态否则会对其他分支造成不必要的影响 1 影响工作空间 新分支、新文件的操作都会影响工作空间和暂存区 【演示新分支影响工作空间】 初始化仓库 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ git branch b1 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # b1分支创建后整个工作空间还没有提交过任何一次文件属于新分支 $ git log --oneline --all 4b3c6bc (HEAD - master, b1) 111 # 指针还是指向master分支的在master分支编辑文件然后切换到b1分支查看工作空间的内容发现变更了影响了b1分支的工作空间内容 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 编辑master分支的文件内容 $ echo 222 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 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: aaa.txtno changes added to commit (use git add and/or git commit -a)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支发现能够切换成功 $ git checkout b1 Switched to branch b1 M aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1分支的文件内容 $ cat aaa.txt 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1分支的状态 $ git status On branch b1 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: aaa.txtno changes added to commit (use git add and/or git commit -a)可以看到在遇到新分支时当前工作空间的操作即使未提交也可以切换到其他分支而且影响其他分支的工作空间状态及工作空间内容注意此时将master未提交的数据也影响到了b1分支当分支切换到master然后提交b1分支被影响的内容就消失了 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # 切换到master分支 $ git checkout master Switched to branch master M aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 提交操作 $ git commit -m 222 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支 $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # 查看内容发现影响消失了 $ cat aaa.txt 111重新切换到master分支编辑文件未提交切换到b1分支失败此时b1分支不属于新分支了 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 333 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支失败此时b1分支不属于新分支了 $ git checkout b1 error: Your local changes to the following files would be overwritten by checkout:aaa.txt Please commit your changes or stash them before you switch branches. Aborting【演示新文件影响工作空间】 把之前编辑的内容删除将工作空间恢复到干净状态 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 把之前编辑的内容删除 $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ cat aaa.txt 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 工作空间又回到了干净状态所有的操作均已提交 $ git status On branch master nothing to commit, working tree clean使用master创建一个新的文件切换到b1分支查看b1分支的工作空间内容发现也多了一个bbb.txt文件影响了b1分钟的工作空间 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 编辑一个新的文件 $ echo 333 bbb.txt # 注意此时创建了一个新的文件bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git status On branch master Untracked files:(use git add file... to include in what will be committed)bbb.txtnothing added to commit but untracked files present (use git add to track)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支发现切换成功 $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ ll total 2 -rw-r--r-- 1 Adminstrator 197121 5 Oct 23 09:23 aaa.txt -rw-r--r-- 1 Adminstrator 197121 4 Oct 23 09:23 bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1的工作空间多了一个文件 $ git status On branch b1 Untracked files:(use git add file... to include in what will be committed)bbb.txtnothing added to commit but untracked files present (use git add to track)切换到master分支提交操作然后切换回b1分支发现b1分支的bbb.txt文件清除了 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # 切换回master分支 $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git add ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 提交操作 $ git commit -m 333-bbb ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支 $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git status On branch b1 nothing to commit, working tree cleanAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1工作空间的bbb.txt文件清除了 $ ll total 1 -rw-r--r-- 1 Adminstrator 197121 5 Oct 23 09:26 aaa.txt2 影响暂存区 【演示新分支影响暂存区】 初始化仓库 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ git branch b1 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # b1分支创建后整个工作空间还没有提交过任何一次文件属于新分支 $ git log --oneline --all 19fd84b (HEAD - master, b1) 111 # 指针还是指向master分支的在master分支编辑文件然后添加到暂存区切换到b1分支查看b1分支的暂存区内容发现变更了影响了b1分支的暂存区 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 222 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git add ./ warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directoryAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 Switched to branch b1 M aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git ls-files -s 100644 a30a52a3be2c12cbc448a5c9be960577d13f4755 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git cat-file -p a30a52a3 111 222重新切换回master提交之前的操作然后再切换回b1分支查看暂存区内容发现影响消除了此时b1不再属于新分支了 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git checkout master Switched to branch master M aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git commit -m 222 ./ warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directory [master 2ae7cda] 2221 file changed, 1 insertion()AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git cat-file -p 58c9bdf9 111切换回master分支编辑文件还没有提交切换到b1分钟发现出错此时b1分支不属于新分支了 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 333 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git add ./ warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directoryAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 error: Your local changes to the following files would be overwritten by checkout:aaa.txt Please commit your changes or stash them before you switch branches. Aborting【演示新文件影响工作空间】 提交之前的操作将工作空间恢复到干净状态 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git commit -m 333 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git log --oneline --all f5d54ea (HEAD - master) 333 2ae7cda 222 e9b98e3 (b1) 111创建一个新文件添加到暂存区然后切换到b1分支发现b1分支的暂存区也多了一个文件影响了b1分支的暂存区 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 444-bbb bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git add ./ warning: LF will be replaced by CRLF in bbb.txt. The file will have its original line endings in your working directoryAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 Switched to branch b1 A bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1分支的暂存区也多了一个文件 $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt 100644 a864cef2a0696aabcc85e6e55f04dc12230bab84 0 bbb.txt切换回master提交之前的操作然后再切换回b1分支查询b1分支的暂存区内容被消除了消除影响 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git checkout master Switched to branch master A bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git commit -m 444-bbb ./ warning: LF will be replaced by CRLF in bbb.txt. The file will have its original line endings in your working directory [master d76cc6f] 444-bbb1 file changed, 1 insertion()create mode 100644 bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt此时bbb.txt文件已经不属于新文件了回到master分支编辑bbb.txt文件未提交然后切换到b1分支发现切换不了 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 555-bbb bbb.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git add ./ warning: LF will be replaced by CRLF in bbb.txt. The file will have its original line endings in your working directoryAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout b1 error: Your local changes to the following files would be overwritten by checkout:bbb.txt Please commit your changes or stash them before you switch branches. Aborting1.3.3 switch切换分支 git switch是Git 2.23版本推出的一个新的命令专门用于分支的切换而git checkout除了做分支的切换还有一些其他的功能如恢复文件恢复暂存区等 初始化仓库 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./使用git checkout恢复文件 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 222 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (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: aaa.txtno changes added to commit (use git add and/or git commit -a)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout aaa.txt Updated 1 path from the indexAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git status On branch master nothing to commit, working tree clean使用git checkout恢复暂存区 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ echo 222 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 添加到暂存区 $ git add ./ warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directoryAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git status On branch master Changes to be committed:(use git restore --staged file... to unstage)modified: aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git reset aaa.txt Unstaged changes after reset: M aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) $ git checkout aaa.txt Updated 1 path from the indexAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 成功恢复 $ git status On branch master nothing to commit, working tree clean但git switch命令是专门用于切换分支的并且git switch切换分支也会存在我们探究的新分支、新文件切换时所带来的影响问题 使用git switch命令切换分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 ((89dda87...)) $ git log --oneline --all 033f6b5 (master) 222 89dda87 (HEAD) 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 ((89dda87...)) $ git branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 ((89dda87...)) $ git switch b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) $ git log --oneline --all 033f6b5 (master) 222 89dda87 (HEAD - b1) 1111.4 分支合并 建立好分支后我们可以使用分支进行开发功能开发功能趋于成熟稳定后我们可以将master分支与当前分支进行合并这样其他分支的功能就集成到主分支上了这也是当前主流的开发方式。 使用分支开发功能从角度上划分一般为同轴开发和非同轴开发。 分支合并语法 git merge {branchName}解释将branchName分支的代码合并到当前分支 1.4.1 快进合并 同轴开发的快进合并 同轴开发创建一个新分支使用新分支开发等待开发功能趋于成熟稳定后我们可以将master分支与新分支进行合并这样其他分支的功能就集成到主分支上了这也是企业里面经常用的一种开发方式 对于前面的版本想要合并后面的版本我们称为快进合并 在同轴开发中多个分支处于同一根开发轴上后面版本的代码包含前面版本的代码因此同轴开发一般只会存在快进合并 如图所示 首先创建一个新分支login分支切换到新分支在新分支开发上功能代码 【示例代码】 初始化项目 rm -rf .git ./* git init echo 用户名密码登录 login.txt git add ./ git commit -m 用户名密码登录功能完成 ./①创建login分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git branch loginAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all 2f5d87a (HEAD - master, login) 用户名密码登录功能完成②切换到login分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout login Switched to branch loginAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git log --oneline --all 2f5d87a (HEAD - login, master) 用户名密码登录功能完成③使用login分支开发集成QQ登录 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ echo 集成QQ登录 login.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git commit -m 集成QQ登录 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git log --oneline --all a879a6c (HEAD - login) 集成QQ登录 2f5d87a (master) 用户名密码登录功能完成④使用login分支开发集成微信登录 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ echo 集成微信登录 login.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git commit -m 集成微信登录 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git log --oneline --all c5a2584 (HEAD - login) 集成微信登录 a879a6c 集成QQ登录 2f5d87a (master) 用户名密码登录功能完成当login分支基于稳定后将功能集成到maste分支 如图所示 ⑤切换回master分支将login分支的代码合并到master分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat login.txt 用户名密码登录AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git merge login Updating 2f5d87a..c5a2584 Fast-forwardlogin.txt | 2 1 file changed, 2 insertions()AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat login.txt 用户名密码登录 集成QQ登录 集成微信登录将login分支的代码合并到master分支其实本质上就是让master也指向与login一样的提交对象 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # master与login分支指向的是同一个Commit对象 $ cat .git/refs/heads/login c5a258428f0bec398017159126f0b87b0d05a1e4 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat .git/refs/heads/master c5a258428f0bec398017159126f0b87b0d05a1e41.4.2 典型合并 非同轴开发的典型合并 非同轴开发创建一个新分支使用新分支开发与此同时master分支也在开发行功能等到新分支开发的功能完毕后将master分支与新分支进行合并将新分支集成到master分支中但是新分支在开发过程中master分支也进行了一部分开发。 在非同轴开发中后面的版本v3需要合并前面的版本v2这种合并方式称为典型合并由于典型合并存在于非同轴 如图所示 需要注意的是如果是同轴开发后面版本的内容肯定包含前面版本的内容因此同轴开发不存在典型合并只存在快进合并。另外非同轴开发也存在快进合并例如上图中的③步骤中前面的版本v2想要合并后面的版本v3这也是一种快进合并。 总结 同轴开发只存在快进合并非同轴开发 前面的版本合并后面的版本快进合并后面的版本合并前面的版本典型合并 Tips典型合并必定是非同轴快进合并可以是非同轴也可以是同轴。 【示例代码】 初始化项目 rm -rf .git ./* git init echo 用户名密码登录 project.txt git add ./ git commit -m 用户名密码登录功能完成 ./①创建login分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git branch loginAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看不同轴的日志需要加上--graph $ git log --oneline --all --graph * 7c0b2a3 (HEAD - master, login) 用户名密码登录功能完成②切换到login分支使用login分支进行开发 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout login Switched to branch loginAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ echo 集成QQ登录 project.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git commit -m 集成QQ登录 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git log --oneline --all --graph * 9c968e0 (HEAD - login) 集成QQ登录 * 7c0b2a3 (master) 用户名密码登录功能完成③切换回master分支添加注册功能 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看master分支的project.txt文件代码并没有合并过来 $ cat project.txt 用户名密码登录 添加头像上传功能AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看日志处于不同轴开发路径 $ git log --oneline --all --graph * fecdb41 (HEAD - master) 添加头像上传功能 | * 9c968e0 (login) 集成QQ登录 |/ * 7c0b2a3 用户名密码登录功能完成④将login分支的合并到master分支产生代码冲突 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git merge login Auto-merging project.txt CONFLICT (content): Merge conflict in project.txt Automatic merge failed; fix conflicts and then commit the result.AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ cat project.txt 用户名密码登录HEAD 添加头像上传功能集成QQ登录login解决代码冲突 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ cat project.txt 用户名密码登录HEAD 添加头像上传功能集成QQ登录loginAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ vi project.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ cat project.txt 用户名密码登录 添加头像上传功能 集成QQ登录AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ git commit -a -m 合并login分支并解决代码冲突 [master 9ccdde2] 合并login分支并解决代码冲突AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * 9ccdde2 (HEAD - master) 合并login分支并解决代码冲突 # 解决冲突后提交产生一次新的版本 |\ | * 9c968e0 (login) 集成QQ登录 * | fecdb41 添加头像上传功能 |/ * 7c0b2a3 用户名密码登录功能完成经过上一次合并后master与login属于同轴了当切换为login分支可以使用快进合并来合并master分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 切换为login分支 $ git checkout login Switched to branch loginAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) # login分支的代码 $ cat project.txt 用户名密码登录 集成QQ登录AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git log --oneline --all --graph * 9ccdde2 (master) 合并login分支并解决代码冲突 |\ | * 9c968e0 (HEAD - login) 集成QQ登录 * | fecdb41 添加头像上传功能 |/ * 7c0b2a3 用户名密码登录功能完成AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) # 合并master分支 $ git merge master Updating 9c968e0..9ccdde2 Fast-forwardproject.txt | 1 1 file changed, 1 insertion()AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) # 查看login分支的代码发现master的代码被合并过来了 $ cat project.txt 用户名密码登录 添加头像上传功能 集成QQ登录AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login) $ git log --oneline --all --graph * 9ccdde2 (HEAD - login, master) 合并login分支并解决代码冲突 |\ | * 9c968e0 集成QQ登录 * | fecdb41 添加头像上传功能 |/ * 7c0b2a3 用户名密码登录功能完成1.5 Git的代码冲突 1.5.1 Git的代码冲突的分类与特点 Git存在代码冲突的情况分为协同开发时的代码冲突与分支合并时的代码冲突其中分支合并时的代码冲突又分为快进合并代码冲突与典型合并代码冲突。 另外还需要注意的是Git与SVN的代码冲突逻辑不一致。 Tips我们本章主要讨论分支合并时的代码冲突。 在SVN中只要两个用户编辑的不是同一行则不会出现代码冲突。在Git中只要两个文件内容不一致在合并分支时必定会产生冲突无论两个文件是否编辑的是同一行。不过具体情况还得细分如下 快进合并两个分支属于不同轴开发前面版本合并后面版本时只要两个文件不一致合并时必定出现冲突不管两个分支编辑的是不是同一行。典型合并由于典型合并两个分支属于不同的开发轴而后面的分支想要合并前面的分支两个文件必定不一致因此典型合并必定出现代码冲突。 Tips同轴开发的快进合并是不会产生冲突的。 【SVN代码冲突模拟】 SVN的情况两个用户编辑的代码是同一行则出现冲突 xiaohuixiaolan创建abc.txt内容为111222执行add执行commit执行update修改内容为111aaa222执行commit修改内容为111bbb222执行update冲突 如果xiaolan用户最后更改的文件内容为 111 222bbb然后再执行update则不会出现代码冲突因为SVN判断代码冲突的代码单元为行即两个用户编辑的不是同一行就不会出现代码冲突。 1.5.2 快进合并代码冲突 快进合并属于前面的版本合并后面的版本需要注意的是前面的版本并非是旧版本后面的版本并非是新版本这主要取决于是否是同轴开发。对于同轴开发后面的版本自然是新版本其内容肯定包含前面版本的内容。但对于非同轴开发后面的版本内容大概率是不包含前面的版本内容的 在同轴开发情况下快进合并不会产生代码冲突但如果在非同轴开发情况下快进合并就会产生代码冲突了。我们观察下列操作 master分支test分支创建abc.txt内容为111222执行add执行commit创建分支 – 此时test分支的abc.txt的内容也是111、222修改内容为111aaa222执行commit切换到test分支修改内容为111222bbb执行commit – 此时test分支和master分支已经不同轴了切换回master分支合并test分支属于快进合并但会出现冲突 Tips注意上述操作在SVN中则不会认为是冲突因为SVN判断是否冲突的标准是操作同一个文件的同一行记录 快进合并冲突演示 【使用Git演示代码冲突】 ①初始化项目 rm -rf .git ./* git initAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git add ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m 111 222 ./②创建test分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git branch test③继续使用master分支开发 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * d40d605 (HEAD - master, test) 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111aaa 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m aaa ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * 09fe9cb (HEAD - master) aaa * d40d605 (test) 111 222④切换到test分支开发 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout test Switched to branch testAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) # 注意和master分支修改的文件内容不是同一行 $ cat aaa.txt 111 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git commit -m bbb ./ [test dfe1b42] bbb1 file changed, 1 insertion(), 1 deletion(-)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) # 查看日志test与master分支已经不同轴 $ git log --oneline --all --graph * dfe1b42 (HEAD - test) bbb | * 09fe9cb (master) aaa |/ * d40d605 111 222⑤切换回master分支合并test分支出现冲突 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) # 切换回master分支 $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 合并test分支属于快进合并但是会产生冲突 $ git merge test Auto-merging aaa.txt CONFLICT (content): Merge conflict in aaa.txt Automatic merge failed; fix conflicts and then commit the result.AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ cat aaa.txtHEAD 111aaa 222111 222bbbtestAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) $ cat aaa.txt 111aaa 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) # 解决冲突 $ git commit -a -m master合并test分支 [master adaec78] master合并test分支AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * adaec78 (HEAD - master) master合并test分支 |\ | * 7b14536 (test) bbb * | eeca4c9 aaa |/ * 5f41035 111 222需要注意的是此时切换回test分支合并master分支内容不会出现冲突属于快进合并但是内容不一致但不会出现冲突因为master分支的内容是刚刚解决了冲突之后用户确定了的内容。 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout test Switched to branch testAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * adaec78 (master) master合并test分支 |\ | * 7b14536 (HEAD - test) bbb * | eeca4c9 aaa |/ * 5f41035 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ cat aaa.txt 111 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git merge master Updating 7b14536..adaec78 Fast-forwardaaa.txt | 3 -1 file changed, 2 insertions(), 1 deletion(-)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ cat aaa.txt 111aaa 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * adaec78 (HEAD - test, master) master合并test分支 |\ | * 7b14536 () bbb * | eeca4c9 aaa |/ * 5f41035 111 2221.5.3 典型合并代码冲突 典型合并时的代码冲突就是我们之前遇到的那种情况典型合并属于不同轴的开发并且后面版本需要合并前面版本的情况由于不同轴因此后面版本的内容不一定包含前面版本的内容由于Git代码冲突的特点因此典型合并必定会出现代码冲突因为后面版本的内容不可能和前面版本内容一致。 我们观察下列操作 master分支test分支创建abc.txt内容为111222执行add执行commit创建分支 – 此时test分支的abc.txt的内容也是111、222修改内容为111aaa222执行commit切换到test分支修改内容为111222bbb执行commit – 此时test分支和master分支已经不同轴了合并master分支的代码属于典型合并出现代码冲突。 典型合并冲突演示 【使用Git演示代码冲突】 ①初始化项目 rm -rf .git ./* git initAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git add ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m 111 222 ./②创建test分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git branch testAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * 45fa06b (HEAD - master, test) 111 222③继续使用master分支开发 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout master Already on masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111aaa 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m aaa ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * c86283b (HEAD - master) aaa * 45fa06b (test) 111 222④切换到test分支继续开发 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout test Switched to branch testAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ cat aaa.txt 111 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git commit -m bbb ./ [test e61df5a] bbb1 file changed, 1 insertion(), 1 deletion(-)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git log --oneline --all --graph * e61df5a (HEAD - test) bbb | * c86283b (master) aaa |/ * 45fa06b 111 222⑤使用test分支合并master分支属于典型合并 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git merge master Auto-merging aaa.txt CONFLICT (content): Merge conflict in aaa.txt Automatic merge failed; fix conflicts and then commit the result.AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING) $ cat aaa.txtHEAD 111 222bbb111aaa 222masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING) $ vi aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING) $ cat aaa.txt 111aaa 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING) $ git commit -a -m 合并master分支并解决冲突 [test 448d619] 合并master分支并解决冲突AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git log --oneline --all --graph * 448d619 (HEAD - test) 合并master分支并解决冲突 |\ | * c86283b (master) aaa * | e61df5a bbb |/ * 45fa06b 111 222同样的如果此时切换回master分支合并test分支虽然属于快进合并并且不同轴但合并依旧不会出现冲突因为test分支的内容是解决冲突并且经过用户确认之后的内容。 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git log --oneline --all --graph * 448d619 (HEAD - test) 合并master分支并解决冲突 |\ | * c86283b (master) aaa * | e61df5a bbb |/ * 45fa06b 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111aaa 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git merge test Updating c86283b..448d619 Fast-forwardaaa.txt | 2 -1 file changed, 1 insertion(), 1 deletion(-)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111aaa 222bbbAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all --graph * 448d619 (HEAD - master, test) 合并master分支并解决冲突 |\ | * c86283b aaa * | e61df5a bbb |/ * 45fa06b 111 2221.6 Git的分支状态存储 有时当你在项目的一部分上已经工作一段时间后所有东西都进入了混乱的状态而这时你想要切换到另一个分支做一点别的事情。 但你必须将当前工作空间所做的操作提交到版本库否则Git不允许切换分支但是当前的操作还不足以生成一次版本快照此时我们就可以使用Git的存储功能将当前工作状态存储起来然后再切换到其他分支工作最终工作完毕后切回当前分支从Git存储中取出之前的工作内容 1.6.1 Git存储引入 【初始化项目环境】 rm -rf ./* .git git init echo 111-master aaa.txt git add ./ git commit -m 111-master ./ git branch b1 git checkout b1 echo 111-b1 aaa.txt git add ./ git commit -m 111-b1 ./ echo 222-b1 aaa.txt git commit -m 222-b1 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看分支详情 $ git log --oneline --graph --all * 01ca592 (HEAD - b1) 222-b1 # b1的位置 * 1337456 111-b1 * f828bbd (master) 111-master # master的位置编辑文件 echo 333-b1 aaa.txt编辑完了后还不想提交此时接收到了新的临时任务想要切换到其他分支继续操作发现切换失败 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看git状态 $ git status On branch b1 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: aaa.txtno changes added to commit (use git add and/or git commit -a) # 有修改操作还未提交AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 切换到master失败 $ git checkout master error: Your local changes to the following files would be overwritten by checkout:aaa.txt Please commit your changes or stash them before you switch branches. Aborting1.6.2 Git存储的使用 1 使用存储状态 语法 git stash list # 查看当前Git中存储的所有状态 git stath # 将当前状态保存 git stash apply {stashName} # 根据存储名称读取Git存储 git stash drop {stashName} # 根据存储名称删除Git存储【使用Git存储将当前状态存储起来】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看当前Git存储列表发现列表为空 $ git stash listAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 使用Git存储 $ git stash warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directory Saved working directory and index state WIP on b1: 01ca592 222-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git stash list stash{0}: WIP on b1: 01ca592 222-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 使用Git将当前状态存储起来后文件内容变为了未更改前的内容 $ cat aaa.txt 111-master 111-b1 222-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 再次查看git的状态发现工作空间正常 $ git status On branch b1 nothing to commit, working tree cleanAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看日志发现使用Git存储也会产生一次日志 $ git log --oneline --graph --all * 082f406 (refs/stash) WIP on b1: 01ca592 222-b1 |\ | * c613227 index on b1: 01ca592 222-b1 |/ * 01ca592 (HEAD - b1) 222-b1 * 1337456 111-b1 * f828bbd (master) 111-master【当前状态被Git存储了当前的工作空间也是正常的因此可以切换到其他分支继续操作】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 切换分支到master $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看master分支的内容 $ cat aaa.txt 111-masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ echo 222-master aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m 222-master ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --graph --all * 4974e13 (HEAD - master) 222-master | * 082f406 (refs/stash) WIP on b1: 01ca592 222-b1 | |\ | | * c613227 index on b1: 01ca592 222-b1 | |/ | * 01ca592 (b1) 222-b1 | * 1337456 111-b1 |/ * f828bbd 111-master2 读取存储状态 等到临时任务处理完后我们可以切换回test分支并将上一次使用Git存储的状态读取出来 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 切换回b1分支 $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看文件内容依旧是没有编辑前的状态 $ cat aaa.txt 111-master 111-b1 222-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Git存储的状态 $ git stash list stash{0}: WIP on b1: 01ca592 222-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 读取状态 $ git stash apply stash{0} On branch b1 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: aaa.txtno changes added to commit (use git add and/or git commit -a) # 读取成功后回到我们当初的状态当前工作空间未提交AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看文件内容将文件内容还原回来了 $ cat aaa.txt 111-master 111-b1 222-b1 333-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git commit -m 333-b1 ./ [b1 1f0ebea] 333-b11 file changed, 1 insertion()AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git log --oneline --graph --all * 1f0ebea (HEAD - b1) 333-b1 | * 4974e13 (master) 222-master | | * 082f406 (refs/stash) WIP on b1: 01ca592 222-b1 | |/| |/| | | | * c613227 index on b1: 01ca592 222-b1 | |/ |/| * | 01ca592 222-b1 * | 1337456 111-b1 |/ * f828bbd 111-master3 存储状态的删除 Git存储被读取之后状态并不会被删除我们可以手动删除存储状态 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Git存储状态发现依旧存在 $ git stash list stash{0}: WIP on b1: 01ca592 222-b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 手动删除状态 $ git stash drop stash{0} Dropped stash{0} (082f40626ab35cf6b1bd413e634e0a1a946824aa)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Git存储的状态发现没有了 $ git stash list1.6.3 Git存储的原理 使用Git存储 Git存储的原理其实是在使用git stash命令后Git直接将当前工作状态的更改添加到暂存区然后提交中途生成了Blob对象、Tree对象、Commit对象等三个对象其中Commit对象会生成2次第一次指向原来的Tree对象第二次指向新的Tree对象之后再将暂存区改回原来的样子执行git stash命令之前的样子 由于当前工作空间的操作均已提交因此当前工作空间的状态自然为nothing to commit然后就可以切换到其他分支了 当使用git stash命令以后会产生两个Commit对象其还会再.git/refs/目录创建一个名为stash的文件该文件保存着最新Commit对象的hash值 读取Git存储状态的原理 当使用git stash apply {stashName}命令读取Git存储状态时其底层其实就是读取到stash文件中的Commit对象通过该Commit对象找到执行git stash命令后生成的Blob对象读取该Blob对象的内容写入当前工作空间达到还原工作空间的目的。 删除Git存储状态的原理 在Git日志中查询不到了然后将git/refs/stash文件删除掉 【准备环境】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ git branch b1 git checkout b1.git/objects/8f/96f2f60c766a6a6b78591e06e6c1529c0ad9af $ find .git/objects/ -type f .git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # Blob对象 .git/objects/7d/811c6d8fa7794fc7a0a2371a4cf197e8cfb47d # Commit对象 .git/objects/8f/96f2f60c766a6a6b78591e06e6c1529c0ad9af # Tree对象AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看当前暂存区 $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git log --oneline --all --graph * 7d811c6 (HEAD - b1, master) 1111 使用存储状态的原理 【编辑文件】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ echo 222 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git status On branch b1 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: aaa.txtno changes added to commit (use git add and/or git commit -a)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 使用Git存储 $ git stash warning: LF will be replaced by CRLF in aaa.txt. The file will have its original line endings in your working directory Saved working directory and index state WIP on b1: 7d811c6 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 暂存区没有变化 $ git cat-file -p 58c9bdf9 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ find .git/objects/ -type f .git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # Blob对象.v1 .git/objects/70/3a3923a3f4d516543ba3e6e9182467f31b328c # Tree对象.v2 .git/objects/7d/811c6d8fa7794fc7a0a2371a4cf197e8cfb47d # Commit对象.v1 .git/objects/8f/96f2f60c766a6a6b78591e06e6c1529c0ad9af # Tree对象.v1 .git/objects/99/11efb0f75f3280b2e8581bd83724e9a7a10528 # Commit对象.v2 .git/objects/a3/0a52a3be2c12cbc448a5c9be960577d13f4755 # Blob对象.v2 .git/objects/b3/e1f5cd5d92a906cff3dfc4816d6e22c72afffe # Commit对象.v3AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看stash文件保存的是最新Commit对象的hash值 $ cat .git/refs/stash b3e1f5cd5d92a906cff3dfc4816d6e22c72afffeAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Blob对象.v2 $ git cat-file -p a30a52a3be2c12cbc448a5c9be960577d13f4755 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Tree对象.v2 $ git cat-file -p 703a3923a3f4d516543ba3e6e9182467f31b328c 100644 blob a30a52a3be2c12cbc448a5c9be960577d13f4755 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Commit对象.v2 $ git cat-file -p 9911efb0f75f3280b2e8581bd83724e9a7a10528 tree 8f96f2f60c766a6a6b78591e06e6c1529c0ad9af # 包裹的是原来的Tree对象v1版本 parent 7d811c6d8fa7794fc7a0a2371a4cf197e8cfb47d # 指向的是Commit对象.v1 author xiaohui xiaohuialiyun.com 1697278938 0800 committer xiaohui xiaohuialiyun.com 1697278938 0800index on b1: 7d811c6 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看Commit对象.v3 $ git cat-file -p b3e1f5cd5d92a906cff3dfc4816d6e22c72afffe tree 703a3923a3f4d516543ba3e6e9182467f31b328c # 包裹的是新的Tree对象(v3) parent 7d811c6d8fa7794fc7a0a2371a4cf197e8cfb47d # 指向Commit对象.v1 parent 9911efb0f75f3280b2e8581bd83724e9a7a10528 # 指向Commit对象.v2 author xiaohui xiaohuialiyun.com 1697278938 0800 committer xiaohui xiaohuialiyun.com 1697278938 0800WIP on b1: 7d811c6 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看日志发现生成了两个Commit对象 $ git log --oneline --all --graph * b3e1f5c (refs/stash) WIP on b1: 7d811c6 111 # Commit对象.v3 |\ | * 9911efb index on b1: 7d811c6 111 # Commit对象.v2 |/ * 7d811c6 (HEAD - b1, master) 111 # HEAD指针还是指向b12 读取存储状态的原理 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 由于当前是Git $ git checkout master Switched to branch masterAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 读取Git存储 $ git stash apply stash{0} On branch b1 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: aaa.txtno changes added to commit (use git add and/or git commit -a) # 工作空间状态恢复成原来的状态了3 删除存储状态的原理 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git stash list stash{0}: WIP on b1: 7d811c6 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 删除Git存储状态 $ git stash drop stash{0} Dropped stash{0} (b3e1f5cd5d92a906cff3dfc4816d6e22c72afffe)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 发现stash文件已经被删除 $ ll .git/refs/ total 0 drwxr-xr-x 1 Adminstrator 197121 0 Oct 14 18:22 heads/ drwxr-xr-x 1 Adminstrator 197121 0 Oct 14 18:20 tags/AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git log --oneline --all --graph * 7d811c6 (HEAD - b1, master) 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ find .git/objects/ -type f .git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # Blob对象.v1 .git/objects/70/3a3923a3f4d516543ba3e6e9182467f31b328c # Tree对象.v2 .git/objects/7d/811c6d8fa7794fc7a0a2371a4cf197e8cfb47d # Commit对象.v1 .git/objects/8f/96f2f60c766a6a6b78591e06e6c1529c0ad9af # Tree对象.v1 .git/objects/99/11efb0f75f3280b2e8581bd83724e9a7a10528 # Commit对象.v2 .git/objects/a3/0a52a3be2c12cbc448a5c9be960577d13f4755 # Blob对象.v2 .git/objects/b3/e1f5cd5d92a906cff3dfc4816d6e22c72afffe # Commit对象.v31.7 Git打标签 1.7.1 标签的语法与介绍 像其他版本控制系统VCS一样Git 可以给仓库历史中的某一个提交打上标签以示重要。 比较有代表性的是人们会使用这个功能来标记发布结点 v1.0 、 v2.0 等等。 创建标签 git tag {tagName} # 以当前状态创建标签 轻量标签 git tag {tagName} {commitHash} # 以指定的提交对象来创建标签轻量标签git tag {tagName} {commitHash} -m {注释} # 以当前状态创建标签 附注标签 git tag {tagName} {commitHash} -m {注释} # 以指定的提交对象来创建标签附注标签查看标签 git tag # 查看所有标签 git show {tagName} # 查看特定标签删除标签 git tag -d {tagName}检出标签 git checkout {tagName} # 检出到指定标签位置会出现头指针分离现象 git checkout -b {branchName} {tagName} # 检出到指定标签位置并在此位置创建分支Git 支持两种标签轻量标签lightweight与附注标签annotated。 轻量标签轻量标签指向某个版本的提交对象的hash值附注标签会在Git数据库中创建一个全新的Git对象——tag对象该tag对象保存了这个版本的提交对象的hash值同时还存储了一些有关于tag本身的日志信息 创建好的标签存储在.git/refs/tags目录 1.7.2 标签的使用 【初始化版本库】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ echo 222 aaa.txt git add ./ git commit -m 222 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 794c1a9 (HEAD - master) 222 a25c873 111【创建标签】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看当前有多少标签 $ git tagAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 创建一个附注标签 $ git tag v1.0 -m 这是我的1.0版本AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看标签 $ git tag v1.0AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看日志 $ git log --oneline 794c1a9 (HEAD - master, tag: v1.0) 222 a25c873 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 创建一个轻量标签 $ git tag v1.2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看标签 $ git tag v1.0 v1.2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看日志 $ git log --oneline 794c1a9 (HEAD - master, tag: v1.2, tag: v1.0) 222 a25c873 111查看.git/refs/tags目录 v1.0是一个附注标签创建附注标签时会创建一个新的tag对象该tag对象保存了所引用的提交对象hash值即一些其他日志信息 v1.2是一个轻量标签创建轻量标签时并不会创建一个tag对象仅仅是将这个表情所指向的提交对象的hash值保存下来 查看轻量标签和附注标签的内容 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ ll .git/refs/tags/ total 2 -rw-r--r-- 1 Adminstrator 197121 41 Oct 16 15:38 v1.0 # 创建了两个标签文件 -rw-r--r-- 1 Adminstrator 197121 41 Oct 16 15:38 v1.2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # v1.0附注标签保存的是tag对象的hash值 $ cat .git/refs/tags/v1.0 8223942a63f358c9f958d39c06f85a7837a29526AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # v1.2轻量标签保存的是提交对象的hash值 $ cat .git/refs/tags/v1.2 794c1a9c132d391e31dfd3c37b16aa948c46d3b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 是一个tag对象 $ git cat-file -t 8223942a63f358c9f958d39c06f85a7837a29526 tagAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 是一个commit对象 $ git cat-file -t 794c1a9c132d391e31dfd3c37b16aa948c46d3b1 commitAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 轻量标签的内容就是一个提交对象 $ git cat-file -p 8223942a63f358c9f958d39c06f85a7837a29526 object 794c1a9c132d391e31dfd3c37b16aa948c46d3b1 type commit tag v1.0 tagger xiaohui xiaohuialiyun.com 1697441891 0800这是我的1.0版本AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 附注标签的内容 $ git cat-file -p 794c1a9c132d391e31dfd3c37b16aa948c46d3b1 tree 703a3923a3f4d516543ba3e6e9182467f31b328c parent a25c8731c03a7c07dc574d8809f6ad7a69c09f27 author xiaohui xiaohuialiyun.com 1697441871 0800 committer xiaohui xiaohuialiyun.com 1697441871 0800222【查看标签】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看v1.0标签(附注标签) $ git show v1.0 tag v1.0 Tagger: xiaohui xiaohuialiyun.com Date: Mon Oct 16 15:38:11 2023 0800这是我的1.0版本commit 794c1a9c132d391e31dfd3c37b16aa948c46d3b1 (HEAD - master, tag: v1.2, tag: v1.0) Author: xiaohui xiaohuialiyun.com Date: Mon Oct 16 15:37:51 2023 0800222diff --git a/aaa.txt b/aaa.txt index 58c9bdf..a30a52a 100644 --- a/aaa.txtb/aaa.txt-1 1,2 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看v1.2标签轻量标签 $ git show v1.2 commit 794c1a9c132d391e31dfd3c37b16aa948c46d3b1 (HEAD - master, tag: v1.2, tag: v1.0) Author: xiaohui xiaohuialiyun.com Date: Mon Oct 16 15:37:51 2023 0800222diff --git a/aaa.txt b/aaa.txt index 58c9bdf..a30a52a 100644 --- a/aaa.txtb/aaa.txt-1 1,2 111 222【使用master分支继续开发】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ echo 333 ccc.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git add ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m 333 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all 472d1e1 (HEAD - master) 333 794c1a9 (tag: v1.2, tag: v1.0) 222 a25c873 111【检出标签】 如果你想查看某个标签所指向的文件版本可以使用 git checkout 命令 虽然这会使你的仓库处于“分离头指针detached HEAD”的状态——这个状态有些不好的副作用 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all 472d1e1 (HEAD - master) 333 794c1a9 (tag: v1.2, tag: v1.0) 222 a25c873 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git checkout v1.0 Note: switching to v1.0.You are in detached HEAD state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:git switch -c new-branch-nameOr undo this operation with:git switch -Turn off this advice by setting config variable advice.detachedHead to falseHEAD is now at 794c1a9 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((v1.0)) # 记得使用-all不然不会显示HEAD之后的内容 $ git log --oneline --all 472d1e1 (master) 333 794c1a9 (HEAD, tag: v1.2, tag: v1.0) 222 # HEAD指针指向的位置并没有指针 a25c873 111在“分离头指针”状态下如果你做了某些更改然后提交它们标签不会发生变化 但你的新提交将不属于任何分支并且将无法访问除非通过确切的提交哈希才能访问。 因此如果你需要进行更改比如你要修复旧版本中的错误那么通常需要创建一个新分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((v1.0)) $ git log --oneline --all 472d1e1 (master) 333 794c1a9 (HEAD, tag: v1.2, tag: v1.0) 222 a25c873 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((v1.0)) # 查看当前HEAD指针位置的工作空间内容发现并没有333 $ cat aaa.txt 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((v1.0)) # 追加内容1010 $ echo 1010 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((v1.0)) $ git commit -m 1010 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((830c1d5...)) $ git log --oneline --all --graph * 830c1d5 (HEAD) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (tag: v1.2, tag: v1.0) 222 * a25c873 111【在当前位置创建分支】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((830c1d5...)) $ git branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((830c1d5...)) # 在当前位置创建b1指针但HEAD指针并没有执行b1 $ git log --oneline --all --graph * 830c1d5 (HEAD, b1) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (tag: v1.2, tag: v1.0) 222 * a25c873 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace ((830c1d5...)) # 切换到b1分支 $ git checkout b1 Switched to branch b1AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git log --oneline --all --graph * 830c1d5 (HEAD - b1) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (tag: v1.2, tag: v1.0) 222 * a25c873 111【检出标签创建分支】 为了避免出现分离头指针现象因此在检出标签时通常会创建一个新的分支 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) $ git log --oneline --all --graph * 830c1d5 (HEAD - b1) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (tag: v1.2, tag: v1.0) 222 * a25c873 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 查看所有标签 $ git tag v1.0 v1.2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b1) # 检出到v1.2标签并且在这个位置创建一个新的分支 $ git checkout -b b2 v1.2 Switched to a new branch b2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) $ git log --oneline --all --graph * 830c1d5 (b1) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (HEAD - b2, tag: v1.2, tag: v1.0) 222 # HEAD指针指向b2分支了 * a25c873 111【删除标签】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 查看当前所有标签 $ git tag v1.0 v1.2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 删除v1.0标签 $ git tag -d v1.0 Deleted tag v1.0 (was 8223942)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 查看当前所有标签 $ git tag v1.2AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 查看日志 $ git log --oneline --all --graph * 830c1d5 (b1) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (HEAD - b2, tag: v1.2) 222 # 发现v1.0标签被删除了 * a25c873 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 查看v1.2标签 $ git tag -d v1.2 Deleted tag v1.2 (was 794c1a9)AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 查看当前所有标签 $ git tagAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (b2) # 查看日志 $ git log --oneline --all --graph * 830c1d5 (b1) 1010 | * 472d1e1 (master) 333 |/ * 794c1a9 (HEAD - b2) 222 # 发现v1.2标签也被删除了 * a25c873 111二、Git数据恢复与还原 2.1 Git的还原 在任何一个阶段你都有可能想要撤消某些操作。 在本章我们将会学习几个撤消你所做修改的基本工具。 注意有些撤消操作是不可逆的。 这是在使用 Git 的过程中会因为操作失误而导致之前的工作丢失的少有的几个地方之一。 有时候我们提交完了才发现漏掉了几个文件没有添加或者提交信息写错了。 此时可以运行带有 --amend 选项的提交命令来重新提交 Git还原的语法 git restore {文件|目录} # 还原工作空间 git restore --staged {文件|目录} # 仅还原暂存区该命令不会还原工作空间git commit --amend -m 注释 # 还原(重置)提交对象信息2.1.1 还原工作空间 【项目初始化】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./【编辑文件然后使用Git还原】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 给文件追加内容改变工作空间的内容 $ echo 222 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看被修改的文件 $ cat aaa.txt 111 222AdministrationXiaoMi MINGW64 ~/Desktop/work (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: aaa.txtno changes added to commit (use git add and/or git commit -a)AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 撤销对工作空间的修改 $ git restore aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 再次查看文件发现之前的更改已经被撤销 $ cat aaa.txt 111AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 再次查看当前状态 $ git status On branch master nothing to commit, working tree clean # 工作空间状态正常2.1.2 还原暂存区 【项目初始化】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./【查看当前暂存区】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看当前暂存区 $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看Blob对象的内容 $ git cat-file -p 58c9bdf9 111【编辑文件】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 编辑文件 $ echo 222 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 添加到暂存区 $ git add ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看暂存区的内容已经被更新了 $ git ls-files -s 100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看Blob对象的内容发现已经修改 $ git cat-file -p c2009 222【还原暂存区】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看当前状态 $ git status On branch master Changes to be committed:(use git restore --staged file... to unstage) # 有更改还未提交modified: aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 还原暂存区 $ git restore --staged aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 工作空间还没有被还原 $ cat aaa.txt 222AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 再次查看暂存区 $ git ls-files -s 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看Blob对象的内容发现已经还原 $ git cat-file -p 58c9 111AdministrationXiaoMi MINGW64 ~/Desktop/work (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: aaa.txtno changes added to commit (use git add and/or git commit -a)【继续还原工作空间】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git restore aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 工作空间也被还原了 $ cat aaa.txt 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git status On branch master nothing to commit, working tree clean2.1.3 还原提交对象 1 提交日志修正 【初始化项目】 rm -rf ./* .git git init echo 初始化文件 aaa.txt git add ./ git commit -m 初始化文件 ./【具体操作】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline f726afa (HEAD - master) 初始化文件AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git commit --amend -m 初始化文件-001 ./ [master c3b69fa] 初始化项目Date: Wed Aug 2 20:03:53 2023 08001 file changed, 1 insertion()create mode 100644 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline c3b69fa (HEAD - master) 初始化文件-001 # 修改了提交日志2 提交内容修正 有时候提交文件之后才发现文件漏了一些内容我们当然可以编辑文件再次提交但这样会使得提交日志过于冗余Git允许我们来修改提交内容并且不造成提交日志的冗余 【初始化项目】 rm -rf ./* .git git init echo 1245 aaa.txt git add ./ git commit -m 初始化文件 ./没有使用还原提交对象对项目中造成很多无用的冗余日志 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ vi aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ cat aaa.txt 12345AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git commit -m 第一次写错了补上了第一次的错误 ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline 0e7e757 (HEAD - master) 第一次写错了补上了第一次的错误 # 多了一条无用日志 13d329d 初始化文件【重新初始化项目】 rm -rf ./* .git git init echo 1245 aaa.txt git add ./ git commit -m 初始化文件 ./使用提交修正 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ vi aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ cat aaa.txt 12345AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git commit --amend -m 初始化文件 ./ # 还原提交对象AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline 1c7942d (HEAD - master) 初始化文件 # 还是一条日志3 提交文件修正 一次提交两个文件结果漏提交了只提交了一个如果再次提交会导致两个文件不是同一个版本。Git同样允许我们对某一次的提交文件进行修正 【初始化项目】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./【提交一个文件】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ echo 222 bbb.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git add ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git commit -m 222 ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看提交日志发现有两个日志 $ git log --oneline 2ae2646 (HEAD - master) 222 59c4706 111【重新初始化项目】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./【使用amend进行提交文件的修正】 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline 3e7ce9d (HEAD - master) 111AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ echo 222 bbb.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git add ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git commit --amend -m 初始化项目 ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline a59cc7a (HEAD - master) 初始化项目 # 只有一个提交日志2.2 Git的数据回退 在你使用 Git 的时候你可能会意外丢失一次提交。 通常这是因为你强制删除了正在工作的分支但是最后却发现你还需要这个分支 亦或者硬重置了一个分支放弃了你想要的提交。 如果这些事情已经发生该如何找回你的提交呢 下面的例子将硬重置你的测试仓库中的 master 分支到一个旧的提交以此来恢复丢失的提交。 首先让我们看看你的仓库现在在什么地方 git reset可以帮助我们回退HEAD指针、暂存区、工作空间 只移动HEAD不改变暂存区和工作空间 git reset --soft {CommitHash} # 回退到指定的Commit对象git reset --soft HEAD~ # 回退到上一个版本 git reset --soft HEAD~~ # 回退到上上个版本移动HEAD、更改暂存区不改变工作空间 git reset --mixed HEAD~ # 回退到上一个版本 git reset --mixed HEAD~~ # 回退到上上一个版本git reset --mixed {CommitHash} # 回退到指定的Commit对象移动HEAD、更改暂存区、更改工作空间 git reset --hard HEAD~ # 回退到上一个版本 git reset --hard HEAD~~ # 回退到上上一个版本git reset --hard {CommitHash} # 回退到指定的Commit对象2.2.1 回退HEA指针 语法 git reset --soft {CommitHash}git reset --soft HEAD~ # 还原到上一个版本 git reset --soft HEAD~~ # 还原到上上个版本Tipsgit reset --soft只会退回HEAD指针并不会对暂存区和工作空间造成影响。 【初始化项目】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ echo 222 aaa.txt git add ./ git commit -m 222 ./ echo 333 aaa.txt git add ./ git commit -m 333 ./AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git log --oneline a6f8911 (HEAD - master) 333 # 指针指向的是最新的Commit对象 0ecb277 222 8b0d39e 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git reflog a6f8911 (HEAD - master) HEAD{0}: commit: 333 0ecb277 HEAD{1}: commit: 222 8b0d39e HEAD{2}: commit (initial): 111【将HEAD指针回退到上一个版本】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git reset --soft HEAD~AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 使用--all也只能查询到HEAD指针指向的位置 $ git log --oneline --all 0ecb277 (HEAD - master) 222 8b0d39e 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 使用reflog命令可以查看到所有的提交对象 $ git reflog 0ecb277 (HEAD - master) HEAD{0}: reset: moving to HEAD~ a6f8911 HEAD{1}: commit: 333 0ecb277 (HEAD - master) HEAD{2}: commit: 222 8b0d39e HEAD{3}: commit (initial): 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看暂存区 $ git ls-files -s 100644 641d57406d212612a9e89e00db302ce758e558d2 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 暂存区的内容没有发生变化 $ git cat-file -p 641d57406d212612a9e89e00db302ce758e558d2 111 222 333AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 工作空间的内容也没有发生变化 $ cat aaa.txt 111 222 333**git reset --soft HEAD~**指令只会回退HEAD指针不会对暂存区、工作空间造成影响其原理也是生成了一次新的Commit对象 【查看当前工作空间状态】 当HEAD指针被回退后当前工作空间的状态变为了Changes to be committed即有修改的操作但还未提交 需要注意的是HEAD指针被回退后暂存区、工作空间的内容均未发生改变如果此时执行commit提交后只是生成了一个新的Commit对象并且HEAD指针将指向这个新的Commit对象但是暂存区和工作空间都未发生变化 因为执行commit提交是将当前暂存区的内容生成Tree对象然后再生成Commit对象HEAD指针被回退后暂存区并没有发送变化此时如果直接提交是毫无意义的 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 当前工作空间状态 $ git status On branch master Changes to be committed:(use git restore --staged file... to unstage)modified: aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 提交 $ git commit -m 222 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看文件内容并没有发生变化 $ cat aaa.txt 111 222 333AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看日志 $ git log --oneline 2226792 (HEAD - master) 222 0ecb277 222 8b0d39e 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git reflog 2226792 (HEAD - master) HEAD{0}: commit: 222 0ecb277 HEAD{1}: reset: moving to HEAD~ a6f8911 HEAD{2}: commit: 333 0ecb277 HEAD{3}: commit: 222 8b0d39e HEAD{4}: commit (initial): 111【选择指定的Commit对象来还原】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git reset --soft 8b0d39eAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline --all 8b0d39e (HEAD - master) 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git reflog 8b0d39e (HEAD - master) HEAD{0}: reset: moving to 8b0d39e 2226792 HEAD{1}: commit: 222 0ecb277 HEAD{2}: reset: moving to HEAD~ a6f8911 HEAD{3}: commit: 333 0ecb277 HEAD{4}: commit: 222 8b0d39e (HEAD - master) HEAD{5}: commit (initial): 1112.2.2 回退暂存区 使用git reset --soft用于还原HEAD指针指向的Commit对象并不会对暂存区和工作空间造成影响 如果想要还原暂存区可以使用git reset --mixed HEAD~或git reset HEAD~同样的git reset --mixed命令也可以还原到相邻位置和任意位置 语法 git reset --mixed {CommitHash}Tipsgit reset --mixed会更改HEAD指针和暂存区并不会对工作空间修改 【初始化项目】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ echo 222 aaa.txt git add ./ git commit -m 222 ./ echo 333 aaa.txt git add ./ git commit -m 333 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 3c69dff (HEAD - master) 333-ccc 4c4b75f 222-bbb bd6f3d2 111-aaaAdministrationXiaoMi MINGW64 ~/Desktop/workspace (master) $ cat aaa.txt 111 222 333AdministrationXiaoMi MINGW64 ~/Desktop/workspace (master) $ git ls-files -s 100644 641d57406d212612a9e89e00db302ce758e558d2 0 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git cat-file -p 641d57406d212612a9e89e00db302ce758e558d2 111 222 333【还原暂存区】 AdministrationXiaoMi MINGW64 ~/Desktop/workspace (master) # 还原暂存区 $ git reset --mixed HEAD~ Unstaged changes after reset: M aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/workspace (master) # 查看暂存区 $ git ls-files -s 100644 a30a52a3be2c12cbc448a5c9be960577d13f4755 0 aaa.txtAdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看暂存区中的Blob对象的内容(发现更改了) $ git cat-file -p a30a52a3be2c12cbc448a5c9be960577d13f4755 111 222AdministrationXiaoMi MINGW64 ~/Desktop/workspace (master) # 工作空间没有更改 $ cat aaa.txt 111 222 333AdministrationXiaoMi MINGW64 ~/Desktop/workspace (master) # 查看日志 $ git log --oneline ffe591a (HEAD - master) 222 f1a17fe 111AdministrationXiaoMi MINGW64 ~/Desktop/work (master) # 查看详细日志 $ git reflog ffe591a (HEAD - master) HEAD{0}: reset: moving to HEAD~ 4e8f0c3 HEAD{1}: commit: 333 ffe591a (HEAD - master) HEAD{2}: commit: 222 f1a17fe HEAD{3}: commit (initial): 111**git reset --soft HEAD~**指令只会还原HEAD指针不会对暂存区、工作空间造成影响其原理也是生成了一次新的Commit对象 【查看当前工作空间状态】 使用reset --soft还原之后当前的工作状态处于Changes to be committed即有更改的文件但还未提交 注意git reset --mixed不会还原工作空间即使当前暂存区被还原了如果此时提交了那么文件还是没有修改因为对于已经执行过add的文件来说执行commit之前会执行一次隐式的add就相当于把工作空间的内容再重新添加到暂存区了然后再生成Tree对象、Commit对象等。 AdministrationXiaoMi MINGW64 ~/Desktop/work (master) $ git status On branch master Changes to be committed:(use git restore --staged file... to unstage)modified: aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git commit -m 222 ./AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看工作空间的文件内容发现还是回退之前的 $ cat aaa.txt 111 222 333AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看状态 $ git status On branch master nothing to commit, working tree cleanAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看暂存区 $ git ls-files -s 100644 641d57406d212612a9e89e00db302ce758e558d2 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看暂存区的内容发现还是回退之前的 $ git cat-file -p 641d57406d212612a9e89e00db302ce758e558d2 111 222 3332.2.3 回退工作空间 语法 git reset --hard HEAD~ # 回退到上一个版本 git reset --hard HEAD~~ # 回退到上上一个版本git reset --hard {CommitHash} # 回退到指定的Commit对象Tips使用git reset --hard回退会回退HEAD指针、暂存区、工作空间这三个地方 【初始化项目】 rm -rf ./* .git git init echo 111 aaa.txt git add ./ git commit -m 111 ./ echo 222 aaa.txt git add ./ git commit -m 222 ./ echo 333 aaa.txt git add ./ git commit -m 333 ./ git log --onelineAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git log --oneline 6fc1ae0 (HEAD - master) 333 d100998 222 980a440 111【执行操作】 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 回退到上个版本 $ git reset --hard HEAD~ HEAD is now at d100998 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # HEAD指针被回退了 $ git log --oneline d100998 (HEAD - master) 222 980a440 111AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git ls-files -s 100644 a30a52a3be2c12cbc448a5c9be960577d13f4755 0 aaa.txtAdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 暂存区被回退了 $ git cat-file -p a30a52a3be2c12cbc448a5c9be960577d13f4755 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 工作空间被回退了 $ cat aaa.txt 111 222AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看回退日志 $ git reflog d100998 (HEAD - master) HEAD{0}: reset: moving to HEAD~ 6fc1ae0 HEAD{1}: commit: 333 d100998 (HEAD - master) HEAD{2}: commit: 222 980a440 HEAD{3}: commit (initial): 111【查看当前工作空间状态】 由于HEAD指针、暂存区、工作空间都被回退了因此当前工作空间为nothing to commit状态即所有的操作均已提交 AdminstratorLAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) $ git status On branch master nothing to commit, working tree clean
http://www.pierceye.com/news/763334/

相关文章:

  • 技术网站的费用怎么做会计分录潍坊模板开发建站
  • 男生女生在床上做的那个网站公众号推广一个6元
  • 湛江做网站设计公司北京婚恋网站哪家最好
  • 大型网站建设的难点是什么物联网技术
  • 怎么免费建个免费的站点写作网站5妙不写就删除
  • 深圳网站建设软件开发公司排名网站做301的坏处
  • ai网站制作的图片
  • 自己想开个网站怎么弄移动端网站设计欣赏
  • 国外网站建站上海品牌策划设计
  • 郑州网站制作选择乐云seo网站建设误区图
  • 湖南智能网站建设多少钱会声会影免费模板网站
  • 社区网站建设方案书建站之星官方网站
  • 过时的网站什么公司做企业网站
  • 最新企业网站搜索引擎优化是做什么
  • 提高网站公信力 单仁手机设计培训网站建设
  • asp.net网站管理系统域名注册报备
  • 买了个网站后怎么做如何提高 网站的点击量
  • 哪些行业网站推广做的多o2o商城源码
  • 北京seo站内优化电商网站前端页面响应式设计
  • 贵港seo关键词整站优化网站恶意攻击
  • 王磊网络网站建设公关
  • 怎么建网站做推广win网站建设
  • 在线做英语题的网站wordpress被设置不录入
  • 桃花岛网站是什么翻硬币网站怎么做
  • 做海报的网站有哪些内容windows同步wordpress
  • 制作网页的网站费用属于资本性支出吗安徽区块链虚拟币网站开发方案
  • 做网站前产品经理要了解什么搜索引擎优化免费
  • 广州网站建设技术方案营销网站推广策略
  • 郑州网站建设、中国菲律宾铁路项目
  • 潜江网站开发学校网站建设领导小组