主頁 > 軟體工程 > GIT回到特定的提交id而不洗掉歷史

GIT回到特定的提交id而不洗掉歷史

2022-03-02 02:51:21 軟體工程

這是我的提交日志,我想切換回特定的提交 id(例如 Second),當我使用 git checkout 時,沒關系,但是,我不再能夠切換回最后一次提交(第四)。

HEAD 指向第二次提交,然后當我記錄我的提交時沒有任何內容。

如何在不洗掉歷史記錄的情況下在提交之間切換?

commit 61c71a9e5a6d9e29a4172e687172dd4b8523eb4a (HEAD -> main)
Author: mohhhe <[email protected]>
Date:   Fri Feb 25 19:08:36 2022  0330

    Fourth

commit 9c3e8919cfa2c970f14056eef34ca12b49025f65
Author: mohhhe <[email protected]>
Date:   Fri Feb 25 19:08:13 2022  0330

    Third

commit d33795596001197f382038a72d20faf0cfbe7ab7
Author: mohhhe <[email protected]>
Date:   Fri Feb 25 19:07:55 2022  0330

    Second

commit 2fe7b1d8270fcfb41d73e69293da10734e37b069
Author: mohhhe <[email protected]>
Date:   Fri Feb 25 19:07:39 2022  0330

    First

uj5u.com熱心網友回復:

小巷類比

想象一下,你在一個大城市的一條狹窄街道或小巷的入口處,周圍是摩天大樓。順著小巷往下看,你可以看到一連串的垃圾箱。現在,沿著小巷走到一半,向前看。一半的垃圾箱不見了!他們去哪兒了?無處:他們就在你身后。

同樣的想法也適用于這里:Git 沒有洗掉你看不到的提交。你只是看不到他們。回到可以看到它們的有利位置,您將再次看到它們。

現實,如它所是

在 Git 中,提交是一個由兩部分組成的物體:它包含所有檔案的快照——好吧,是 Git 知道的所有檔案,在你(或任何人)制作該快照的時間——以及一些元資料。每個提交都有編號,帶有一個大的、丑陋的、看起來隨機的哈希 ID,如61c71a9e5a6d9e29a4172e687172dd4b8523eb4a您的輸出所示。

哈希 ID 是 Git查找提交所需的。提交本身存盤為一堆部分,使用提交物件和其他內部支持物件。您在此處看到的哈希 ID 是提交物件本身的哈希 ID,它僅包含元資料:快照位于物件中,該物件還有更多子物件。但是您通常不需要知道這一點;需要知道的是 Git 有一個很大的資料庫來保存它的所有物件,每個物件都有編號,而且 Git 本身需要編號來檢索物件。1

然而,人類在數字方面非常糟糕。那四個哈希 ID 又是什么?無論如何,不??值得記住它們。Git 為您提供了一種非常快速的方法來查找四個哈希 ID 中的一個: name main,您很容易記住,它可以找到其中一個哈希 ID。

隨著時間的推移,main找到的一個哈希 ID 可能會發生變化,但現在,它會61c71a9e5a6d9e29a4172e687172dd4b8523eb4a為您和 Git 找到。該提交是分支上的最新main提交。這是根據定義,因為名稱main包含該 ID。因此,如果您希望 Git 在 上找到最新的提交main,您可以簡單地向 Git 詢問main,Git 會查找名稱main并找到該 ID,從而找到該提交。

如果以及當您進行的提交時,這就是 Git 將執行的操作(以某種順序或其他順序;您并沒有真正了解這是否有任何特定的順序):

  • 制作 Git 知道的每個檔案的快照。要讓 Git 看到你對 Git已經知道的檔案所做的任何更新,你必須在它上面運行。要讓 Git 看到您創建的任何新檔案,但直到現在還不存在,您必須在它上面運行。它還有很多東西,但這是你必須繼續運行的原因的第一個近似值:告訴 Git新快照應該使用新的或更新的檔案git addgit addgit add

  • 收集一堆元資料。Git 將收集的元資料包括您的姓名(在您的設定中user.name設定)和電子郵件地址(來自您的user.email設定)。它包括精確到秒的當前日期和時間。并且,它包括當前最新的提交,不管是什么,在你所在的分支上——在這種情況下main

Git 將所有這些寫出來以進行的提交,該提交獲得一個新的、唯一的、以前從未使用過、永遠不會再次使用的哈希 ID。此哈希 ID 絕不能出現在任何Git 存盤庫中,除非用于標識您剛剛進行的此提交。(這就是為什么哈希 ID 如此之大和丑陋:所以它們可以是唯一的。)

Git then stores the new commit's hash ID in the current branch name. So now the name main selects your new commit—the one you just made.


1That's because this big database is a key-value store, with the hash IDs being the keys. There's a slow method of walking the entire database and getting every <key, value> pair, but this takes many seconds, or even minutes, in a big repository: far too slow to be useful. A key lookup takes milliseconds, so that's what you want Git to be doing.


Commits thus form backwards-looking chains

What this all means is that the name main automatically and always selects the last commit in the branch named main. By definition, main is the end of the street / alleyway / superhighway / motorway / whatever it is. You add new commits by making new commits while you're on that "road", and that extends the "road" a bit further.

Another way to show this is to draw the commits using uppercase letters to stand in for the real hash IDs. Here, we have your original four commits, which we'll call A, B, C, and D for short:

A <-B <-C <-D   <--main

The name main will "point to" (contain the hash ID of) the last of these commits, commit D. Commit D has a snapshot—a copy of all the files, frozen for all time—and some metadata, and D's metadata says that the previous commit is commit C. We say that D points to C.

Commit C, of course, has a snapshot and metadata. The snapshot holds the files that Git knew about at the time you made C, frozen for all time, and the metadata holds the date-and-time and so on, including the hash ID of earlier commit B. We say that C points to B.

Commit B holds a snapshot and metadata too, and points backwards to commit A, which holds a snapshot and metadata. But commit A was the very first commit you made, in what had been, up until you made A, a totally-empty repository. So commit A doesn't point further backwards: it can't.

That's how your four commits are, in your repository. They can never change! They are completely read-only, and those four hash IDs are now used up forever.2 The name main points to the last one—until you make a new commit. Then new commit E springs into being, pointing backwards to D, and Git updates the name main to point to E:

A <-B <-C <-D <-E   <--main

2This is technically impossible, and Git doesn't really try to prevent anyone else from getting the same hash ID except by using cryptographic trickery to make it so unlikely that we don't have to worry about it. Nobody will accidentally re-use your hash IDs. The crypto makes it hard to do it on purpose, too.


Driving back into the past

But what happens when you want to visit an old commit? You ran:

git checkout d33795596001197f382038a72d20faf0cfbe7ab7

to tell Git to erase, from your work area, all the files that are safely stored forever in commit D, and go back to commit B: extract the stored-forever files from commit B into your work area. Git did that, and then git log showed you commits B and A and stopped. Why?

Git uses your HEAD to be able to see things

Git has a very special name, HEAD, that is not a branch name at all.3 Instead, this name HEAD is normally attached to a branch name. That's what your first git log shows:

commit 61c71a9e5a6d9e29a4172e687172dd4b8523eb4a (HEAD -> main)

Git has the name HEAD "pointing to" the name main here. I like to draw it this way instead:

A--B--C--D   <-- main (HEAD)

with the name HEAD "attached to" the name main. (I also got lazy about drawing the arrows between commits. Just remember that the connecting lines, from A to B to C to D, are really backwards-pointing arrows.)

Running git log tells Git: First, use HEAD to find a commit. Since HEAD is attached to main, Git uses main to find commit D. The git log command then shows you commit D—well, shows it by default; there are options you can give git log to change this—and then follows D's arrow back to C and shows C. Then git log follows C's arrow to B, and shows B, and follows B's arrow to A and shows A. Commit A has no backwards arrow, so git log can finally stop.

When you git checkout a commit by its hash ID, however, Git goes into what Git calls detached HEAD mode. Here, the name HEAD is no longer attached to a branch name. Instead, it points directly to a commit. If you choose commit B, you get this:

A--B   <-- HEAD
    \
     C--D   <-- main

The git log command works as before: it uses HEAD to find a commit. But this time HEAD finds commit B, not name main and then commit D. So git log shows B, and follows B's arrow back to A and shows A, and then runs out of commits to show and stops.

If you want to see all your commits, you can:

git checkout main

which switches back to branch main, re-attaching your HEAD:

A--B--C--D   <-- main (HEAD)

and now you're starting git log from the end of the road—the last commit on main—and you'll see all four commits. Or, you can run:

git log main

which tells git log that it should use the name main to look up the commit to start with. This will find commit D, even though HEAD is still pointing directly to commit B.


3It's technically possible to create a branch named HEAD. Don't do it.


More than one branch name

Once you understand the above, you're ready to handle multiple branch names. Suppose we have this:

A--B--C--D   <-- main (HEAD)

and we create a new name, such as develop, pointing to commit D, by running:

git branch develop

We now have this:

A--B--C--D   <-- develop, main (HEAD)

That is, both names, develop and main, point to commit D. The special name HEAD is currently attached to the name main though. Let's make a new commit on main, commit E, and draw it in:

           E   <-- main (HEAD)
          /
A--B--C--D   <-- develop

Commit E is now the latest commit on main, while commit D continues to be the latest commit on develop.

If you now run:

git checkout develop

or:

git switch develop

to switch to branch develop, we get:

           E   <-- main
          /
A--B--C--D   <-- develop (HEAD)

Commit E still exists, but Git will take all of E's files out of our work area, and put in all of D's files instead. The name HEAD is now attached to the name develop, not the name main, so git log will show commits D, C, B, and A and then stop. Running git log main will show E, then D, then C, and so on.

Note that commits up through D are on both branches. But now that we're on develop instead of main, let's make another new commit:

           E   <-- main
          /
A--B--C--D
          \
           F   <-- develop (HEAD)

Commits A through D are still on both branches, but now main and develop each have one commit that the other branch doesn't have. The two names pick the latest commits, which are E and F. E is the latest main-branch commit and F is the latest develop-branch commit. They're both "the latest commit"! If we make another new commit on develop, like this:

           E   <-- main
          /
A--B--C--D
          \
           F--G   <-- develop (HEAD)

then the two latest commits are now E and G. Each branch name "means" that particular commit, which is by definition the latest commit on that branch. Moreover, all the commits you (or Git) can find by starting at that "latest" commit, and working backwards, are "on" that branch. So when we have:

          I--J   <-- br1
         /
...--G--H   <-- main
         \
          K--L   <-- br2

we have three latest commits, and commits up through H are on all three branches. Pick one name to check out, and that's the set of commits you'll see with git log; the files in your work area will be those from that latest—or tip—commit.

Note that the commits never change: once you make a commit, it is good forever. However, we find commits through branch names, and those do move about. If we take the last example and move the name br2 back one hop:

          I--J   <-- br1
         /
...--G--H   <-- main
         \
          K   <-- br2
           \
            L   ???

we may never be able to find commit L again. It has become "lost", as there's no way to recover its hash ID. As long as we can find J and K, though, we can't lose H, even if we completely delete the name main. Deleting that name just means we no longer have direct access to commit H: we have to find it by working back one step from K, or two from J.

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/435168.html

標籤:混帐

上一篇:在單個檔案上使用gitdiff顯示重命名/移動狀態

下一篇:PR從release到main有沖突

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • Git本地庫既關聯GitHub又關聯Gitee

    創建代碼倉庫 使用gitee舉例(github和gitee差不多) 1.在gitee右上角點擊+,選擇新建倉庫 ? 2.選擇填寫倉庫資訊,然后進行創建 ? 3.服務端已經準備好了,本地開始作準備 (1)Git 全域設定 git config --global user.name "成鈺" git c ......

    uj5u.com 2020-09-10 05:04:14 more
  • CODING DevOps 代碼質量實戰系列第二課,相約周三

    隨著 ToB(企業服務)的興起和 ToC(消費互聯網)產品進入成熟期,線上故障帶來的損失越來越大,代碼質量越來越重要,而「質量內建」正是 DevOps 核心理念之一。**《DevOps 代碼質量實戰(PHP 版)》**為 CODING DevOps 代碼質量實戰系列的第二課,同時也是本系列的 PHP ......

    uj5u.com 2020-09-10 05:07:43 more
  • 推薦Scrum書籍

    推薦Scrum書籍 直接上干貨,推薦書籍清單如下(推薦有順序的哦) Scrum指南 Scrum精髓 Scrum敏捷軟體開發 Scrum捷徑 硝煙中的Scrum和XP : 我們如何實施Scrum 敏捷軟體開發:Scrum實戰指南 Scrum要素 大規模Scrum:大規模敏捷組織的設計 用戶故事地圖 用 ......

    uj5u.com 2020-09-10 05:07:45 more
  • CODING DevOps 代碼質量實戰系列最后一課,周四發車

    隨著 ToB(企業服務)的興起和 ToC(消費互聯網)產品進入成熟期,線上故障帶來的損失越來越大,代碼質量越來越重要,而「質量內建」正是 DevOps 核心理念之一。 **《DevOps 代碼質量實戰(Java 版)》**為 CODING DevOps 代碼質量實戰系列的最后一課,同時也是本系列的 ......

    uj5u.com 2020-09-10 05:07:52 more
  • 敏捷軟體工程實踐書籍

    Scrum轉型想要做好,第一步先了解并真正落實Scrum,那么我推薦的Scrum書籍是要看懂并實踐的。第二步是團隊的工程實踐要做扎實。 下面推薦工程實踐書單: 重構:改善既有代碼的設計 決議極限編程 : 擁抱變化 代碼整潔代碼 程式員的職業素養 修改代碼的藝術 撰寫可讀代碼的藝術 測驗驅動開發 : ......

    uj5u.com 2020-09-10 05:07:55 more
  • Jenkins+svn+nginx實作windows環境自動部署vue前端專案

    前面文章介紹了Jenkins+svn+tomcat實作自動化部署,現在終于有空抽時間出來寫下Jenkins+svn+nginx實作自動部署vue前端專案。 jenkins的安裝和配置已經在前面文章進行介紹,下面介紹實作vue前端專案需要進行的哪些額外的步驟。 注意:在安裝jenkins和nginx的 ......

    uj5u.com 2020-09-10 05:08:49 more
  • CODING DevOps 微服務專案實戰系列第一課,明天等你

    CODING DevOps 微服務專案實戰系列第一課**《DevOps 微服務專案實戰:DevOps 初體驗》**將由 CODING DevOps 開發工程師 王寬老師 向大家介紹 DevOps 的基本理念,并探討為什么現代開發活動需要 DevOps,同時將以 eShopOnContainers 項 ......

    uj5u.com 2020-09-10 05:09:14 more
  • CODING DevOps 微服務專案實戰系列第二課來啦!

    近年來,工程專案的結構越來越復雜,需要接入合適的持續集成流水線形式,才能滿足更多變的需求,那么如何優雅地使用 CI 能力提升生產效率呢?CODING DevOps 微服務專案實戰系列第二課 《DevOps 微服務專案實戰:CI 進階用法》 將由 CODING DevOps 全堆疊工程師 何晨哲老師 向 ......

    uj5u.com 2020-09-10 05:09:33 more
  • CODING DevOps 微服務專案實戰系列最后一課,周四開講!

    隨著軟體工程越來越復雜化,如何在 Kubernetes 集群進行灰度發布成為了生產部署的”必修課“,而如何實作安全可控、自動化的灰度發布也成為了持續部署重點關注的問題。CODING DevOps 微服務專案實戰系列最后一課:**《DevOps 微服務專案實戰:基于 Nginx-ingress 的自動 ......

    uj5u.com 2020-09-10 05:10:00 more
  • CODING 儀表盤功能正式推出,實作作業資料可視化!

    CODING 儀表盤功能現已正式推出!該功能旨在用一張張統計卡片的形式,統計并展示使用 CODING 中所產生的資料。這意味著無需額外的設定,就可以收集歸納寶貴的作業資料并予之量化分析。這些海量的資料皆會以圖表或串列的方式躍然紙上,方便團隊成員隨時查看各專案的進度、狀態和指標,云端協作迎來真正意義上 ......

    uj5u.com 2020-09-10 05:11:01 more
最新发布
  • windows系統git使用ssh方式和gitee/github進行同步

    使用git來clone專案有兩種方式:HTTPS和SSH:
    HTTPS:不管是誰,拿到url隨便clone,但是在push的時候需要驗證用戶名和密碼;
    SSH:clone的專案你必須是擁有者或者管理員,而且需要在clone前添加SSH Key。SSH 在push的時候,是不需要輸入用戶名的,如果配置... ......

    uj5u.com 2023-04-19 08:41:12 more
  • windows系統git使用ssh方式和gitee/github進行同步

    使用git來clone專案有兩種方式:HTTPS和SSH:
    HTTPS:不管是誰,拿到url隨便clone,但是在push的時候需要驗證用戶名和密碼;
    SSH:clone的專案你必須是擁有者或者管理員,而且需要在clone前添加SSH Key。SSH 在push的時候,是不需要輸入用戶名的,如果配置... ......

    uj5u.com 2023-04-19 08:35:34 more
  • 2023年農牧行業6大CRM系統、5大場景盤點

    在物聯網、大資料、云計算、人工智能、自動化技術等現代資訊技術蓬勃發展與逐步成熟的背景下,數字化正成為農牧行業供給側結構性變革與高質量發展的核心驅動因素。因此,改造和提升傳統農牧業、開拓創新現代智慧農牧業,加快推進農牧業的現代化、資訊化、數字化建設已成為農牧業發展的重要方向。 當下,企業數字化轉型已經 ......

    uj5u.com 2023-04-18 08:05:44 more
  • 2023年農牧行業6大CRM系統、5大場景盤點

    在物聯網、大資料、云計算、人工智能、自動化技術等現代資訊技術蓬勃發展與逐步成熟的背景下,數字化正成為農牧行業供給側結構性變革與高質量發展的核心驅動因素。因此,改造和提升傳統農牧業、開拓創新現代智慧農牧業,加快推進農牧業的現代化、資訊化、數字化建設已成為農牧業發展的重要方向。 當下,企業數字化轉型已經 ......

    uj5u.com 2023-04-18 08:00:18 more
  • 計算機組成原理—存盤器

    計算機組成原理—硬體結構 二、存盤器 1.概述 存盤器是計算機系統中的記憶設備,用來存放程式和資料 1.1存盤器的層次結構 快取-主存層次主要解決CPU和主存速度不匹配的問題,速度接近快取 主存-輔存層次主要解決存盤系統的容量問題,容量接近與價位接近于主存 2.主存盤器 2.1概述 主存與CPU的聯 ......

    uj5u.com 2023-04-17 08:20:31 more
  • 談一談我對協同開發的一些認識

    如今各互聯網公司普通都使用敏捷開發,采用小步快跑的形式來進行專案開發。如果是小專案或者小需求,那一個開發可能就搞定了。但對于電商等復雜的系統,其功能多,結構復雜,一個人肯定是搞不定的,所以都是很多人來共同開發維護。以我曾經待過的商城團隊為例,光是后端開發就有七十多人。 為了更好地開發這類大型系統,往 ......

    uj5u.com 2023-04-17 08:18:55 more
  • 專案管理PRINCE2核心知識點整理

    PRINCE2,即 PRoject IN Controlled Environment(受控環境中的專案)是一種結構化的專案管理方法論,由英國政府內閣商務部(OGC)推出,是英國專案管理標準。
    PRINCE2 作為一種開放的方法論,是一套結構化的專案管理流程,描述了如何以一種邏輯性的、有組織的方法,... ......

    uj5u.com 2023-04-17 08:18:51 more
  • 談一談我對協同開發的一些認識

    如今各互聯網公司普通都使用敏捷開發,采用小步快跑的形式來進行專案開發。如果是小專案或者小需求,那一個開發可能就搞定了。但對于電商等復雜的系統,其功能多,結構復雜,一個人肯定是搞不定的,所以都是很多人來共同開發維護。以我曾經待過的商城團隊為例,光是后端開發就有七十多人。 為了更好地開發這類大型系統,往 ......

    uj5u.com 2023-04-17 08:18:00 more
  • 專案管理PRINCE2核心知識點整理

    PRINCE2,即 PRoject IN Controlled Environment(受控環境中的專案)是一種結構化的專案管理方法論,由英國政府內閣商務部(OGC)推出,是英國專案管理標準。
    PRINCE2 作為一種開放的方法論,是一套結構化的專案管理流程,描述了如何以一種邏輯性的、有組織的方法,... ......

    uj5u.com 2023-04-17 08:17:55 more
  • 計算機組成原理—存盤器

    計算機組成原理—硬體結構 二、存盤器 1.概述 存盤器是計算機系統中的記憶設備,用來存放程式和資料 1.1存盤器的層次結構 快取-主存層次主要解決CPU和主存速度不匹配的問題,速度接近快取 主存-輔存層次主要解決存盤系統的容量問題,容量接近與價位接近于主存 2.主存盤器 2.1概述 主存與CPU的聯 ......

    uj5u.com 2023-04-17 08:12:06 more