Overview

涉及Git一些日常操作 :)

基础知识

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

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 回滚的更多相关文章

  1. git的几种回滚 git revert 和 git reset的区别

    git的几种回滚 git revert 和 git reset的区别:强烈建议:对HEAD不熟的话最好不要用HEAD,直接用commitID吧,我遇到的问题:reset HEAD~1之后,可能是别人提 ...

  2. Git 使用revert回滚已提交的commit

    在git使用中如果提交错误的代码至远程服务器,可以使用git revert 命令回滚单次commit并且不影响其他commit. 回滚最新一次的提交记录: git revert HEAD 回滚前一次的 ...

  3. Git远程库版本回滚

    在git的一般使用中,如果发现错误的将不想staging的文件add进入index之后,想回退取消,这就叫做git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id.可以使用命令:g ...

  4. git远程代码库回滚(webstorm下)

    git远程代码库回滚(webstorm下) 1. 场景 添加了一个文件[file-for-test.js]到git的控制下 进行了三次修改,并分别进行了三次commit,最后进行了一次push git ...

  5. Git的撤销与回滚

    1,commit 之前的撤销 未添加至暂存区的撤销(add 之前) git status git checkout . 已添加至暂存区的撤销(add 之后,有或者没有commit操作都可以执行) gi ...

  6. git用法之[回滚代码]

    我们在写代码的任何过程中,都有可能出错,任何过程都有可能要!回!滚!代!码!事关重大!一定要详细讲讲. 一.关于 工作区.暂存区.本地分支: 工作区:即自己当前分支所修改的代码,git add xx ...

  7. git push错误,如何回滚

    --> git push Counting objects: 81, done.Delta compression using up to 4 threads.Compressing objec ...

  8. Git使用小技巧之回滚和撤销

    想要获取更多文章可以访问我的博客 - 代码无止境. 日常的开发,我们难免会创建错误的git提交记录,整个时候git给我们提供了两个命令来解决这个问题.一个命令是git reset,另一个是git re ...

  9. git push之后回滚(撤销)代码

    问题描述:首先,先说明一下,为什么会引发这次的话题,是这样的,我做完功能Agit push之后,2个月后需求部门要求不要功能A了,然后需要在没有功能A的基础上开发,怎么办?赶紧回滚代码呀. 然后我用g ...

随机推荐

  1. JS给HTML5页面<Select></Select>绑定选中项

    获取选中值: function setApprovalPersonName() { var name = $("#approvalPersion").find("opti ...

  2. Git密钥生成步骤SSH Key

    顺便推荐下自己的网站: 一个php后台极速开发框架 https://www.lotusadmin.top/ 一个有趣的网站 https://www.waytomilky.com/ Git是分布式的代码 ...

  3. EF 的一些不常用的功能

    1.Add-Migration Initia-IgnoreChanges 生成已有数据库初始化代码 2.update-database -verbose 升级数据库并显示sql语句 3.Update- ...

  4. 异步FIFO跨时钟域亚稳态如何解决?

    跨时钟域的问题:前一篇已经提到要通过比较读写指针来判断产生读空和写满信号,但是读指针是属于读时钟域的,写指针是属于写时钟域的,而异步FIFO的读写时钟域不同,是异步的,要是将读时钟域的读指针与写时钟域 ...

  5. Bootstrap table的一些简单使用总结

    在GitHub上Bootstrap-table的源码地址是:https://github.com/wenzhixin/bootstrap-table Bootstrap-table的文档地址:http ...

  6. Spring Cloud构建微服务架构(七)消息总线

    先回顾一下,在之前的Spring Cloud Config的介绍中,我们还留了一个悬念:如何实现对配置信息的实时更新.虽然,我们已经能够通过/refresh接口和Git仓库的Web Hook来实现Gi ...

  7. 杂项-frame:Rails框架

    ylbtech-杂项-frame:Rails框架 Rails框架首次提出是在2004年7月,它的研发者是26岁的丹麦人David Heinemeier Hansson.不同于已有复杂的Web 开发框架 ...

  8. 长短时记忆网络(LSTM)

    长短时记忆网络 循环神经网络很难训练的原因导致它的实际应用中很处理长距离的依赖.本文将介绍改进后的循环神经网络:长短时记忆网络(Long Short Term Memory Network, LSTM ...

  9. 第7章 进程关系(5)_贯穿案例2:mini shell(2)

    5. 贯穿案例2:mini shell(2) (1)己经完成的功能:pwd.cd.exit命令 (2)阶段性目标: ①env.export.echo及其他命令 ②标准输入.输出重定向"> ...

  10. 基于WMI获取本机真实网卡物理地址和IP地址

    using System; using System.Collections.Generic; using System.Management; using System.Runtime.Intero ...