天天看点

Git回滚远程版本

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

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

Git回滚远程版本

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

1

2

3

4

5

6

<code>vim a.txt</code>

<code>git add .</code>

<code>git commit -a -m</code><code>"add a.txt"</code>

<code>git push</code>

<code>rm</code> <code>a.txt</code>

<code>git commit -a -</code>

no pic say 个 78。。。

Git回滚远程版本

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

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

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

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

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

talk is cheap,show me the code or money~ 代码如下:

<code>git log</code>

<code>git reset --soft ${commit-</code><code>id</code><code>}</code>

<code>git stash</code>

<code>git push -f</code>

详解如下:

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

7

8

<code>commit 84686b426c3a8a3d569ae56b6788278c10b27e5b</code>

<code>author: jeffli1993 &lt;[email protected]&gt;</code>

<code>date:   fri apr 8 19:11:32 2016 +0800</code>

<code>   </code><code>我删除了老板的东西</code>

<code>commit 72bd6304c3c6e1cb7034114db1dd1b8376a6283a</code>

<code>date:   fri apr 8 19:05:23 2016 +0800</code>

<code>   </code><code>add a.txt</code>

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

第2行,输入对应版本即可:

<code>git reset --soft 72bd6304c3c6e1cb7034114db1dd1b8376a6283a</code>

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

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

第4行,覆盖 -f,对

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

<code>1. git reset 回滚到某个版本之前</code>

<code>2. git push -f 强制push覆盖</code>

下一篇: Git 备忘录