很多时候,git新手容易误操作,比如,在levelIISZ-1.4.dev分支下,运行了git pull idc cpp-1.0的结果,这样做麻烦很大,经常导致maven项目格式不正确,这个时候,可以用git reset --hard 去撤销这次修改
但是这样做也有问题,可能之前本地的,没有提交的修改,都消失了。可以尝试git revert命令

reset是指将当前head的内容重置,不会留任何痕迹。

Sets the current head to the specified commit and optionally resets the index and working tree to match.

git reset --hard HEAD~3

会将最新的3次提交全部重置,就像没有提交过一样。

根据--soft --mixed --hard,会对working tree和index和HEAD进行重置。

revert是撤销某次提交,但是这次撤销也会作为一次提交进行保存(这样就不会丢失原来修改过,但是没有提交的内容?)。

//////////////////////////////////////////////////////////////////////////////////

非常好的一篇文章

http://alpha-blog.wanglianghome.org/2010/07/30/git-partial-rollback/

如何使用git回退部分修改

人总有犯错误的时候,如果发现不小心把私货提交到公共代码库,改如何挽回呢?

例如如下代码库:

$ mkdir git-partial-revert
$ cd git-partial-revert
$ echo "hello a" >a.txt
$ echo "hello b" >b.txt
$ git init
Initialized empty Git repository in /home/liang/project/git/git-partial-revert/.git/
$ git add *.txt
$ git commit -m "initial version"
[master (root-commit) b5e1a24] initial version
 2 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
 create mode 100644 b.txt
$ git log
commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:20:38 2010 +0800

initial version

以下是一次错误的修改。

$ echo "hello a from b" >>a.txt
$ echo "hello b from a" >>b.txt
$ git commit -a -m "hello a from b"
[master df3144e] hello a from b
 2 files changed, 2 insertions(+), 0 deletions(-)
$ git push origin master
$ git log
commit df3144e3168f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:22:51 2010 +0800

hello a from b

commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:20:38 2010 +0800

initial version

错误在于只应该提交对于a.txt的修改,b.txt的修改是私货,应该留在本地,以后提交。

第一种方法:纯手工修改。取决于要修改的内容多少,这可能是最简单也可能是最笨的方法。

如果错误发生在最新的commit里面,可以使用git reset修改。如下:

$ git reset b5e1a24f -- b.txt
Unstaged changes after reset:
M       b.txt
$ cat b.txt
hello b
hello b from a
$ git log
commit df3144e3168f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <lwang1@marvell.com>
Date:   Fri Jul 30 13:22:51 2010 +0800

hello a from b

commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:20:38 2010 +0800

initial version
$ git diff
diff --git a/b.txt b/b.txt
index b1bdbca..ab47375 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
 hello b
+hello b from a
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   b.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   b.txt
#
$ git diff --cached
diff --git a/b.txt b/b.txt
index ab47375..b1bdbca 100644
--- a/b.txt
+++ b/b.txt
@@ -1,2 +1 @@
 hello b
-hello b from a
$ git commit -m "revert change in b"
[master d49f9f2] revert change in b
 1 files changed, 0 insertions(+), 1 deletions(-)
$ git push origin master
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   b.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/b.txt b/b.txt
index b1bdbca..ab47375 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
 hello b
+hello b from a
$ git log --stat
commit d49f9f2531ed9106ea53006bd698bbcdd54698e9
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:34:43 2010 +0800

revert change in b

b.txt |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)

commit df3144e3168f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:22:51 2010 +0800

hello a from b

a.txt |    1 +
 b.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:20:38 2010 +0800

initial version

a.txt |    1 +
 b.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

这时b的修改就从公共代码库上拿掉了,但是还保留在本地。

或者也可以使用git revert。下面操作的缺点是没有保留对于b.txt的修改。如何保留修改留给读者作为习题 :-)

$ git revert -n df3144e31
Finished one revert.
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   a.txt
#       modified:   b.txt
#
$ git diff --cached
diff --git a/a.txt b/a.txt
index 2c5e468..74614c9 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1 @@
 hello a
-hello a from b
diff --git a/b.txt b/b.txt
index ab47375..b1bdbca 100644
--- a/b.txt
+++ b/b.txt
@@ -1,2 +1 @@
 hello b
-hello b from a
$ git reset HEAD a.txt
Unstaged changes after reset:
M       a.txt
$ cat a.txt
hello a
$ git checkout -- a.txt
$ cat a.txt
hello a
hello a from b
$ cat b.txt
hello b
$ git commit -m "revert change in b"
[master 5f6a2e1] revert change in b
 1 files changed, 0 insertions(+), 1 deletions(-)
$ git log --stat
commit 5f6a2e16bfd59281d0150e3644aa1485dd6c0078
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 14:17:44 2010 +0800

revert change in b

b.txt |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)

commit df3144e3168f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:22:51 2010 +0800

hello a from b

a.txt |    1 +
 b.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <a@b.c>
Date:   Fri Jul 30 13:20:38 2010 +0800

initial version

a.txt |    1 +
 b.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

如果错误的修改已经不是最新的commit,则只能使用git revert。

/////////////////////////////////////////////////////////////////////////////

我们teamleader的总结,很不错的!!!

http://guibin.iteye.com/blog/101436

超级有用的git reset --hard和git revert命令的更多相关文章

  1. git reset --hard和git revert命令

      git reset --hard和git revert命令   git误操作时可以用git reset –hard 去撤销这次修改, 但是这样做也有问题,可能在之前本地有没有提交的修改也都消失了, ...

  2. git reset HEAD 与 git reset --hard HEAD的区别

    感谢原文作者:天地逍遥 原文链接:https://www.jianshu.com/p/aeb50b94e6c0 git reset HEAD 是将咱暂存区和HEAD的提交保持一致 git reset ...

  3. git回退之git reset

    参考 https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD%E5%AF%86 https: ...

  4. [转]恢复 git reset -hard 的误操作

    转帖:http://hi.baidu.com/configuration/item/97fddeea252818d0eb34c964 有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 r ...

  5. 恢复 git reset -hard 的误操作

    有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 reset, rebase 和 merge.甚至一些很小的操作,例如删除一个分支,我都担心数据丢失. 不 久之前,我在做一些大动作(re ...

  6. git reset and git checkout

    git reset --hard <commit>: 1.替换引用的指向.引用指向新的提交ID; 2.替换暂存区.替换后,暂存区的内容和引用指向的文件夹树一致; 3.替换工作区.替换后,工 ...

  7. [转] git reset简介

    http://blog.csdn.net/hudashi/article/details/7664464 http://guibin.iteye.com/blog/1014369 http://hi. ...

  8. 【Git 学习三】深入理解git reset 命令

    重置命令(git reset)是Git 最常用的命令之一,也是最危险最容易误用的命令.来看看git reset命令用法. --------------------------------------- ...

  9. git reset 版本回退

    git log 查看所有提交信息. commit 67692318180bed6b2a17db0708cfbe0231e33db3 (HEAD -> master) Author: kingBo ...

随机推荐

  1. HDU_1561_The more, The Better_树型dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 The more, The Better Time Limit: 6000/2000 MS (J ...

  2. 搭建linux环境:如何在vmware安装linux虚拟机??

    本来不想再整一遍的,奈何分布式压测呀,呀呀呀呀呀呀 1.安装linux虚机 (1)在桌面上双击VMware Workstation图标后启动虚拟机,鼠标单击文件,选择新的虚拟机: (2)单击“next ...

  3. [Tensorflow] 使用 model.save_weights() 保存 Keras Subclassed Model

    import numpy as np import matplotlib.pyplot as plt import os import time import tensorflow as tf tf. ...

  4. boostrap标签

    字体: <lead>:加强显示 <strong><b>:字体加粗 <i><em>:斜体字 .text-muted:提示,使用浅灰色(#999 ...

  5. POJ P2096 Collecting Bugs

    思路 分类讨论,不妨先设$DP[i][j]$表示已经发现$i$种子系统中有$n$种$bug$无非只有四种情况 发现的$bug$在旧的系统旧的分类,概率$p1$是$(i/s)*(j/n)$. 发现的$b ...

  6. 深入分析同步工具类之AbstractQueuedSynchronizer

      概览: AQS(简称)依赖内部维护的一个FIFO(先进先出)队列,可以很好的实现阻塞.同步:volatile修饰的属性state,哪个线程先改变这个状态值,那么这个线程就获得了优先权,可以做任何事 ...

  7. TestNG忽略测试

    用@Test(enabled = false) 声明需要被忽略执行的测试方法 package com.janson; import org.testng.annotations.Test; publi ...

  8. linux top-显示或管理执行中的程序

    推荐:更多linux 性能监测与优化 关注:linux命令大全 top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面, ...

  9. pageContext对象的使用及常用方法

    pageContext对象的使用及常用方法 制作人:全心全意 获取页面上下文的pageContext对象是一个比较特殊的对象,通过它可以获取JSP页面的request.response.session ...

  10. Djang学习笔记-1

    1.django的生命周期: url匹配 -> 视图函数 -> 返回用户字符串 url匹配 -> 视图函数 -> 打开一个HTML文件,并读取内容2.创建Django proj ...