1. 程式人生 > >遠端倉庫版本回退方法

遠端倉庫版本回退方法

1 簡介

最近在使用Git時遇到了遠端分支需要版本回滾的情況,於是做了一下研究,寫下這篇部落格。

2 問題

如果提交了一個錯誤的版本,怎麼回退版本?

如果提交了一個錯誤的版本到遠端分支,怎麼回退遠端分支版本?

如果提交了一個錯誤的版本到公共遠端分支,又該怎麼回退版本?

3 本地分支版本回退的方法

如果你在本地做了錯誤提交,那麼回退版本的方法很簡單 
先用下面命令找到要回退的版本的commit id:

<code class="hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git reflog </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

接著回退版本:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">reset</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">hard</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">Obfafd</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

0bfafd就是你要回退的版本的commit id的前面幾位

4 自己的遠端分支版本回退的方法

如果你的錯誤提交已經推送到自己的遠端分支了,那麼就需要回滾遠端分支了。 
首先要回退本地分支:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">reflog</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">reset</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">hard</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">Obfafd</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

緊接著強制推送到遠端分支:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git push <span class="hljs-operator" style="box-sizing: border-box;">-f</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

注意:本地分支回滾後,版本將落後遠端分支,必須使用強制推送覆蓋遠端分支,否則無法推送到遠端分支

5 公共遠端分支版本回退的問題

看到這裡,相信你已經能夠回滾遠端分支的版本了,那麼你也許會問了,回滾公共遠端分支和回滾自己的遠端分支有區別嗎? 
答案是,當然有區別啦。

一個顯而易見的問題:如果你回退公共遠端分支,把別人的提交給丟掉了怎麼辦?

下面來分析:

假如你的遠端master分支情況是這樣的:

A1–A2–B1

其中A、B分別代表兩個人,A1、A2、B1代表各自的提交。並且所有人的本地分支都已經更新到最新版本,和遠端分支一致。

這個時候你發現A2這次提交有錯誤,你用reset回滾遠端分支master到A1,那麼理想狀態是你的隊友一拉程式碼git pull,他們的master分支也回滾了,然而現實卻是,你的隊友會看到下面的提示:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">$ git status
On branch master
Your branch is ahead <span class="hljs-operator" style="box-sizing: border-box;">of</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'origin/master'</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> commits.
  (use <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git push"</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> publish your <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> commits)
nothing <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> commit, working <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">directory</span> clean</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

也就是說,你的隊友的分支並沒有主動回退,而是比遠端分支超前了兩次提交,因為遠端分支回退了嘛。

(1) 這個時候,你大吼一聲:兄弟們,老子回退版本了。如果你的隊友都是神之隊友,比如: Tony(騰訊CTO),那麼Tony會冷靜的使用下面的命令來找出你回退版本後覆蓋掉的他的提交,也就是B1那次提交:

<code class="hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git reflog</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

然後冷靜的把自己的分支回退到那次提交,並且拉個分支:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git checkout tony_branch        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//先回到自己的分支  </span>
git reflog                      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//接著看看當前的commit id,例如:0bbbbb    </span>
git reset <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span>hard B1             <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//回到被覆蓋的那次提交B1</span>
git checkout <span class="hljs-attribute" style="box-sizing: border-box;">-b</span> tony_backup     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拉個分支,用於儲存之前因為回退版本被覆蓋掉的提交B1</span>
git checkout tony_branch        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拉完分支,迅速回到自己分支</span>
git reset <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span>hard <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>bbbbbb        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//馬上回到自己分支的最前端</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

通過上面一通敲,Tony暫時舒了一口氣,還好,B1那次提交找回來了,這時tony_backup分支最新的一次提交就是B1,接著Tony要把自己的本地master分支和遠端master分支保持一致:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">reset</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">hard</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">origin/master</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

執行了上面這條命令後,Tony的master分支才真正的回滾了,也就是說你的回滾操作才能對Tony生效,這個時候Tony的本地maser是這樣的:

A1

接著Tony要再次合併那個被丟掉的B1提交:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git checkout master            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"> //切換到master</span>
git <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">merge</span> tony_backup          <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"> //再合併一次帶有B1的分支到master</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

好了,Tony終於長舒一口氣,這個時候他的master分支是下面這樣的:

A1 – B1

終於把丟掉的B1給找回來了,接著他push一下,你一拉也能同步。

同理對於所有隊友也要這樣做,但是如果該隊友沒有提交被你丟掉,那麼他拉完程式碼git pull之後,只需要強制用遠端master覆蓋掉本地master就可以了

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">git</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">reset</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">hard</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">origin/master</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(2) 然而很不幸的是,現實中,我們經常遇到的都是豬一樣的隊友,他們一看到下面提示:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">$ git status
On branch master
Your branch is ahead <span class="hljs-operator" style="box-sizing: border-box;">of</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'origin/master'</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> commits.
  (use <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"git push"</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> publish your <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> commits)
nothing <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">to</span> commit, working <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">directory</span> clean</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

就習慣性的git push一下,或者他們直接用的SourceTree這樣的圖形介面工具,一看到介面上顯示的是推送的提示就直接點了推送按鈕,臥&槽,你辛辛苦苦回滾的版本就這樣輕鬆的被你豬一樣的隊友給還原了,所以,只要有一個隊友push之後,遠端master又變成了:

A1 – A2 – B1

這就是分散式,每個人都有副本。這個時候你連揍他的心都有了,怎麼辦呢?你不能指望每個人隊友都是git高手,下面我們用另外一種方法來回退版本。

注意:博主是在虛擬機器中實驗的,用於模擬兩個人的操作,如果你在一個機器上,用同一個賬號在不同的目錄下克隆兩份程式碼來實驗的話,回退遠端分支後,另外一個人是不會看到落後遠端分支兩次提交的,所以請務必使用虛擬機器來模擬A、B兩個人的操作

6 公共遠端分支版本回退的方法

使用git reset回退公共遠端分支的版本後,需要其他所有人手動用遠端master分支覆蓋本地master分支,顯然,這不是優雅的回退方法,下面我們使用另個一個命令來回退版本:

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git revert HEAD                     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//撤銷最近一次提交</span>
git revert HEAD~<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>                   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//撤銷上上次的提交,注意:數字從0開始</span>
git revert <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0f</span>faacc                  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//撤銷0ffaacc這次提交</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

git revert 命令意思是撤銷某次提交。它會產生一個新的提交,雖然程式碼回退了,但是版本依然是向前的,所以,當你用revert回退之後,所有人pull之後,他們的程式碼也自動的回退了。 
但是,要注意以下幾點:

  1. revert 是撤銷一次提交,所以後面的commit id是你需要回滾到的版本的前一次提交
  2. 使用revert HEAD是撤銷最近的一次提交,如果你最近一次提交是用revert命令產生的,那麼你再執行一次,就相當於撤銷了上次的撤銷操作,換句話說,你連續執行兩次revert HEAD命令,就跟沒執行是一樣的
  3. 使用revert HEAD~1 表示撤銷最近2次提交,這個數字是從0開始的,如果你之前撤銷過產生了commi id,那麼也會計算在內的。
  4. 如果使用 revert 撤銷的不是最近一次提交,那麼一定會有程式碼衝突,需要你合併程式碼,合併程式碼只需要把當前的程式碼全部去掉,保留之前版本的程式碼就可以了.

git revert 命令的好處就是不會丟掉別人的提交,即使你撤銷後覆蓋了別人的提交,他更新程式碼後,可以在本地用 reset 向前回滾,找到自己的程式碼,然後拉一下分支,再回來合併上去就可以找回被你覆蓋的提交了。

7 revert 合併程式碼,解決衝突

使用revert命令,如果不是撤銷的最近一次提交,那麼一定會有衝突,如下所示:

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><<<<<<< HEAD
全部清空
<span class="hljs-header" style="box-sizing: border-box;">第一次提交
=======</span>
全部清空
>>>>>>> parent of c24cde7... 全部清空</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

解決衝突很簡單,因為我們只想回到某次提交,因此需要把當前最新的程式碼去掉即可,也就是HEAD標記的程式碼:

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><<<<<<< HEAD
全部清空
<span class="hljs-header" style="box-sizing: border-box;">第一次提交
=======</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

把上面部分程式碼去掉就可以了,然後再提交一次程式碼就可以解決衝突了。

8 繼續擴充套件,簡單粗暴的回滾方法

看到這裡也許你已經覺得學會了遠端倉庫版本回滾方法了,但是實踐中總是會遇到很多不按套路來的問題,考慮下面一種情況:

如果你們開發中,忽然發現前面很遠的地方有一次錯誤的合併程式碼,把本來下一次才能發的功能的程式碼合併到了這一次來了,這個時候全體成員都覺得直接回滾比較快,因為他們都有備份,覆蓋了無所謂,這個時候用reset的話對隊友的要求比較高,用revert的話呢要大面積的解決衝突,也很麻煩呀,怎麼辦呢?

這個時候,可以使用簡單粗暴的辦法,直接從那個錯誤的提交的前一次拉取一份程式碼放到其他目錄,然後將master程式碼全部刪除,把那份新程式碼方進去,然後提交,果然簡單粗暴啊,雖然這種方法不入流,但是,實踐中發現很好使啊,所以,實踐是檢驗真理的唯一標準。遇到問題還是要靈活應對。 
如果你遇到問題,歡迎給我留言,我CSDN部落格———“梧桐那時雨”。

9 總結

遠端分支回滾的三種方法:

  1. 自己的分支回滾直接用reset
  2. 公共分支回滾用revert
  3. 錯的太遠了直接將程式碼全部刪掉,用正確程式碼替代