从零开始的git学习之旅-版本回退与撤销修改_git版本回退
文章目录
前言
Git能够管理文件的历史版本,这也是版本控制器的能力。如果某一天发现之前的工作中存在着大量的错误,那就可能需要从某个特定的版本重新开始,那就需要版本回退功能。 同样,如果希望取消自己的某些代码,那就需要进行撤销修改。本文主要对这两种情况进行了介绍,同时还对删除文件进行了说明。
一、版本回退
1 语法说明
Git中 使用git reset来进行版本回退,可以指定退回到某一次提交的版本。其中,回退是指 将版本库中的内容进行回退,工作区或者暂存区是否回退由命令参数进行决定。
git reset命令的语法格式为:
–mixed:默认选项,即不用不写该参数。将暂存区,版本库的内容回退到指定提交版本,工作区文件保持不变。
–soft: 工作区和暂存区的内容均不变,只将版本库内容进行回退。
–hard:将暂存区,工作区,版本库都退回到指定版本。如果工作区中有未提交的代码,最后不要使用该命令。该参数慎用。
HEAD:
- 可以直接写成commit_id,退回到指定版本
- HEAD(HEAD~0)表示当前版本
- HEAD^(HEAD~1)表示上一版本
- HEAD^(HEAD~2)表示上上版本
- 以此类推
2 基本使用
如下,创建test文件,分别对其进行三次提交。
使用 git log --pretty=oneline查看提交记录。
现在,假设提交完version3后,发现version3有编写错误,想回退到version2,重新对version2进行编写。这里,我们希望工作区也进行回退,故使用 --hard参数。
#全部回退git reset --hard 62f5f5cae30ad2758848ae7ad21b25b706524d49
可以看到,test的文件内容已经回退到version2了。使用git log查看一下提交日志,发现HEAD已经指向了version2。
如上图中,62f5f5c是什么意思?仔细对比上面的commit_id可以看出,这是version2的部分commit_id。Git回退时,可以使用部分commit_id来代替目标版本。
现在已经回退到version2版本,但此时我反悔了,但git log此时已经无法查看,怎么办? Git提供了git reflog 命令,记录本地的每一次命令。
#查看记录的每次提交记录,git reflog
用此方法,就可以回退到version3了。但实际开发中,可能开发的时间很长,要找到指定commit_id,显然也很困难了。
二、撤销修改
修改的目的:不影响远程仓库(概念后面再说)
1. 工作区的代码,还未add
理论上,直接将这些代码手动修改一下就可以了,可如果是你写了好几天的代码还未提交呢?这样自己可能也不知道修改了哪些代码。 Git提供了 git checkout – 命令来让工作区的文件回到最近一次add/commit的状态。
# --非常重要,是不可以省略的git checkout -- [file]
下面演示一下这个过程:
[yx@VM-16-11-centos gitcode]$ lsfile1 file2 file3 file4 readme test[yx@VM-16-11-centos gitcode]$ cat testhello version1hello version2[yx@VM-16-11-centos gitcode]$ vim test[yx@VM-16-11-centos gitcode]$ cat testhello version1hello version2hello version3[yx@VM-16-11-centos gitcode]$ git checkout -- test[yx@VM-16-11-centos gitcode]$ cat testhello version1hello version2
2. 已经add,但没有commit
add后就保存到了暂存区,此时怎么撤销呢?
#方法一:git reset HEAD [文件名] #回退到当前版本#方法二:git reset --mixed #回退版本库和暂存区,之后就只需要回退工作区即可。git checkout -- [file] #回退工作区
演示如下:
[yx@VM-16-11-centos gitcode]$ cat testhello version1hello version2[yx@VM-16-11-centos gitcode]$ vim test[yx@VM-16-11-centos gitcode]$ git add .[yx@VM-16-11-centos gitcode]$ git status #查看结果,待commit# On branch master# Changes to be committed:# (use \"git reset HEAD ...\" to unstage)##modified: test#[yx@VM-16-11-centos gitcode]$ cat test #新增了一行内容hello version1hello version2hello version3[yx@VM-16-11-centos gitcode]$ git reset HEAD test #撤销修改Unstaged changes after reset:Mtest[yx@VM-16-11-centos gitcode]$ git status# On branch master# Changes not staged for commit: #此时暂存区就干净了# (use \"git add ...\" to update what will be committed)# (use \"git checkout -- ...\" to discard changes in working directory)##modified: test#no changes added to commit (use \"git add\" and/or \"git commit -a\")
3. 已经add,并且已经commit
方法: 使用 git reset --hard HEAD^ 来回退到上一个版本。但这是有条件的,就是其必须未将该版本推送到远程仓库,一旦推送,就真惨了…
#向test中新增一个一行代码[yx@VM-16-11-centos gitcode]$ vim test [yx@VM-16-11-centos gitcode]$ git add .[yx@VM-16-11-centos gitcode]$ git commit -m \'md test\' #提交修改[master e7637da] md test 1 file changed, 1 insertion(+)[yx@VM-16-11-centos gitcode]$ git reset --hard HEAD^ #回退到上一版本HEAD is now at 62f5f5c version2[yx@VM-16-11-centos gitcode]$ cat test #回退成功hello version1hello version2[yx@VM-16-11-centos gitcode]$
三、删除文件
由第一章内容可知,Git中,删除文件也是一个修改操作,如果要删除一个文件该怎么办呢?
如上这种删除方式是无用的,因为其只删除了工作区的文件,那这样就导致了工作区和版本库不一致了,要删除文件,不仅要删除工作区文件,还有同时删除版本库的文件。
一般删除文件有两种情况:
- 不小心删除
- 要从版本库中删除该文件
对于第一种情况,可以使用前面的 git checkout-- [file]来进行恢复。
[yx@VM-16-11-centos gitcode]$ lsfile1 readme test[yx@VM-16-11-centos gitcode]$ rm file1[yx@VM-16-11-centos gitcode]$ git status# On branch master# Changes not staged for commit:# (use \"git add/rm ...\" to update what will be committed)# (use \"git checkout -- ...\" to discard changes in working directory)##deleted: file1#no changes added to commit (use \"git add\" and/or \"git commit -a\")[yx@VM-16-11-centos gitcode]$ git checkout -- file1[yx@VM-16-11-centos gitcode]$ lsfile1 readme test #恢复成功
对于第二种情况,明显是还未完全删完。 Git中,使用 git rm命令来将文件从暂存区和工作区中删除,之后commit即可。
[yx@VM-16-11-centos gitcode]$ lsfile1 readme test[yx@VM-16-11-centos gitcode]$ git rm file1rm \'file1\'[yx@VM-16-11-centos gitcode]$ git status# On branch master# Changes to be committed:# (use \"git reset HEAD ...\" to unstage)##deleted: file1#[yx@VM-16-11-centos gitcode]$ git commit -m \'delete file1\'[master abbf2f5] delete file1 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 file1[yx@VM-16-11-centos gitcode]$ git status# On branch masternothing to commit, working directory clean
现在,文件就从版本库中被删除了。