git commit --amend的撤销方法
某同事执行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的撤销方法的更多相关文章
- git commit --amend
任何时候,你都有可能需要撤消刚才所做的某些操作.接下来,我们会介绍一些基本的撤消操作相关的命令.请注意,有些撤销操作是不可逆的,所以请务必谨慎小心,一旦失误,就有可能丢失部分工作成果. 有时候我们提交 ...
- [转]git commit --amend用法
适用场景: 比方说,你的代码已经提交到git库,leader审核的时候发现有个Java文件代码有点问题,于是让你修改,通常有2种方法: 方法1:leader 将你提交的所有代码 abandon掉,然后 ...
- git commit 之后,撤销commit操作
撤销.修改commit 写代码过程中,如果已经git add [files] git -m commit [files],没有push代码到远程仓库,想撤销commit,可以根据实际情况,使用以下参数 ...
- git commit之后,撤销 commit
写完代码后,我们一般这样 git add . //添加所有文件 git commit -m "本功能全部完成" 执行完commit后,想撤回commit,怎么办? 可以执行如下命令 ...
- git commit --amend用法(摘抄)
适用场景: 比方说,你的代码已经提交到git库,leader审核的时候发现有个Java文件代码有点问题,于是让你修改,通常有2种方法: 方法1:leader 将你提交的所有代码 abandon掉,然后 ...
- 『现学现忘』Git后悔药 — 34、git commit --amend 命令
目录 1.git commit --amend 命令说明 2.使用场景 (1)场景一 (2)场景二 3.git commit --amend 命令原理 这是我们Git中的第三种后悔药. 1.git c ...
- [译]git commit --amend
git commit --amend命令用来修复最近一次commit. 可以让你合并你缓存区的修改和上一次commit, 而不是提交一个新的快照. 还可以用来编辑上一次的commit描述. 记住ame ...
- git commit --amend用法
提交信息很长时间内会一直保留在你的代码库(code base)中,所以你肯定希望通过这个信息正确地了解代码修改情况. 下面这个命令可以让你编辑最近一次的提交信息,但是你必须确保没有对当前的代码库(wo ...
- GIT:修改上一次提交的注释信息(git commit --amend)
git commit -m 注释信息 如果这时候注释信息输入错误,就可以输入以下指令更改 git commit --amend 键入" i "进入编辑模式 修改后键入ESC,:wq ...
随机推荐
- IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA环境搭建,JPA实现非字符型设置长度
1.在github上已有配置环境,如下链接,(需要环境JDK1.8及以上版本,Tomcat1.8及以上版本,搭建maven库,使用编译器IntellJ IDEA) https://github.com ...
- 1.9yield方法
yield()方法的作用放弃当前的cpu资源,将他让给其他的任务去占用cpu的执行时间,但放弃的时间不确定,有可能刚放弃,马上又获得cpu时间片 测试 package com.cky.thread; ...
- Forward团队-爬虫豆瓣top250项目-团队编程项目开发环境搭建过程
本次结对编程和团队项目我都需要用python环境,所以环境的搭建是一样的.(本文部分内容引用自己博客:http://www.cnblogs.com/xingyunqi/p/7527411.html) ...
- uva1659(最大费用循环流)
紫书上的一道题,做法见紫书P378,这篇博客用的第二种方法,关于正确性的证明,画图可以发现如果一个环是负环,跑最小费用流跑出的是环上的所有正边,再减去负边和即为跑一遍的负权,如果是正环,最小费用流即为 ...
- thrift学习总结
thrift 重要的几个组件有 :数据类型,transport,protocol,versioning,processor 1.数据类型 thrift的数据类型有1.一些原生类型,比如string,i ...
- linux 查看、关闭 ssh pts/n登录的用户
1.查看登录用户: [root@TiaoBan- bin]# w :: up days, :, users, load average: 1.90, 1.75, 1.84 USER TTY FROM ...
- 3.insert添加用法
一.新增用户接口 UserMapper.java package tk.mybatis.simple.mapper; import org.apache.ibatis.annotations.Para ...
- Android-Throwable: A WebView method was called on thread 'JavaBridge'.
错误详情: 01-30 03:36:52.441 12000-12048/cn.h5 D/@@@: e.ttt:java.lang.RuntimeException: java.lang.Throwa ...
- ASP.Net Core 2.2 MVC入门到基本使用系列 (二)
本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...
- .net core 与ELK(5)安装logstash
1.下载https://www.elastic.co/downloads/logstash到/usr/local/src wget https://download.elastic.co/logsta ...