WenJieWangFlyToWorld

导航

git撤销commit 并保存之前的修改

撤销并保留修改

参数 –soft

  1.  # 先进行commit ,之后后悔啦
  2.  $ git commit -am "对首篇报告研究员字段改为author_name"

执行git log

  1.  $ git log
  2.   
  3.  commit 3d6788f577faba5e1d408e372031c81beee79749
  4.  Author: yous <yous.com>
  5.  Date: Thu Dec 14 10:08:36 2017 +0800
  6.  
  7.  添加
  8.   
  9.  commit 5029f0cc08cffb77f7358de7d5534e8f8eacb82e
  10.  Author: yous <yous.com>
  11.  Date: Thu Dec 14 09:52:39 2017 +0800
  12.  
  13.  Revert "Revert "修改过程""
  14.   
  15.  This reverts commit c81f785a06804f5f40b41dedd038efbe6d83f8a8.
  16.   
  17.  commit c81f785a06804f5f40b41dedd038efbe6d83f8a8
  18.  Author: yous <yous.com>
  19.  Date: Thu Dec 14 09:52:22 2017 +0800
  20.   
  21.  Revert "修改"
  22.   
  23.  This reverts commit 5a1d18a032d8c9269613ff14593847f82043e627.
  24.   
  25.  commit 5a1d18a032d8c9269613ff14593847f82043e627

可以看出,第一个是我刚刚commit的,我要撤销,当然是选择第二个;

执行命令git reset --soft <commit>

$ git reset --soft 5029f0cc08cf
  •  之后我们查看下,状态:
  1.  $ git status
  2.  On branch yutao
  3.  Your branch is up-to-date with 'origin/yutao'.
  4.  Changes to be committed:
  5.  (use "git reset HEAD <file>..." to unstage)
  6.   
  7.  modified: dataservice/app/ggservice/v1/event/service/InfoEventService.java

可以看出已经回撤啦,并且保留了修改。

参数 –mixed

  1.  $ git reset --mixed 5029f0cc08cff
  2.  Unstaged changes after reset:
  3.  M dataservice/app/ggservice/v1/event/service/InfoEventService.java
  4.   
  5.  yutao@yutao MINGW64 /d/sts/workspace/ggservice (yutao)
  6.  $ git status
  7.  On branch yutao
  8.  Your branch is up-to-date with 'origin/yutao'.
  9.  Changes not staged for commit:
  10.  (use "git add <file>..." to update what will be committed)
  11.  (use "git checkout -- <file>..." to discard changes in working directory)
  12.  
  13.  modified: dataservice/app/ggservice/v1/event/service/InfoEventService.java
  14.   
  15.  no changes added to commit (use "git add" and/or "git commit -a")

这种方式也是可以的。 
参数--soft--mixed区别:

参数区别
--soft 会将改动放在缓存区
--mixed 不把改动放在缓存区

git reset –hard <commit_id>

这种方式,我个人是不推荐,它也是撤销,但是不会保留修改。 
除非你确实是不想要刚刚commit的内容,否则,这个操作会让你之前干的活,白干。 
所以非常不推荐这个方式。

posted on 2019-03-01 14:05  WenjieWangFlyToWorld  阅读(272)  评论(0编辑  收藏  举报