Git撤销提交commit

2022-08-04 12:16:55
      有时,在git push之后,才发现还有一些代码需要进行很小的改动,这些改动在原则上不应该作为一次新的提交。 
  这时,我们需要撤销这次推送(git push)与提交(git commit),然后进行代码修改,再重新进行提交和推送。

1.撤销提交信息

首先,通过git log -n 3 --stat查看最近三次的提交信息,以便获取需要回退至的版本号:

$ git log-n3--stat
commit4589cee66d69aa6de1a4e8e0b21dd1a392e89823(HEAD-> master, origin/master)
Author: Fuyj<2312696405@qq.com>
Date:   Wed Jul1516:58:222020+0800

    关注作者完成

commit a54d5553ef92679779b0287af61130fb4479f08f
Author: Fuyj<2312696405@qq.com>
Date:   Wed Jul1516:05:312020+0800

    详情页面完成

commit3c21b32406a688f72031b2b1f6e37f7c31a83a0e
Author: Fuyj<2312696405@qq.com>
Date:   Wed Jul1514:40:042020+0800

    修复前端传值问题,增添点赞,收藏文章功能~

我们需要撤销关注作者完成这次提交,所以需要回退至的版本是详情页面完成,即需要回退至的版本号是:a54d5553ef92679779b0287af61130fb4479f08f

2.然后,通过git reset --soft <版本号>重置至指定版本的提交,达到撤销提交的目的:

$ git reset--soft a54d5553ef92679779b0287af61130fb4479f08f

参数soft指的是:保留当前工作区,以便重新提交
还可以选择参数hard,会撤销相应工作区的修改,一定要谨慎使用

然后,通过git log确认是否成功撤销:

$ git reset--soft a54d5553ef92679779b0287af61130fb4479f08f
$ git log
commita54d5553ef92679779b0287af61130fb4479f08f(HEAD-> master)
Author: Fuyj<2312696405@qq.com>
Date:   Wed Jul1516:05:312020+0800

    详情页面完成

commit3c21b32406a688f72031b2b1f6e37f7c31a83a0e
Author: Fuyj<2312696405@qq.com>
Date:   Wed Jul1514:40:042020+0800

    修复前端传值问题,增添点赞,收藏文章功能

commit e4a2f8c9e4f2d4f68b63b07e6e400a2448543ea5
Author: Fuyj<2312696405@qq.com>

这时候已经成功撤销

3.通过git push origin master –force强制提交当前版本号,以达到撤销版本号的目的:

$ git push origin master--force
Total0(delta0), reused0(delta0)
To github.com:LBJFYJ232323/imooc-news-fyj.git+4589cee...a54d555 master->master(forced update)

4.修改代码,重新提交至远程库

$ git add.
warning:LF will be replaced byCRLFin components/like/like.vue.
The file will have its original line endingsin your working directory
warning:LF will be replaced byCRLFin components/list-card/list-card.vue.
The file will have its original line endingsin your working directory
warning:LF will be replaced byCRLFin components/list/list.vue.
The file will have its original line endingsin your working directory
warning:LF will be replaced byCRLFin pages/home-detail/home-detail.vue.
The file will have its original line endingsin your working directory
warning:LF will be replaced byCRLFin pages/tabbar/follow/follow.vue.
The file will have its original line endingsin your working directory

$ git commit-m"关注文章完成"[master835e8f8] 关注文章完成10 files changed,168insertions(+),15deletions(-)
 create mode100644 cloudfunctions-aliyun/get_follow/get_follow.param.json
 create mode100644 cloudfunctions-aliyun/get_follow/index.js

$ git push origin master

至此我们已经修改完成
本文均为原创,如有侵权,请联系删除。

  • 作者:Fuyj
  • 原文链接:https://blog.csdn.net/qq_45090740/article/details/107366354
    更新时间:2022-08-04 12:16:55