$ git logcommit a44822002522f2ab1e3a5e2a1cec00a7d3d02185 (HEAD -> master, origin/master, origin/HEAD)Author: hanchao5272 <
当前位置:   article > 正文

git撤销已经push的代码_2git 如何撤回已push的代码1.8万

2git 如何撤回已push的代码1.8万

1.撤销提交信息

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

  1. <span style="color:#000000"><code class="language-shell">$ git log
  2. commit a44822002522f2ab1e3a5e2a1cec00a7d3d02185 (HEAD -> master, origin/master, origin/HEAD)
  3. Author: hanchao5272 <<span style="color:#006666 !important">89298141</span>@qq<span style="color:#009900 !important">.com</span>>
  4. 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>
  5. CSDN-java反射<span style="color:#006666 !important">06</span>-成员变量Field-代码优化
  6. commit aa909cff2239536df14820fe086d96305b24e9f1
  7. Author: hanchao5272 <<span style="color:#006666 !important">89298141</span>@qq<span style="color:#009900 !important">.com</span>>
  8. 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>
  9. CSDN-java反射-修饰符-代码优化
  10. </code></span>
  • 我们需要撤销CSDN-java反射06-成员变量Field-代码优化这次提交,所以需要回退至的版本是CSDN-java反射-修饰符-代码优化,即需要回退至的版本号是:aa909cff2239536df14820fe086d96305b24e9f1。

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

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

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

  1. <span style="color:#000000"><code class="language-shell">$ git <span style="color:#000088 !important">log</span>
  2. commit aa909cff2239536df14820fe086d96305b24e9f1 (HEAD <span style="color:#4f4f4f !important">-> </span>master)
  3. 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>
  4. <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>
  5. CSDN<span style="color:#98c379">-java</span>反射<span style="color:#4f4f4f !important">-</span>修饰符<span style="color:#4f4f4f !important">-</span>代码优化
  6. </code></span>

已经成功撤销。

2.撤销

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

  1. <span style="color:#000000"><code class="language-shell">$ git <span style="color:#000088 !important">push</span> origin master --force
  2. 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>)
  3. To github<span style="color:#009900 !important">.com</span>:hanchao5272/myreflect<span style="color:#009900 !important">.git</span>
  4. + a448220..<span style="color:#009900 !important">.aa</span>909cf master -> master (forced update)
  5. </code></span>

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

 

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/729274
推荐阅读
相关标签