NutzSite

Java快速开发框架


git 远程分支回滚

<p>git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id</p> <p>【本地代码库回滚】:</p> <p>git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除</p> <p>git reset --hard HEAD~3:将最近3次的提交回滚</p> <p>【远程代码库回滚】:</p> <p>这个是重点要说的内容,过程比本地回滚要复杂</p> <p>应用场景:自动部署系统发布后发现问题,需要回滚到某一个commit,再重新发布</p> <p>原理:先将本地分支退回到某个commit,删除远程分支,再重新push本地分支</p> <p>操作步骤:</p> <ol> <li>git checkout the_branch </li> <li>git pull </li> <li>git branch the_branch_backup //备份一下这个分支当前的情况 </li> <li>git reset --hard the_commit_id //把the_branch本地回滚到the_commit_id </li> <li>git push origin :the_branch //删除远程 the_branch </li> <li>git push origin the_branch //用回滚后的本地分支重新建立远程分支 </li> <li>git push origin :the_branch_backup //如果前面都成功了,删除这个备份分支</li> </ol> <p>如果使用了gerrit做远程代码中心库和code review平台,需要确保操作git的用户具备分支的push权限,并且选择了 Force Push选项(在push权限设置里有这个选项)</p> <p>另外,gerrit中心库是个bare库,将HEAD默认指向了master,因此master分支是不能进行删除操作的,最好不要选择删除master分支的策略,换用其他分支。如果一定要这样做,可以考虑到gerrit服务器上修改HEAD指针。。。不建议这样搞</p> <h2>方法一:</h2> <p>1、新建backup分支 作为备份,以防万一 </p> <pre><code>git branch backup</code></pre> <p>2、将本地的backup分支 推送到远程的backup </p> <pre><code>git push origin backup:backup</code></pre> <p>3、本地仓库彻底回退到xxxxx版本,xxxxx版本之后的commit信息将丢失</p> <pre><code>git reset --hard xxxxx</code></pre> <p>4、删除远程的master分支 (注意master前有个:) </p> <pre><code>git push origin :master</code></pre> <p>主要远程仓库的master如果是保护分支将报错,请去掉对分支的保护设置:<br /> remote: GitLab: You are allowed to deleted protected branches from this project. To <a href="http://gitlab.mogujie.org/shihao/afanty.git"><a href="http://gitlab.mogujie.org/shihao/afanty.git">http://gitlab.mogujie.org/shihao/afanty.git</a></a> ! [remote rejected] master (pre-receive hook declined) error: failed to push some refs to '<a href="http://gitlab.mogujie.org/xxxx/xxxx.git'"><a href="http://gitlab.mogujie.org/xxxx/xxxx.git">http://gitlab.mogujie.org/xxxx/xxxx.git</a>'</a></p> <p>5、重新创建远程master分支(这跟第1次提交本地代码库给远程仓库的命令一样) </p> <pre><code>git push origin master</code></pre> <h2>方法二:</h2> <p>1、本地代码回滚到上一版本(或者指定版本) </p> <pre><code>git reset --hard HEAD~1</code></pre> <p>2、加入-f参数,强制提交,远程端将强制跟新到reset版本 </p> <pre><code>git push -f</code></pre> <p><a href="https://www.jianshu.com/p/efaad905258c">https://www.jianshu.com/p/efaad905258c</a></p>

页面列表

ITEM_HTML