<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 版本回退 現在,你已經學會了修改文件,然后把修改提交到Git版本庫,現在,再練習一次,修改readme.txt文件如下: ``` Git is a distributed version control system. Git is free software distributed under the GPL. ``` 然后嘗試提交: ``` $ git add readme.txt $ git commit -m "append GPL" [master 3628164] append GPL 1 file changed, 1 insertion(+), 1 deletion(-) ``` 像這樣,你不斷對文件進行修改,然后不斷提交修改到版本庫里,就好比玩RPG游戲時,每通過一關就會自動把游戲狀態存盤,如果某一關沒過去,你還可以選擇讀取前一關的狀態。有些時候,在打Boss之前,你會手動存盤,以便萬一打Boss失敗了,可以從最近的地方重新開始。Git也是一樣,每當你覺得文件修改到一定程度的時候,就可以“保存一個快照”,這個快照在Git中被稱為`commit`。一旦你把文件改亂了,或者誤刪了文件,還可以從最近的一個`commit`恢復,然后繼續工作,而不是把幾個月的工作成果全部丟失。 現在,我們回顧一下readme.txt文件一共有幾個版本被提交到Git倉庫里了: 版本1:wrote a readme file ``` Git is a version control system. Git is free software. ``` 版本2:add distributed ``` Git is a distributed version control system. Git is free software. ``` 版本3:append GPL ``` Git is a distributed version control system. Git is free software distributed under the GPL. ``` 當然了,在實際工作中,我們腦子里怎么可能記得一個幾千行的文件每次都改了什么內容,不然要版本控制系統干什么。版本控制系統肯定有某個命令可以告訴我們歷史記錄,在Git中,我們用`git log`命令查看: ``` $ git log commit 3628164fb26d48395383f8f31179f24e0882e1e0 Author: Michael Liao <askxuefeng@gmail.com> Date: Tue Aug 20 15:11:49 2013 +0800 append GPL commit ea34578d5496d7dd233c827ed32a8cd576c5ee85 Author: Michael Liao <askxuefeng@gmail.com> Date: Tue Aug 20 14:53:12 2013 +0800 add distributed commit cb926e7ea50ad11b8f9e909c05226233bf755030 Author: Michael Liao <askxuefeng@gmail.com> Date: Mon Aug 19 17:51:55 2013 +0800 wrote a readme file ``` `git log`命令顯示從最近到最遠的提交日志,我們可以看到3次提交,最近的一次是`append GPL`,上一次是`add distributed`,最早的一次是`wrote a readme file`。 如果嫌輸出信息太多,看得眼花繚亂的,可以試試加上`--pretty=oneline`參數: ``` $ git log --pretty=oneline 3628164fb26d48395383f8f31179f24e0882e1e0 append GPL ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file ``` 需要友情提示的是,你看到的一大串類似`3628164...882e1e0`的是`commit id`(版本號),和SVN不一樣,Git的`commit id`不是1,2,3……遞增的數字,而是一個SHA1計算出來的一個非常大的數字,用十六進制表示,而且你看到的`commit id`和我的肯定不一樣,以你自己的為準。為什么`commit id`需要用這么一大串數字表示呢?因為Git是分布式的版本控制系統,后面我們還要研究多人在同一個版本庫里工作,如果大家都用1,2,3……作為版本號,那肯定就沖突了。 每提交一個新版本,實際上Git就會把它們自動串成一條時間線。如果使用可視化工具查看Git歷史,就可以更清楚地看到提交歷史的時間線: ![git-log-timeline](img/001384907545599be4a60a0b5044447b47c8d8b805a25d2000.png) 好了,現在我們啟動時光穿梭機,準備把readme.txt回退到上一個版本,也就是“add distributed”的那個版本,怎么做呢? 首先,Git必須知道當前版本是哪個版本,在Git中,用`HEAD`表示當前版本,也就是最新的提交`3628164...882e1e0`(注意我的提交ID和你的肯定不一樣),上一個版本就是`HEAD^`,上上一個版本就是`HEAD^^`,當然往上100個版本寫100個`^`比較容易數不過來,所以寫成`HEAD~100`。 現在,我們要把當前版本“append GPL”回退到上一個版本“add distributed”,就可以使用`git reset`命令: ``` $ git reset --hard HEAD^ HEAD is now at ea34578 add distributed ``` `--hard`參數有啥意義?這個后面再講,現在你先放心使用。 看看readme.txt的內容是不是版本`add distributed`: ``` $ cat readme.txt Git is a distributed version control system. Git is free software. ``` 果然。 還可以繼續回退到上一個版本`wrote a readme file`,不過且慢,然我們用`git log`再看看現在版本庫的狀態: ``` $ git log commit ea34578d5496d7dd233c827ed32a8cd576c5ee85 Author: Michael Liao <askxuefeng@gmail.com> Date: Tue Aug 20 14:53:12 2013 +0800 add distributed commit cb926e7ea50ad11b8f9e909c05226233bf755030 Author: Michael Liao <askxuefeng@gmail.com> Date: Mon Aug 19 17:51:55 2013 +0800 wrote a readme file ``` 最新的那個版本`append GPL`已經看不到了!好比你從21世紀坐時光穿梭機來到了19世紀,想再回去已經回不去了,腫么辦? 辦法其實還是有的,只要上面的命令行窗口還沒有被關掉,你就可以順著往上找啊找啊,找到那個`append GPL`的`commit id`是`3628164...`,于是就可以指定回到未來的某個版本: ``` $ git reset --hard 3628164 HEAD is now at 3628164 append GPL ``` 版本號沒必要寫全,前幾位就可以了,Git會自動去找。當然也不能只寫前一兩位,因為Git可能會找到多個版本號,就無法確定是哪一個了。 再小心翼翼地看看readme.txt的內容: ``` $ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL. ``` 果然,我胡漢三又回來了。 Git的版本回退速度非常快,因為Git在內部有個指向當前版本的`HEAD`指針,當你回退版本的時候,Git僅僅是把HEAD從指向`append GPL`: ![git-head](img/001384907584977fc9d4b96c99f4b5f8e448fbd8589d0b2000.png) 改為指向`add distributed`: ![git-head-move](img/001384907594057a873c79f14184b45a1a66b1509f90b7a000.png) 然后順便把工作區的文件更新了。所以你讓`HEAD`指向哪個版本號,你就把當前版本定位在哪。 http://michaelliao.gitcafe.io/video/git-reset.mp4 現在,你回退到了某個版本,關掉了電腦,第二天早上就后悔了,想恢復到新版本怎么辦?找不到新版本的`commit id`怎么辦? 在Git中,總是有后悔藥可以吃的。當你用`$ git reset --hard HEAD^`回退到`add distributed`版本時,再想恢復到`append GPL`,就必須找到`append GPL`的commit id。Git提供了一個命令`git reflog`用來記錄你的每一次命令: ``` $ git reflog ea34578 HEAD@{0}: reset: moving to HEAD^ 3628164 HEAD@{1}: commit: append GPL ea34578 HEAD@{2}: commit: add distributed cb926e7 HEAD@{3}: commit (initial): wrote a readme file ``` 終于舒了口氣,第二行顯示`append GPL`的commit id是`3628164`,現在,你又可以乘坐時光機回到未來了。 http://michaelliao.gitcafe.io/video/git-reflog-reset.mp4 ## 小結 現在總結一下: * `HEAD`指向的版本就是當前版本,因此,Git允許我們在版本的歷史之間穿梭,使用命令`git reset --hard commit_id`。 * 穿梭前,用`git log`可以查看提交歷史,以便確定要回退到哪個版本。 * 要重返未來,用`git reflog`查看命令歷史,以便確定要回到未來的哪個版本。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看