如何使用git回退部分修改(转)

 
 

很多时候,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的总结,很不错的!!!

如何使用git回退部分修改(转)的更多相关文章

  1. git回退文件修改

    假设git仓库某个文件的提交信息如下: [cxy@localhost-live mate-power-manager]$ git log -n3 SPECS/mate-power-manager.sp ...

  2. git使用---安装,提交,回退,修改,分支,标签等

    下面是对git的各种使用及命令的基础使用,来自廖雪峰老师的git教程,这个收录下,作为git的使用总结. github上面地址为:https://github.com/Zhangguoliu/lear ...

  3. git回退操作

    情况一:checkout 当你修改了某个文件,未提交暂存区,回退本次修改 git checkout -- file 情况三:reset 当你的代码,已提交到暂存区,还未提交到远程仓库 git log ...

  4. (转)git checkout 撤销修改

    背景:学习git相关命令 git撤销修改和版本回退 git status查看当前仓库的状态 liuzhipeng@exdroid43:~/pad/pad-test$ git status 位于分支 m ...

  5. git 放弃本地修改

     本文以转移至本人的个人博客,请多多关注! 如果在修改时发现修改错误,而要放弃本地修改时, 一, 未使用 git add 缓存代码时. 可以使用 git checkout -- filepathnam ...

  6. git 放弃本地修改(转)

    如果在修改时发现修改错误,而要放弃本地修改时, 一, 未使用 git add 缓存代码时. 可以使用 git checkout -- filepathname (比如: git checkout -- ...

  7. git 放弃本地修改操作

      如果在修改时发现修改错误,而要放弃本地修改时, 一, 未使用 git add 缓存代码时. 可以使用 git checkout -- filepathname (比如: git checkout ...

  8. [git 学习篇] git checkout 撤销修改

    git status 查看当前创库情况 liuzhipeng@exdroid43:~/pad/pad-test$ git status 位于分支 master 您的分支与上游分支 'origin/ma ...

  9. git指令-撤销修改

    git指令-撤销修改 如果在文件中添加了错误的内容,可以撤销修改 eg: 解决: 可以删掉最后一行,手动把文件恢复到上一个版本的状态: 使用git status 你可以发现,Git会告诉你,git c ...

随机推荐

  1. Java利用PushbackReader实现返回对文本中的指定字符串之前的内容

    import java.io.FileReader; import java.io.PushbackReader; public class PushbackTest { public static ...

  2. 【神经网络与深度学习】【C/C++】使用blas做矩阵乘法

    使用blas做矩阵乘法   #define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <st ...

  3. (5.9)mysql高可用系列——正常主从切换测试

    [0]实验环境 操作系统:CentOS linux 7.5 数据库版本:5.7.24 数据库架构:主从复制,主库用于生产,从库用于数据容灾和主库备机,采用默认传统的异步复制. 主库IP:192.168 ...

  4. reids集群状态正常redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster

    重新启动redis集群时启动失败,报错: redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachab ...

  5. Minicom 简单使用

    一. 什么是minicom 1.1. minicom 是linux 下的一个串口调试工具 二. minicom的使用 2.1. 进入设置 sudo minicom -s 2.1.1. 串口设置 i. ...

  6. Html table 插入图像填充整个单元格

    把image的display属性设置为block就可以了

  7. 使用Python基于百度等OCR API的文字识别

    百度OCR Baidu OCR API:一定额度免费,目前是每日500次 Python SDK文档:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.htm ...

  8. Bicolored RBS CodeForces - 1167D (括号)

    建树, 然后高度最大值的最小值显然为$\lceil \frac{dep}{2}\rceil$, 将$>\frac{dep}{2}$的全部分出去即可. #include <sstream&g ...

  9. python网络爬虫(4)结构与基本概念

    基本模型 请求与响应 import urllib.request as urllib2 request=urllib2.Request('http://www.zhihu.com') response ...

  10. ElasticSearch工作原理与优化

    elasticsearch设计的理念就是分布式搜索引擎,底层其实还是基于lucene的,通过倒排索引的方式快速查询.比如一本书的目录是索引,然后快速找到每一章的的文本内容这种叫正向索引:而如果一件衣服 ...