某同事执行git commit 时太兴奋,执行了

git commit --amend

慌了,不敢编辑上一个commit的description了,直接选择了wq退出,然而git毕竟强大,默认将改动合并提交并覆盖了上一个commit生成了一个新的commit id,这下更慌了,上一个commit id在git log里没了,没了,没了

此时只有两个字,奔溃

好在git有撤销方法,下面的代码拿来举例。


当前代码仓有如下文件:

$ ll
total 4
-rw-r--r-- 1 ****** 1051817 3 九月 13 15:49 1.txt
-rw-r--r-- 1 ****** 1051817 3 九月 13 16:00 2.txt
-rw-r--r-- 1 ****** 1051817 2 九月 13 14:16 3.txt
-rw-r--r-- 1 ****** 1051817 7 九月 13 13:54 README.md

修改1.txt和2.txt,并提交。

$ echo "12" >> 1.txt
$ echo "22" >> 2.txt
$ git add .
$ git commit -m "modified 1/2.txt"
[master b82585f] modified 1/2.txt
3 files changed, 3 insertions(+)
create mode 100644 3.txt

在未push前,继续修改3.txt,并执行git commit --amend覆盖上一个commit。

$ echo "32" >> 3.txt
$ git add 3.txt
$ git commit --amend
[master 6889e84] modified 1/2/3.txt
Date: Thu Sep 14 14:18:40 2017 +0800
3 files changed, 4 insertions(+)
create mode 100644 3.txt

执行git log发现最新的commit id 已经从b82585f变为了6889e84

$ git log
commit 6889e8402d8f40b38f5e6c0aff686b6b3ca82389 (HEAD -> master)
Author: ******
Date: Thu Sep 14 14:18:40 2017 +0800 modified 1/2/3.txt $ 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,

这就演示到文章开头同事遇到的问题了,amend覆盖了上一个commit,且提交了对文件的修改,只是他未修改description。


撤销开始

如果我们忘记了被覆盖的那个commit id,那就用reflog命令看一下

$ git reflog
6889e84 (HEAD -> master) HEAD@{0}: commit (amend): modified 1/2/3.txt
b82585f HEAD@{1}: commit: modified 1/2.txt $ git reset b82585f
Unstaged changes after reset:
M 3.txt $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory) modified: 3.txt no changes added to commit (use "git add" and/or "git commit -a") $ git log
commit b82585f65b4c467a7e5855191b73a613fcf20892 (HEAD -> master)
Author: ares.wang <ares.wang@seraphic-corp.com>
Date: Thu Sep 14 14:18:40 2017 +0800 modified 1/2.txt

可以看出,reset已经恢复到上一个被覆盖的commit id了,且对文件的修改回退到代码仓not staged的状态了

不使用 git reset --hard的目的就是为了保留本地修改,否则修改就会被丢弃!演示如下:

$ git reset --hard b82585f
HEAD is now at b82585f modified 1/2.txt $ 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

3.txt的modified没有了,切记慎用--hard参数,除非你确定放弃当前未提交的所有修改!

git commit --amend的撤销方法的更多相关文章

  1. git commit --amend

    任何时候,你都有可能需要撤消刚才所做的某些操作.接下来,我们会介绍一些基本的撤消操作相关的命令.请注意,有些撤销操作是不可逆的,所以请务必谨慎小心,一旦失误,就有可能丢失部分工作成果. 有时候我们提交 ...

  2. [转]git commit --amend用法

    适用场景: 比方说,你的代码已经提交到git库,leader审核的时候发现有个Java文件代码有点问题,于是让你修改,通常有2种方法: 方法1:leader 将你提交的所有代码 abandon掉,然后 ...

  3. git commit 之后,撤销commit操作

    撤销.修改commit 写代码过程中,如果已经git add [files] git -m commit [files],没有push代码到远程仓库,想撤销commit,可以根据实际情况,使用以下参数 ...

  4. git commit之后,撤销 commit

    写完代码后,我们一般这样 git add . //添加所有文件 git commit -m "本功能全部完成" 执行完commit后,想撤回commit,怎么办? 可以执行如下命令 ...

  5. git commit --amend用法(摘抄)

    适用场景: 比方说,你的代码已经提交到git库,leader审核的时候发现有个Java文件代码有点问题,于是让你修改,通常有2种方法: 方法1:leader 将你提交的所有代码 abandon掉,然后 ...

  6. 『现学现忘』Git后悔药 — 34、git commit --amend 命令

    目录 1.git commit --amend 命令说明 2.使用场景 (1)场景一 (2)场景二 3.git commit --amend 命令原理 这是我们Git中的第三种后悔药. 1.git c ...

  7. [译]git commit --amend

    git commit --amend命令用来修复最近一次commit. 可以让你合并你缓存区的修改和上一次commit, 而不是提交一个新的快照. 还可以用来编辑上一次的commit描述. 记住ame ...

  8. git commit --amend用法

    提交信息很长时间内会一直保留在你的代码库(code base)中,所以你肯定希望通过这个信息正确地了解代码修改情况. 下面这个命令可以让你编辑最近一次的提交信息,但是你必须确保没有对当前的代码库(wo ...

  9. GIT:修改上一次提交的注释信息(git commit --amend)

    git commit -m 注释信息 如果这时候注释信息输入错误,就可以输入以下指令更改 git commit --amend 键入" i "进入编辑模式 修改后键入ESC,:wq ...

随机推荐

  1. w7 目录

    第17章 期中架构体系介绍 期中架构环境准备 01-期中架构内容简介 02-期中架构大酒楼详解 03-期中架构使用到的软件简介 04-期中架构运维角度观察与使用的软件 05-重头开始创建一台新的虚拟机 ...

  2. web程序打包详解

       重要更新:鉴于很多小伙伴们说看不到图,我这边换了几个浏览器看了下,都看得到的,估计是网速问题,请耐心等待,另外,为了更好的方便大家学习,特此提供源码以及一个word文档,word文档就是本文内容 ...

  3. HDU 5212 Code (莫比乌斯反演)

    题意:给定上一个数组,求 析: 其中,f(d)表示的是gcd==d的个数,然后用莫比乌斯反演即可求得,len[i]表示能整队 i 的个数,可以线性筛选得到, 代码如下: #pragma comment ...

  4. GPS 编程笔记

    1.在GPS系统内,经纬度的显示方式一般都可以根据自己的爱好选择,一般有"hddd.ddddd"(度.度),"hddd*mm.mmm"(度.分. 分),&quo ...

  5. day27(反射之内省机制)

    内省 内省:底层是使用反射机制实现的,是对于反射的进一步封装. 反射:通过类名来获取类中的所有属性和方法及类中的所有隐藏的方法. 内省:通过一个标准类(javabean类)来获取bean中的字段.ge ...

  6. eclipse编辑器栏上的路径怎么去掉

    找到这个按钮,点一下就可以了:

  7. 20155326《Java程序设计》实验一实验报告

    实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版) ...

  8. 微信小程序-bindtap事件与冒泡

    bindtap就是点击事件 在.wxml文件绑定: <text id='textId' data-userXxx='100' bindtap='tapMessage'>cilck here ...

  9. python_条件语句

    条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python程序语言指定任何非0和非空(null)值为true,0 或者 null为fals ...

  10. codeblock快捷键使用

    •在编辑区按住右键可拖动代码,省去拉滚动条之麻烦:相关设置:Mouse Drag Scrolling. •Ctrl+D可复制当前行或选中块. •可拖动选中块使其移动到新位置,按住Ctrl则为复制到新位 ...