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

git commit --amend

慌了,不敢编辑上一个commit的description了,直接选择了wq退出,然而git毕竟强大,默认将改动合并提交并覆盖了上一个commit生成了一个新的commit id,这下更慌了,上一个commit idgit 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.txt2.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. 3-具体学习git--reset回到过去的版本(commit间穿梭),checkout单个文件穿梭

    git log --oneline 命令可以在一块儿显示做过的改动. 我在change 2时忘了一条,想在change 1后再添加一个语句或一个操作,然后这个状态再提交仍作为change 2.将这个s ...

  2. application.properties /application.yml官网查看配置;springboot application.properties 官网查看,info yml 查看;springboot.yml查看info;springboot.yml查看Actuator监控中心info

    官网查看: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#appendix 查看info ...

  3. SVN previous operation has not finished

    svn提交遇到恶心的问题,可能是因为上次cleanup中断后,进入死循环了. 错误如下: 解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.d ...

  4. python 基础1

    一.python版本的介绍 python有两个大的版本2.X与3.X的版本,而在不久的将来将全面的进入3的版本.3的版本将比2的版本功能更加强大,而且也修复了大量的bug. 二.python的安装可以 ...

  5. RNN入门

    RNN入门学习 原文地址:http://blog.csdn.net/hjimce/article/details/49095371 作者:hjimce 一.相关理论 RNN(Recurrent Neu ...

  6. Write Markdown Syntax Online Document with Sphinx and Pandoc

    There is no doubt that we have to write doc while we are developing software. But How do you write d ...

  7. uint8_t / uint16_t / uint32_t /uint64_t

    这些数据类型是 C99 中定义的,它就是一个结构的标注,可理解为type/typedef的缩写,表示通过typedef定义.它们只是使用typedef给类型起的别名 #ifndef _UINT8_T ...

  8. (拓扑)确定比赛名次 -- hdu -- 1285

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  9. libtool 创建库的工具

    libtool 创建库的工具 1. 背景 在不同的系统中建立动态链接库的方法有很大的差别,这主要是因为每个系统对动态链接库的用法和实现并不相同,以及编译器对动态链接库支持的选项也不太一样. 对于开发人 ...

  10. 第一天---关于环境和java基础

    一.版本管理 git svn 二.Intellj idea快捷键 三.java基础知识