Git push 撤销已经推送(push)至远端仓库的提交(commit)信息

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

下面是我本地刚刚做了一个git add, commit, push ,操作的一个例子.

为了实现这个目的,需要进行三步操作。

1 . 撤销提交信息

首先,通过git log查看提交信息,以便获取需要回退至的版本号:

$ git log
commit dc29acef9d6b51600a0b1d0b244cfefaeada307b (HEAD -> sun_master_elasticsearch, origin/sun_master_elasticsearch)
Author: yangshuqiugithub <421072757@qq.com>
Date:   Tue Aug 6 14:47:19 2019 +0800

    updated es for cms

commit ed7e243e8ef460eba3d30180f04b098ba5582393
Author: yangshuqiugithub <421072757@qq.com>
Date:   Wed Apr 17 09:45:37 2019 +0800

    create custom search engine for Magento site

commit bb841e8c443432d083c8bb4d1677396e9cb288e5
Author: Aaron Liu <shurui91@gmail.com>
Date:   Mon Apr 15 19:54:15 2019 -0700

    social media image updated

我们需要撤销”updated es for cms”这次提交,所以需要回退至的版本是”create custom search engine for Magento site”,即需要回退至的版本号是:ed7e243e8ef460eba3d30180f04b098ba5582393。

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

$ git reset --soft ed7e243e8ef460eba3d30180f04b098ba5582393

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

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

$ git log
commit ed7e243e8ef460eba3d30180f04b098ba5582393 (HEAD -> sun_master_elasticsearch)
Author: yangshuqiugithub <421072757@qq.com>
Date:   Wed Apr 17 09:45:37 2019 +0800

    create custom search engine for Magento site

已经成功撤销。

2 . 撤销

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

$ git push origin  sun_master_elasticsearch --force
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/lollicupusa/lollicupStore2.git
 + dc29acef9...ed7e243e8 sun_master_elasticsearch -> sun_master_elasticsearch (forced update)

必须添加参数force进行强制提交,否则会提交失败,并报错:

$ git push origin master
To github.com:hanchao5272/myreflect.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:hanchao5272/myreflect.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

报错原因:本地项目版本号低于远端仓库版本号。

3.修改代码,重新提交和推送

//修改代码,添加修改
git add .
//重新提交
git commit -m "CSDN-java反射06-成员变量Field-代码优化"
//重新推送
git push origin master

4. git reset –hard 使用

再使用之前,同意是先准备文件。然后git add ,commit,push

下面进行 撤销提交信息

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_elasticsearch)
$ git log
commit e2d5421d8abc7e67b899e53c994e4ec666e23094 (HEAD -> sun_master_elasticsearch, origin/sun_master_elasticsearch)
Author: yangshuqiugithub <421072757@qq.com>
Date:   Tue Aug 6 15:16:47 2019 +0800

    test git  reset hard

commit ed7e243e8ef460eba3d30180f04b098ba5582393
Author: yangshuqiugithub <421072757@qq.com>
Date:   Wed Apr 17 09:45:37 2019 +0800

    create custom search engine for Magento site

commit bb841e8c443432d083c8bb4d1677396e9cb288e5
Author: Aaron Liu <shurui91@gmail.com>
Date:   Mon Apr 15 19:54:15 2019 -0700

git reset — hard <版本号>重置至指定版本的提交,达到撤销提交的目的:

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_elasticsearch)
$  git reset --hard ed7e243e8ef460eba3d30180f04b098ba5582393
HEAD is now at ed7e243e8 create custom search engine for Magento site

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_elasticsearch)
$ git log
commit ed7e243e8ef460eba3d30180f04b098ba5582393 (HEAD -> sun_master_elasticsearch)
Author: yangshuqiugithub <421072757@qq.com>
Date:   Wed Apr 17 09:45:37 2019 +0800

    create custom search engine for Magento site

这个时候,就发现,我们在上个版本的内容就不存在了,也就是说撤销了工作区修改的内容。因此一定要谨慎使用

同时,我们也通过 git log 发现了撤销成功,版本执行了回退。

这个时候可以通过git push origin sun_master_elasticsearch –force强制提交当前版本号,以达到撤销版本号的目的:

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_elasticsearch)
$ git push origin sun_master_elasticsearch --force
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/lollicupusa/lollicupStore2.git
 + e2d5421d8...ed7e243e8 sun_master_elasticsearch -> sun_master_elasticsearch (forced update)

lollicup@DESKTOP-4M99Q31 MINGW64 /d/www/lollicupStore2 (sun_master_elasticsearch)
$ git log
commit ed7e243e8ef460eba3d30180f04b098ba5582393 (HEAD -> sun_master_elasticsearch, origin/sun_master_elasticsearch)
Author: yangshuqiugithub <421072757@qq.com>
Date:   Wed Apr 17 09:45:37 2019 +0800

    create custom search engine for Magento site

现在就可以修改代码。重新提交和推送了。

这个时候,你可以打开你的远程仓库,这个时候发现,他当前的分支版本又回到从前了。即进行了分支版本同步。

本文参考(示例验证通过):
https://my.oschina.net/u/1000241/blog/1840689/

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用*标注