git撤销已经push的代码

本文详细介绍了如何使用Git撤销本地提交并回退到指定版本,以及如何通过git push origin master --force强制推送更新到远程仓库,覆盖原有版本。在操作中,重点讲解了git log用于查看提交历史,git reset --soft用于撤销提交,以及git push带force参数的使用,强调了在使用force参数时需谨慎,以防丢失工作区信息。
摘要由CSDN通过智能技术生成

1.撤销提交信息

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

<span style="color:#000000"><code class="language-shell">$ git log
commit a44822002522f2ab1e3a5e2a1cec00a7d3d02185 (HEAD -> master, origin/master, origin/HEAD)
Author: hanchao5272 <<span style="color:#006666 !important">89298141</span>@qq<span style="color:#009900 !important">.com</span>>
Date:   Sun Mar <span style="color:#006666 !important">4</span> <span style="color:#006666 !important">11</span>:<span style="color:#006666 !important">14</span>:<span style="color:#006666 !important">55</span> <span style="color:#006666 !important">2018</span> +<span style="color:#006666 !important">0800</span>

    CSDN-java反射<span style="color:#006666 !important">06</span>-成员变量Field-代码优化

commit aa909cff2239536df14820fe086d96305b24e9f1
Author: hanchao5272 <<span style="color:#006666 !important">89298141</span>@qq<span style="color:#009900 !important">.com</span>>
Date:   Sat Mar <span style="color:#006666 !important">3</span> <span style="color:#006666 !important">23</span>:<span style="color:#006666 !important">43</span>:<span style="color:#006666 !important">03</span> <span style="color:#006666 !important">2018</span> +<span style="color:#006666 !important">0800</span>

    CSDN-java反射-修饰符-代码优化
</code></span>
  • 我们需要撤销CSDN-java反射06-成员变量Field-代码优化这次提交,所以需要回退至的版本是CSDN-java反射-修饰符-代码优化,即需要回退至的版本号是:aa909cff2239536df14820fe086d96305b24e9f1。

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

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

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

<span style="color:#000000"><code class="language-shell">$ git <span style="color:#000088 !important">log</span>
commit aa909cff2239536df14820fe086d96305b24e9f1 (HEAD <span style="color:#4f4f4f !important">-> </span>master)
Author: hanchao5272 <span style="color:#4f4f4f !important"><</span><span style="color:#006666 !important">89298141</span>@qq<span style="color:#4f4f4f !important">.</span>com<span style="color:#4f4f4f !important">></span>
<span style="color:#4f4f4f !important">Date</span>:   Sat Mar <span style="color:#006666 !important">3</span> <span style="color:#006666 !important">23</span>:<span style="color:#006666 !important">43</span>:<span style="color:#006666 !important">03</span> <span style="color:#006666 !important">2018</span> <span style="color:#4f4f4f !important">+</span><span style="color:#006666 !important">0800</span>

    CSDN<span style="color:#98c379">-java</span>反射<span style="color:#4f4f4f !important">-</span>修饰符<span style="color:#4f4f4f !important">-</span>代码优化
</code></span>

已经成功撤销。

2.撤销

通过git push origin master –force强制提交当前版本号,master是什么分支就写什么分支这里以master实力以达到撤销版本号的目的:

<span style="color:#000000"><code class="language-shell">$ git <span style="color:#000088 !important">push</span> origin  master --force
Total <span style="color:#006666 !important">0</span> (delta <span style="color:#006666 !important">0</span>), reused <span style="color:#006666 !important">0</span> (delta <span style="color:#006666 !important">0</span>)
To github<span style="color:#009900 !important">.com</span>:hanchao5272/myreflect<span style="color:#009900 !important">.git</span>
 + a448220..<span style="color:#009900 !important">.aa</span>909cf master -> master (forced update)
</code></span>

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

 

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值