转载

每日一搏 | Git 回滚远程版本

摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!

“房子是租的 但生活不是”

1.故事的开始

远程master分支下代码被不小心提交了很多垃圾代码或项目删掉,想要回滚到以前的某一版本并删除commit log。怎么办?情景如图:

每日一搏 | Git 回滚远程版本

情景很简单。老板上传了个文件,我把他删掉了。有一种办法,把文件再push下,但是也不想他看到图中那comment(ps:这样我才不会被fire)。实现上面场景的代码如下:

vim A.txt git add . git commit -a -m "add A.txt" git push rm A.txt git commit -a -m "我删除了老板的东西" git push 

————–分割线 ————–

一头雾水的小白怎么办?怎么办?怎么办?

push到远程的提交默认是不能修改的,但是一定要修改不是不行:

git push -f 

2.解决之道

2.1工作区,暂存区,本地版本库 & 远程版本库

No pic say 个 78。。。

每日一搏 | Git 回滚远程版本

》》工作区:就是我们操作的目录

》》暂存区:操作目录的快照

》》本地版本库:Git的精髓,人人都是中央仓库。也就是Git分布式的好处,自然对比SVN这种集中式

》》远程版本库:Github这种中央仓库,可以达到共享。

常用的操作也如图所示,不言而喻了。

2.2 实战解决

Talk is cheap,Show me the code or money~ 代码如下:

git log git reset --soft ${commit-id} git stash git push -f 

详解如下:

第1行: git log 查看提交历史,然后找到要回滚的版本 。历史如下,

  1. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">commit&nbsp;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">84686b426c3a8a3d569ae56b6788278c10b27e5b</span>

  2. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Author</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">JeffLi1993</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&lt;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">qiangqiangli1993<a href="http://my.oschina.net/gmaiman" target="_blank">@gmail</a> </span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">.</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">com</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&gt;</span>

  3. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Date</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&nbsp;&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Fri</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Apr</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">8</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">19</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">11</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">32</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">2016</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">+</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">0800</span>

  4. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&nbsp; &nbsp;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">我删除了老板的东西</span>

  5. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">commit&nbsp;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">72bd6304c3c6e1cb7034114db1dd1b8376a6283a</span>

  6. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Author</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">JeffLi1993</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&lt;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">qiangqiangli1993<a href="http://my.oschina.net/gmaiman" target="_blank">@gmail</a> </span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">.</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">com</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&gt;</span>

  7. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Date</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&nbsp;&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Fri</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">Apr</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">8</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">19</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">05</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">:</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">23</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">2016</span>&nbsp;<span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">+</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">0800</span>

  8. <span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">&nbsp; &nbsp;add A</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">.</span><span style="margin: 0px; padding: 0px; list-style: none; box-sizing: border-box;">txt</span>

我们想要回滚到的版本就是:72bd6304c3c6e1cb7034114db1dd1b8376a6283a

第2行:git reset –soft 72bd6304c3c6e1cb7034114db1dd1b8376a6283a

撤销到某个版本之前,之前的修改退回到暂存区(不懂看漂亮的图哦~)。soft 和 hard参数的区别就是,hard修改记录都没了,soft则会保留修改记录。

第3行:暂存为了安全起见。

第4行:git push -f

将本地master push 到远程版本库中,  -f 强制覆盖

3. 小结

git reset 回滚到某个版本之前git push -f 强制push覆盖

欢迎点击我的博客及GitHub — 博客提供RSS订阅哦!

———-  http://www.bysocket.com/ ————-  https://github.com/JeffLi1993 ———-

微         博:BYSocket豆         瓣: BYSocket FaceBook: BYSocket Twitter    : BYSocket

原文  http://my.oschina.net/jeffli1993/blog/655458?fromerr=GB29Mq87
正文到此结束
Loading...