Git reset head revert 回滚

Overview

涉及Git一些日常操作 :)

基础知识

《Pro Git》至少了解branch,commit的概念,及基本的原理

  • Git常用魔法
  • 存档:master代码回滚方法
  • 我是QA,我用Git

Git 工作区、暂存区和版本库

Overview

暂存区(stage, index)是 Git 最重要的概念之一

  • 工作区: 你的目录下面的文件们
  • 暂存区: 由.git/index保存引用,.git/object保存对象的区域
  • 版本库: 仓库

Relations

Why stage before commit

  • Split work into separate commits  分开提交,比如当只想提交一部分时
  • Don't break the build  不影响项目构建

HEAD

这样的引用标识符——它看起来并不像一个普通的引用——其实并不包含 SHA-1 值,而是一个指向另外一个引用的指针。内容如下

 

 

HEAD~n

表示从当前HEAD指向的commit开始数的第几个(参见git log HEAD或git log)

从0开始计数,git log中第一个commit 表示HEAD~0

reset

下面这段话非常重要

 

git reset [<mode>] [<commit>]
This form resets the current branch head to <commit> and possibly updates the index
(resetting it to the tree of <commit>) and the working tree depending on <mode>.
!!!!!! If <mode> is omitted, defaults to "--mixed". The <mode> must be one of the following:

--soft
Does not touch the index file nor the working tree at all (but resets the head to <commit>,
 just like all modes do). This leaves all your changed files "Changes to be committed", 
as git status would put it.

--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for
commit) and reports what has not been updated. This is the default action.

--hard
Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

--merge
Resets the index and updates the files in the working tree that are different between <commit> and HEAD,
 but keeps those which are different between the index and working tree (i.e. which have changes which
 have not been added). If a file that is different between <commit> and the index has unstaged changes, 
reset is aborted.

In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged 
index entries.

--keep
Resets index entries and updates files in the working tree that are different between <commit> and HEAD.
If a file that is different between <commit> and HEAD has local changes, reset is aborted.

!!!If you want to undo a commit other than the latest on a branch, git-revert(1) is your friend.

 

将当前的branch head重置到某个commit上面,这可能会影响index(暂存)区域。
默认情况下使用--mixed形式`

下面是各个形式的差别

注意,暂时只关注soft, mixed, hard,另外还有两种是merge和keep

working index HEAD target         working index HEAD
----------------------------------------------------
 A       B     C    D     --soft   A       B     D
                          --mixed  A       D     D
                          --hard   D       D     D

 

撤销上次add的,(但不想改变工作目录下面的代码)

git reset HEAD (该命令会改变index与HEAD,使得文件状态变成unstage)

如下图所示:默认情况下,HEAD可以不写。

该命令可以完整的写法是git reset HEAD~0 --mixed

解释为重定向到这次的提交

working index HEAD target         working index HEAD
----------------------------------------------------
 A       B     C    C     --mixed   A       C     C

 

另外,从上面看来,值得注意的是以下写法是等同的

  • git reset
  • git reset HEAD
  • git reset HEAD~0

撤销上次commit的,(但不想改变工作目录下面的代码)

git reset HEAD~1 --mixed

解释为重定位到上次提交

(该命令会改变index与HEAD,使得文件状态变成unstage)

working index HEAD target         working index HEAD
----------------------------------------------------
 A       B     C    D     --mixed   A       D     D

 

如果想完全忽略这次commit编辑的内容

1. 可以继续使用 git checkout – . 清除所有与index(此时index已经等于上次commit)不同的改变

2. 或者直接使用 git reset HEAD~1 --hard

revert

简单来说,执行某次commit操作的逆向操作,并且提交(默认提交)。

发布回滚

回滚有两种可能

  • 回滚线上项目正在使用的代码
  • 回滚版本控制器中主线的源代码

通常情况下,QA大叫出事啦出事啦,回滚回滚。意思是线上项目的回滚而非代码的回滚。(当然最好还是问清楚是不是这样)

源代码真的要回滚吗?

主线源代码回滚,能做是能做,但比较麻烦。所以先case by case考察一下必要性。线上回滚,并不意味着源代码一定要回滚。下面这些情况就不用回滚:

  • 线上回滚后,查出并不是因为源代码有问题。下次不用改源代码,再发一遍rtag就好啦。
  • 下次线上发布,是在修正前次失败的发布中的错误后,发布前次失败的发布想发布的内容。

只有当要修正前次失败的发布中的错误需要较长时间,而同时又有其他new feature或bug fix着急上线时,才需要主线上的源代码回滚。

是reset还是revert

不论是使用reset还是使用revert都会带来不同程度的麻烦

Reset比revert会带来更多地麻烦。情况会非常复杂。

我们系统中使用revert。

已经merge master的开发分支,只需要继续merge master即可(此次回滚完全不可见)

当前开发(正在发布的),需要merge master之后 revert掉master上revert的那次commit

reference

  1. Git 工作区、暂存区和版本库
  2. What is the benefit of git's two-stage commit process (staging)?
  3. What does 'stage' mean in git?

你可能感兴趣的:(Git reset head revert 回滚)